0

私はこのスクリプトを持っています。フォルダーの場所をスキャンし、フォルダーの名前を CSV ファイルから取得したフォルダー所有者の名前にマップし、AD からユーザーの電子メール アドレスを取得して、クリック可能なmailto:リンクとして新しい列に追加します。これは、HTML ページの表にすべて出力されます。

これを数回繰り返して、現在は最終段階です。

私の問題は、フォルダ名をmailto body HTMLに取り込む方法です。

Import-Module ActiveDirectory

function Encode($str) {
return ( $str -replace ' ', '%20' -replace '\n', '%0A%0D' )
}

function name($filename, $folderowners, $directory, $output){
$subject = Encode("Folder Access Request")
$body = Encode("Please can I have access to the following folder $directory")
$server = hostname
$date =  Get-Date -format "dd-MMM-yyyy HH:mm"
$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color:black;}"
$a = $a + "Table{background-color:#ffffff;border-collapse: collapse;}"
$a = $a + "TH{border-width:1px;padding:0px;border-style:solid;border-color:black;}"
$a = $a + "TR{border-width:1px;padding-left:5px;border-style:solid;border-color:black;}"
$a = $a + "TD{border-width:1px;padding-left:5px;border-style:solid;border-color:black;}"
$a = $a + "body{ font-family:Calibri; font-size:11pt;}"
$a = $a + "</style>"

$c = " <br></br> Content"
$c = $c +"<p>More Content</p>"
$x = ""

$b = Import-Csv $folderowners
$mappings = @{}
$b | % { $mappings.Add($_.FolderName, $_.Owner) }


       Get-ChildItem $directory | where {$_.PSIsContainer -eq $True} | select Name, Path, @{n="Owner";e={$mappings[$_.Name]}}, @{n="Email";e={"mailto:"+((Get-ADUser $mappings[$_.Name] -Properties mail).mail)}}  | sort -property Name | 
 ConvertTo-Html -head $a -PostContent $c | % {
  $body = Encode("Please can I have access to the following folder " + $_.Name)
  $_ -replace '(mailto:)([^<]*)', 
    "<a href=`"`$1`$2?subject=$subject&amp;body=$body`">`$2</a>"
} | Out-File $output
}

    name "gdrive" "\\server\departmentfolders$\location\gdrive.csv" "x:" "\\server\departmentfolders$\location\gdrive.html"

これが出てきて、電子メールの本文にパスが表示されますが、パスの場所 \server\departmentfolders$ だけが含まれているフォルダー名は含まれていません。これは、ほとんどフォルダー名が必要なだけです...

4

1 に答える 1

0

置換文字列で変数を使用する場合は、文字列を一重引用符ではなく二重引用符で囲む必要があります。

$_ -replace '...', "... $directory ..."

ただし、その場合、置換文字列の他の要素をエスケープする必要があります。つまり、内部の二重引用符と、正規表現のグループ ( ) への後方参照 ( $1, ) です。$2(...)

$_ -replace '(...)(...)', "<a href=`"`$1`$2...`">..."

%20また、URL 内のスペース ( ) と改行 ( ) をエンコードする必要が%0A%0Dあります。

function Encode($str) {
  return ( $str -replace ' ', '%20' -replace '\n', '%0A%0D' )
}

全体は次のようになります。

Import-Module ActiveDirectory

function Encode($str) {
  return ( $str -replace ' ', '%20' -replace '\n', '%0A%0D' )
}

function name($filename, $folderowners, $directory, $output) {
  $subject = Encode("Folder Access Request")

  ...

  Get-ChildItem $directory | ... |
    ConvertTo-Html -head $a -PostContent $c | % {
      if ( $_ -match '^<tr><td>([^<]*)' ) {
        $body = Encode("Please can I have access to the following folder " +
                       "{0}\{1}" -f ($directory, $matches[1]))
      }
      $_ -replace '(mailto:)([^<]*)', 
        "<a href=`"`$1`$2?subject=$subject&amp;body=$body`">`$2</a>"
    } | Out-File $output
}

...
于 2013-05-01T07:33:26.253 に答える