2011-05-17 225 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...