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

时间:2011-05-17 21:06:39

标签: powershell command-line batch-file powershell-v2.0

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

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命令(或语句)?

6 个答案:

答案 0 :(得分:63)

这是代码在批处理文件中的样子(经过测试,有效):

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;}"

基于以下信息:

http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/

答案 1 :(得分:9)

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

答案 2 :(得分:6)

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

<强>的优点:

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

<强>缺点:

  • 需要powershell版本2.0 +

batch-ps-script.cmd 的评论和可运行示例:

<# : 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文件的摘录:

<# : 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...

答案 3 :(得分:3)

<强> untested.cmd

;@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

答案 4 :(得分:0)

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

;@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
)

答案 5 :(得分:0)

从批处理文件调用多行 PowerShell 语句时,除最后一行外,每一行都以插入符号结束。您不必在行首留出额外的间距,这是我的惯例。

PowerShell 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

如果你正在输入一个像 Select-Object 这样的 cmdlet,你需要用一个分号来结束命令,否则它会包含下一行。

Powershell $disk = Get-WmiObject Win32_LogicalDisk -Filter """"DeviceID='D:'"""" ^| Select-Object Freespace; ^
           Exit ([math]::truncate($disk.freespace / 1GB))