欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[转载][微调]使用批处理文件为运行外壳运行PowerShell脚本

程序员文章站 2022-03-09 12:04:49
...

废话不多说,直接上码:

<# : Begin batch (batch script is in commentary of powershell v2.0+)
@echo off
: Use local variables
setlocal
: Change current directory to script location - useful for including .ps1 files
cd %~dp0
: Invoke this file as powershell expression

powershell -executionpolicy remotesigned -Command "Invoke-Command -ScriptBlock ([scriptblock]::Create($([System.IO.File]::ReadAllText('%~f0')))) -ArgumentList ([string]'%*').split()"

: Restore environment variables present before setlocal and restore current directory
endlocal
: End batch - go to end of file
goto:eof
#>
# here start your powershell script

# example: include another .ps1 scripts (commented, for quick copy-paste and test run)
#. ".\anotherScript.ps1"

# include lib
. ".\lib\System.io.ps1"

脚本参考:

https://*.com/questions/6037146/how-to-execute-powershell-commands-from-a-batch-file
https://*.com/questions/13724940/how-to-run-a-powershell-script-from-the-command-line-and-pass-a-directory-as-a-p
https://*.com/questions/51934047/execute-a-remote-generic-powershell-script-with-generic-parameters

相关标签: PowerShell