本文介绍了如何将列表中的文件夹名称动态地包含到mailto URL中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有这个脚本。它扫描文件夹位置并将文件夹名称映射到文件夹所有者的名称,然后从CSV文件中获取用户的电子邮件地址,并将其添加到新列中作为可点击 mailto :链接。然后这些都会输出到HTML页面的表格中。 完成了一些迭代,然后进入最后阶段。 现在我的问题是如何将文件夹名称拉到mailto正文HTML中。 Import- Module ActiveDirectory 函数编码($ str){ return($ str -replace'','%20'-replace'\\\','%0A%0D')$ ody $编码(请我可以访问以下文件夹$目录) $服务器=主机名 $日期=获取日期格式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>内容 $ c = $ c +< p>更多内容< / p> $ x = $ b = Import-Csv $ folderowners $ mappings = @ {} $ b | %{$ mappings.Add($ _。FolderName,$ _。Owner)} Get-ChildItem $ directory |其中{$ _。PSIsContainer -eq $ True} |选择Name,Path,@ {n =Owner; e = {$ mappings [$ _。Name]}},@ {n =Email; e = {mailto:+((Get-ADUser $ mappings [$ _。Name] - 属性邮件).mail)}} | sort -property Name | ConvertTo-Html -head $ a -PostContent $ c | %{ $ body = Encode(请允许我访问以下文件夹+ $ _。Name) $ _ -replace'(mailto:)([^<] *)' ,< a href =``$ 1` $ 2?subject = $ subject& amp; body body = $ body`>`$ 2< / a> } | Out-File $输出} 名称gdrive\\server\departmentfolders $ \location\gdrive.csvx:\\ server\departmentfolders $ \ location_\\gdrive.html 现在出来了它的电子邮件正文显示的路径,但不包括文件夹名称只是路径位置\服务器\分区文件夹$这几乎只是需要文件夹名称...如果你想在替换字符串中使用变量,你必须在字符串周围使用双引号而不是单引号: $ _ -replace'...',... $ directory ... 在这种情况下,您必须转义替换字符串中的其他元素,即内部双引号和后引用( $ 1 , $ 2 )到正则表达式中的组((...)): $ _ -replace '(...)(...)',< a href =``$ 1` $ 2 ...`> ... %20 )和换行符(%0A% 0D )。 函数Encode($ str){ return( $ str -replace'','%20'-replace'\\\','%0A%0D')} 整个事情可能如下所示: Import-Module ActiveDirectory 函数编码($ str){ return($ str -replace'','%20'-replace'\\\','%0A%0D')} 函数名称($ filename,$ folderowners,$ directory,$ output){ $ subject =编码(文件夹访问请求) ... Get-ChildItem $ directory | ... | ConvertTo-Html -head $ a -PostContent $ c | %{ if($ _ -match'^< tr>< td>([^<] *)'){ $ body = Encode(Please can I access to the以下文件夹+ {0} \ {1}-f($ directory,$ matches [1]))} $ _ -replace'(mailto:)( [^<] *)',< a href =``$ 1` $ 2?subject = $ subject& amp; body = $ body`>`$ 2< / a> } | Out-File $ output } ... I have this script. It scans a folder location and maps the names of folders to that of the folder owners which is pulled from a CSV file it then gets the users email address from AD and adds it to a new column as a clickable mailto: link. This is then all output in a table on a HTML page.Gone through a few iterations of this and now at the final stage.My issue now is how to pull in the folder name to the mailto body HTML.Import-Module ActiveDirectoryfunction 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&body=$body`">`$2</a>"} | Out-File $output} name "gdrive" "\\server\departmentfolders$\location\gdrive.csv" "x:" "\\server\departmentfolders$\location\gdrive.html"This now comes out and in the body of the email it shows the path but doesnt include the folder name just the path location \server\departmentfolders$ which is very nearly just need the folder name... 解决方案 If you want to use variables in the replacement string you have to use double quotes instead of single quotes around the string:$_ -replace '...', "... $directory ..."In that case you have to escape other elements in the replacement string, though, namely inner double quotes and back-references ($1, $2) to groups ((...)) in the regular expression:$_ -replace '(...)(...)', "<a href=`"`$1`$2...`">..."Also you should encode spaces (%20) and line breaks (%0A%0D) in the URL.function Encode($str) { return ( $str -replace ' ', '%20' -replace '\n', '%0A%0D' )}The whole thing might look like this:Import-Module ActiveDirectoryfunction 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&body=$body`">`$2</a>" } | Out-File $output}... 这篇关于如何将列表中的文件夹名称动态地包含到mailto URL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 09:01