2011-05-17 226 views
31

我有一个PowerShell脚本的网站添加到Internet Explorer中的受信任的站点:如何从批处理文件执行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 

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

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

+0

另见... http://stackoverflow.com/questions/2035193/how-to-run-a-powershell-script – SteveC 2013-08-29 09:32:23

回答

46

这是代码会是什么样子在一个批处理文件(测试工作):

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/

+2

我如何分割长线?与您的示例类似。 – JuanPablo 2013-10-08 14:08:56

6

类型输入cmd.exe Powershell -Help并看到这些例子。

1

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 
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 
) 
0

该解决方案类似于walid2mi(谢谢你的灵感),但允许在读主机cmdlet的标准控制台输入。

优点:

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

缺点:

  • 需要的PowerShell 2.0以上版本

评论和一批-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...