[玩转系统] 星期五有趣 文本转 HTML
作者:精品下载站 日期:2024-12-14 07:43:23 浏览:11 分类:玩电脑
星期五有趣 文本转 HTML
我的想法是获取一个文本文件(例如脚本),并使用交替阴影行的样式将其转换为 HTML 文档。我已经从其他一些脚本中获得了样式标头,因此该部分很简单。我的第一次尝试是简单地获取文件内容并转换为 HTML。
get-content .\XMLDemo.txt | convertto-html
但如果你在家尝试的话,你会发现一个问题。 ConvertTo-HTML 正在寻找一组对象属性,而文件的内容没有任何属性。或者换句话说,我需要将文件的内容变成一个对象。因为我知道我想要包含行号,所以我想我可以使用 PowerShell 3.0 中引入的 [pscustomobject] 类型。
get-content .\XMLDemo.txt | foreach -begin {$i=0} -process {
#create a custom object out of each line of text
$i++
[pscustomobject]@{Line=$i;" "=$_}
}
运行时,每行都会变成一个自定义对象,其中包含行号属性和每行值属性。通常,您需要为您的属性命名,但在本例中我不需要属性名称,因此我使用空白。你最终会明白为什么。现在我可以将其通过管道传输到 ConvertTo-HTML 并获得我期望的输出。这就是剧本的关键。这是完整的函数,ConvertTo-HTMLListing
#requires -version 3.0
Function ConvertTo-HTMLListing {
<#
.Synopsis
Convert text file to HTML listing
.Description
This command will take the contents of a text file and create an HTML document complete with line numbers.
There are options to suppress the line numbers and to skip any blank lines. The command is intended to convert one file at a time although you can pipe a file name to the command.
.Example
PS C:\> ConvertTo-HTMLListing -path c:\scripts\myscript.ps1 | out-file d:\MyScript.htm
.Example
PS C:\> dir c:\work\myfile.ps1 | ConvertTo-HTMLListing | Out-file d:\myfile.htm
.Example
PS C:\> foreach ($file in (dir c:\work\*.txt)) { ConvertTo-HTMLListing $file.fullname | Out-File D:$($File.basename).htm }
Create an HTML file for each text file in C:\work.
.Notes
Last Updated: 8/22/2014
Version : 0.9
Learn more:
PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones6/)
PowerShell Deep Dives (http://manning.com/hicks/)
Learn PowerShell in a Month of Lunches (http://manning.com/jones3/)
Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
.Link
https://jdhitsolutions.com/blog/2014/08/friday-fun-text-to-html
.Link
ConvertTo-HTML
#>
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory,HelpMessage="Enter the path to the file",
ValueFromPipeline,ValueFromPipelineByPropertyName)]
[alias("PSPath")]
[ValidateScript({Test-Path $_})]
[string]$Path,
[switch]$SkipBlankLines,
[switch]$NoLineNumber
)
Begin {
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
$head = @'
<Title>Script Listing</Title>
<style>
body { background-color:#FFFFFF;
font-family:Consolas;
font-size:12pt; }
td, th { border:0px solid black;
border-collapse:collapse; }
th { color:white;
background-color:black; }
table, tr, td, th { padding: 0px; margin: 0px }
tr:nth-child(odd) {background-color: lightgray}
table { margin-left:25px; }
h2 {
font-family:Tahoma;
}
.footer
{ color:green;
margin-left:25px;
font-family:Tahoma
}
</style>
'@
} #begin
Process {
$file = Resolve-Path -Path $Path
Write-Verbose "Processing $($file.providerpath)"
$body = "<H2>$($file.providerpath)</H2>"
$content = Get-Content -Path $file
if ($SkipBlankLines) {
#filter out blank lines
Write-Verbose "Skipping blanks"
$content = $content | where {$_ -AND $_ -match "\w"}
}
Write-Verbose "Converting text to objects"
$processed = $content | foreach -begin {$i=0} -process {
#create a custom object out of each line of text
$i++
[pscustomobject]@{Line=$i;" "=$_}
}
if ($NoLineNumber) {
$processed = $processed | Select " "
}
Write-Verbose "Creating HTML"
$body+= $processed | ConvertTo-Html -Fragment | foreach {
#convert spaces to HTML spaces
$_.replace(" "," ")
}
$post = "<br><div class='footer'>$(Get-Date)</div>"
#create the HTML output and write to the pipeline
ConvertTo-HTML -Head $head -Body $body -PostContent $post
} #process
End {
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end
} #end function
该函数采用文件名并将文本内容转换为 HTML 文档。
该功能包括一个嵌入的样式表,它允许我为不同的部分以及交替的行使用不同的字体。唯一的另一个“技巧”是我用 HTML 等效内容替换原始文本中的空格。
$body+= $processed | ConvertTo-Html -Fragment | foreach {
#convert spaces to HTML spaces
$_.replace(" "," ")
}
这就是为什么 PowerShell cmdlet 只做一件事很有帮助。如果 ConvertTo-HTML 自动写入文件,我就必须跳过更多的环节。但事实并非如此。该函数也仅将 HTML 写入管道。您仍然需要将其通过管道传输到 Out-File 进行保存。基于注释的帮助中有一些示例。
您会在输出中注意到该表有一个行号标题,但没有任何文本行。这就是为什么我在 pscustomobject 中使用空格作为属性名称。
我唯一拥有的其他功能是跳过空白行和行号的选项。
现在,在您开始说“是的,但是……怎么样”之前,我知道还有其他技术和 ISE 插件可以将脚本转换为完整的彩色 HTML 文件。您也可以简单地在浏览器中打开该文件并将其显示为文本。我的功能实际上是作为一种教学工具,尽管拥有一个基于 HTML 的 PowerShell 脚本库可能会很酷。
这有趣吗?你学到新东西了吗?如果没有的话,下周总是有的。周末愉快。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag