使用 ConvertTo-HTML 将 URL 列表转换为可点击的 HTML 链接 [英] Convert a list of URLs into clickable HTML links using ConvertTo-HTML
问题描述
我有下面的函数可以创建一个 HTML 表格,但是我希望表格中的值指向超链接.
函数 MyFunction{清除主机$item=$null$hash = $null #hashTable$hash =@{"Google.com" = "www.google.com";"Yahoo" = "www.yahoo.com";"My Directory" ="C:\Users\Public\Favorites" ;"MSN" = "www.msn.com"}ForEach($item in @($hash.KEYS.GetEnumerator())){$hash |Sort-Object - 属性名称}$hash.GetEnumerator() |sort - 属性名称 |选择-对象名称、值 |转换为 HTML |输出文件 .\Test.html}我的功能
开始之前的小说明:ForEach
似乎毫无意义,因为它只是为表中的每个元素输出已排序的表.所以它被删除了.
ConvertTo-HTML
本身非常适合使用 PowerShell 对象制作表格,但我们需要的是超链接.ConvertTo-HTML
确实支持计算属性,所以起初制作一个格式化的超链接似乎很容易.
ConvertTo-HTML -Property *,@{Label="Link";Expression={"<a href='$($_.Value)'>$($_.Name)</a>"}}
与此相关的小问题是 ConvertTo-Html
对发送给它的字符串进行了一些转换,这混淆了我们的意图.查看创建的输出文件,我们看到以下 Yahoo 链接:
<td><a href='www.yahoo.com'>Yahoo</a></td>
ConvertTo-Html
使我们的文本浏览器变得友好,这通常很好,但现在阻碍了我们.阅读来自 .它以不同的方式处理,所以我不知道将其标记为重复.
I have the function below that will create an HTML table however I want the values in my table to hyperlinks.
Function MyFunction{
clear-host
$item=$null
$hash = $null #hashTable
$hash =@{"Google.com" = "www.google.com"; "Yahoo" = "www.yahoo.com";"My Directory" ="C:\Users\Public\Favorites" ;"MSN" = "www.msn.com"}
ForEach($item in @($hash.KEYS.GetEnumerator())){
$hash | Sort-Object -Property name
}
$hash.GetEnumerator() | sort -Property Name |Select-Object Name, Value | ConvertTo-HTML | Out-File .\Test.html
}
MyFunction
Small side note before we start: The ForEach
seems pointless as it just outputs the sorted table for each element in the table. So that gets removed.
ConvertTo-HTML
on its own is great for making table with PowerShell object but was we need is hyperlinks instead. ConvertTo-HTML
does support calculated properties so at first it would seem easy to just make a formatted hyperlink.
ConvertTo-HTML -Property *,@{Label="Link";Expression={"<a href='$($_.Value)'>$($_.Name)</a>"}}
The small issue with that is ConvertTo-Html
does some conversion on the string being sent to it which obfuscates our intention. Looking in the output file that is created we see the following for the Yahoo link:
<td><a href='www.yahoo.com'>Yahoo</a></td>
ConvertTo-Html
made our text browser friendly which would be nice normally but hinders us now. Reading a blog from PowerShell Magazine covers this issue by decoding the html before it is send to file.
Function MyFunction{
clear-host
$hash = @{"Google.com" = "www.google.com"; "Yahoo" = "www.yahoo.com";"My Directory" ="C:\Users\Public\Favorites" ;"MSN" = "www.msn.com"}
$hash.GetEnumerator() | sort -Property Name | Select-Object Name, Value |
ConvertTo-HTML -Property *,@{Label="Link";Expression={"<a href='$($_.Value)'>$($_.Name)</a>"}}
}
$html = MyFunction
Add-Type -AssemblyName System.Web
[System.Web.HttpUtility]::HtmlDecode($html) | Out-File C:\temp\test.html
Using [System.Web.HttpUtility]::HtmlDecode
converts values like <
back to what we want them to be. Have a look at the output
Went looking to see if this has been asked before and there is a similar answer: How to generate hyperlinks to .htm files in a directory in Powershell?. It is handled in a different way so im on the fence about marking this as a duplicate.
这篇关于使用 ConvertTo-HTML 将 URL 列表转换为可点击的 HTML 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!