将PowerShell脚本发布在Web上,通常有两种方式,第一种直接提供脚本文件的下载。这里单讲第二种。
第二种是将脚本代码中的关键字以可视化的形式展现在Web网页中,常见于博客和论坛中,也有两种方式:
一种是将脚本放置在code或pre标签中,然后通过Java Script脚本在客户端进行渲染,在WordPress尤为常见。另外一种是将脚本在发布前进行HTML着色渲染,让脚本本身包含了HTML样式。这里推荐一个PowerShell脚本,使用Powershell脚本自己给自己着色,真可谓善莫大焉。
其主要原理也得益于PowerShell的强大功能:在PowerShell中有一个类型System.Management.Automation.PSTokenType, 这个类型包含PowerShell脚本的语法标记。
PS D:\> [enum]::GetNames([System.Management.Automation.PSTokenType])
Unknown
Command
CommandParameter
CommandArgument
Number
String
Variable
Member
LoopLabel
Attribute
Type
Operator
GroupStart
GroupEnd
Keyword
Comment
StatementSeparator
NewLine
LineContinuation
还有一个方法[system.management.automation.psparser]: