如何从批处理文件中执行powershell命令?

How to execute powershell commands from a batch file?

我有一个PowerShell脚本将网站添加到Internet Explorer中的可信站点:

1
2
3
4
5
set-location"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set-location ZoneMap\Domains
new-item TESTSERVERNAME
set-location TESTSERVERNAME
new-itemproperty . -Name http -Value 2 -Type DWORD

我想从批处理文件中执行这些PowerShell命令。 当我必须运行单个命令时似乎很简单,但在这种情况下,我有一系列相关的命令。 我想避免为批处理调用PS脚本创建单独的文件 - 所有内容都必须在批处理文件中。

问题是:如何从批处理文件中执行powershell命令(或语句)?


这是批处理文件中的代码(测试,工作):

1
powershell -Command"& {set-location 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'; set-location ZoneMap\Domains; new-item SERVERNAME; set-location SERVERNAME; new-itemproperty . -Name http -Value 2 -Type DWORD;}"

根据以下信息:

PowerShell script in a .bat file


键入cmd.exe Powershell -Help并查看示例。


此解决方案类似于walid2mi(感谢您的灵感),但允许通过Read-Host cmdlet输入标准控制台。

优点:

  • 可以像标准的.cmd文件一样运行
  • 批处理和PowerShell脚本只有一个文件
  • powershell脚本可能是多行的(易于阅读的脚本)
  • 允许标准控制台输入(通过标准方式使用Read-Host cmdlet)

缺点:

  • 需要PowerShell版本2.0+

batch-ps-script.cmd的注释和可运行示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<# : 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-Expression $([System.IO.File]::ReadAllText('%~f0'))"
: 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"

# example: standard input from console
$variableInput = Read-Host"Continue? [Y/N]"
if ($variableInput -ne"Y") {
    Write-Host"Exit script..."
    break
}

# example: call standard powershell command
Get-Item .

.cmd文件的摘录:

1
2
3
4
5
6
7
8
9
<# : batch script
@echo off
setlocal
cd %~dp0
powershell -executionpolicy remotesigned -Command"Invoke-Expression $([System.IO.File]::ReadAllText('%~f0'))"
endlocal
goto:eof
#>

# here write your powershell commands...


untested.cmd

1
2
3
4
5
6
7
8
9
10
11
12
13
;@echo off
;Findstr -rbv ; %0 | powershell -c -
;goto:sCode

set-location"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set-location ZoneMap\Domains
new-item TESTSERVERNAME
set-location TESTSERVERNAME
new-itemproperty . -Name http -Value 2 -Type DWORD

;:sCode
;echo done
;pause & goto :eof

寻找将powershell脚本放入批处理文件的可能性,我找到了这个线程。 walid2mi的想法并没有100%用于我的脚本。但是通过一个临时文件,包含它编写的脚本。这是批处理文件的框架:

1
2
3
4
5
6
7
8
9
10
11
12
;@echo off
;setlocal ENABLEEXTENSIONS
;rem make from X.bat a X.ps1 by removing all lines starting with ';'
;Findstr -rbv"^[;]" %0 > %~dpn0.ps1
;powershell -ExecutionPolicy Unrestricted -File %~dpn0.ps1 %*
;del %~dpn0.ps1
;endlocal
;goto :EOF
;rem Here start your power shell script.
param(
    ,[switch]$help
)