从Powershell搜索创建HTML链接

时间:2016-03-11 17:08:00

标签: powershell

我已创建此脚本以查找某些文件。

$wpath = Get-ChildItem F:\@software\ -recurse
$outp = $wpath | Where-Object {$_.extension -eq ".wav" -and $_.name -like "*hou*"}
$outp = $outp | Select-Object -Property Fullname
$outp | ConvertTo-Html | Out-File f:\@software\htmlout99.html
ii f:\@software\htmlout99.html

这将以正确的格式提供我之后的结果,例如

F:\@software\TFTP-Root\14Hours.wav  
F:\@software\TFTP-Root\ddHours.wav

如何在HTML输出可点击链接中创建结果,即文件的可点击链接?

3 个答案:

答案 0 :(得分:2)

您需要使用ex来自己定义链接。计算的属性。 ConvertTo-HTML会对某些字符进行hmmlencode(如< and >),因此我们需要在保存前对其进行解码。例如:

#Load System.Web to get HttpUtility
Add-Type -AssemblyName System.Web

$html = Get-ChildItem -Path F:\@software\ -Recurse |
Where-Object {$_.extension -eq ".wav" -and $_.name -like "*hou*"} |
Select-Object -Property @{n="Links";e={ "<a href='$(([uri]$_.FullName).AbsoluteUri)'>$($_.FullName)</a>" }} |
ConvertTo-Html -Property "Links"

#Decode special characters like < > etc.
[System.Web.HttpUtility]::HtmlDecode($html) | Set-Content "f:\@software\htmlout99.html"

答案 1 :(得分:1)

蛮力方法就是使用它:

$wpath = Get-ChildItem F:\@software\ -recurse
$outp = $wpath | Where-Object {$_.extension -eq ".wav" -and $_.name -like "*hou*"}
$outp = $outp | Select-Object -Property @{n='FullName';e={'<a href="file://{0}">{0}</a>' -f $_.FullName}}
$outp | ConvertTo-Html | ForEach-Object { $_.Replace('&quot;','"').Replace('&lt;','<').Replace('&gt;','>') } | Out-File f:\@software\htmlout99.html
ii f:\@software\htmlout99.html

它使用calculated property将FullName转换为链接。然后将其转换为HTML。问题是ConvertTo-Html转义控制字符,因为它假设内容是而不是 HTML。我确信,如果你的文件名中确实有控制字符,你一定会遇到一些问题。

真正的解决方案是完全放弃ConvertTo-Html,然后自己滚动。

答案 2 :(得分:1)

另一种类似@Bacon Bits的提及方式。我经常使用这种方法来处理我的sched任务通知。

import re

if re.match('^[A-Z][a-z]{2}[0-9]{3}$', UserID):
    print("Correct Format")
else:
    print("Wrong Format")