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

如何从批处理文件执行powershell命令?,powershell,command-line,batch-file,powershell-2.0,Powershell,Command Line,Batch File,Powershell 2.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 -T

我使用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命令或语句?

这是批处理文件中的代码经过测试后的效果:

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;}"
根据以下信息:

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

untested.cmd


为了寻找将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
)

此解决方案类似于walid2mi感谢您的启发,但允许通过Read-Host cmdlet进行标准控制台输入

优点:

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

需要powershell 2.0版+ batch-ps-script.cmd的注释和可运行示例:

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

从批处理文件调用多行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))

另见。。。我怎么能把长线分开?与您的示例类似的行。@JuanPablo很抱歉回复太晚,但直到今天才承诺检查我的stackoverflow使用情况并更加积极。你得到答案了吗?可以传递多个.ps1文件吗?你能举个例子吗?听起来像是另一个问题。用法说明:在某些边缘情况下,cd%~dp0不起作用,这种调用方式会将$PSScriptRoot设置为null,因为我们是作为一系列命令而不是脚本运行的。我已经删除了cd%~dp0,并将调用的表达式更改为$$ScriptHome='%~dp0';[System.IO.File]::ReadAllText“%$dpf0”来处理此问题。我是否在我的powershell命令中包含?@JustinGoldberg--此注释块中的是.bat/.cmd commands for powershell run,在该注释块之后,您可以使用任何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
Powershell $disk = Get-WmiObject Win32_LogicalDisk -Filter """"DeviceID='D:'"""" ^| Select-Object Freespace; ^
           Exit ([math]::truncate($disk.freespace / 1GB))