[英]How do I create a bat file that executes powershell script without an external ps1 file?
一種方法是將 Powershell 腳本嵌入到帶有回聲和注釋組合的 .bat 文件中:
echo `
REM | Out-Null <#
powershell.exe -ExecutionPolicy Bypass -Command "iex ((Get-Content \"%~f0\") -join \"`n\") | Out-Null"
goto :eof
#>
(powershell script here)
<#
:eof
::#>
這會導致控制台輸出中出現一些工件,但腳本成功地嵌入到 bat 文件中運行。 這可以正常工作,因為“echo”既是 Windows 命令行命令,也是 Powershell 命令。
編輯:這里是我多年后的改進版本:
@powershell.exe -ExecutionPolicy Bypass -Command "$_=((Get-Content \"%~f0\") -join \"`n\");iex $_.Substring($_.IndexOf(\"goto :\"+\"EOF\")+9)"
@goto :EOF
(powershell script here)
改進:
:EOF
)Write-Output
兼容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.