0

我有这个脚本。它扫描文件夹位置并将文件夹名称映射到从 CSV 文件中提取的文件夹所有者的名称,然后从 AD 获取用户电子邮件地址并将其作为可点击mailto:链接添加到新列中。然后,这将在 HTML 页面上的表格中全部输出。

经历了几次迭代,现在进入了最后阶段。

我现在的问题是如何将文件夹名称拉入 mailto 正文 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...`">..."

您还应该对URL 中的空格 ( %20) 和换行符 ( ) 进行编码。%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 回答