单个bat中调用powershell 多行注释法
1. 来源
How to execute powershell commands from a batch file?
kapitanrum 的回答:https://stackoverflow.com/a/41986771
2. 思路
bat中注释使用rem
或::
,而powershell单行注释用#
,多行注释用<##>
,这一差异可以轻松将ps1脚本转为bat后缀的脚本,而不需要做大量的改动。
要想在bat脚本中运行powershell命令,需要使用powershell.exe的-Command
参数。幸运的是,ps1中你可以使用Invoke-Command
(别名icm
)来运行scriptblock,所以我们只需要将脚本文本通过[scriptblock]::Create()
做一个简单的转换就好了。
3. 代码
如果脚本编码不同,设置Get-Content
读取时的编码,或者用[IO.File]
类指定编码格式读取脚本.
<# :
@powershell "icm ([scriptblock]::Create((gc '%~f0' -Raw -Encoding UTF8)))"
exit
#>
Write-Host "Hello World" -fore Red
pause
# powershell script
最后
以上就是体贴钢笔最近收集整理的关于如何从批处理文件执行Powershell命令?多行注释单个bat中调用powershell 多行注释法的全部内容,更多相关如何从批处理文件执行Powershell命令?多行注释单个bat中调用powershell内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复