如何从批处理文件执行PowerShell命令?
我有一个 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命令(或语句)?
I have a PowerShell script to add a website to a Trusted Sites in 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
I want to execute these PowerShell commands from a batch file. It seems simple when I have to run a single command, BUT in this case I have a sequence of related commands. I want to avoid creating a separate file for the PS script to be called from the batch - everything must be in the batch file.
The question is: How to execute PowerShell commands (or statements) from a batch file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这就是批处理文件中的代码的样子(经过测试,有效):
基于以下信息:
http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/
This is what the code would look like in a batch file(tested, works):
Based on the information from:
http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/
此解决方案类似于 walid2mi(感谢您的灵感),但允许通过 Read-Host cmdlet 进行标准控制台输入。
优点:
缺点:
batch-ps-script.cmd 的注释和可运行示例:
.cmd 文件的片段:
This solution is similar to walid2mi (thank you for inspiration), but allows the standard console input by the Read-Host cmdlet.
pros:
cons:
Commented and runable example of batch-ps-script.cmd:
Snippet for .cmd file:
输入 cmd.exe
Powershell -Help
并查看示例。Type in cmd.exe
Powershell -Help
and see the examples.未经测试的.cmd
untested.cmd
从批处理文件调用多行 PowerShell 语句时,每行以插入符号结束,最后一行除外。行首不必有额外的间距,这是我的惯例。
如果您通过管道输入 Select-Object 等 cmdlet,则需要以分号结束命令,否则它将包含下一行。
When calling multiline PowerShell statements from a batch file, end each line with a caret, except the last line. You don't have to have the extra spacing at the beginning of the line, that's my convention.
If you are piping into a cmdlet like Select-Object, you need to terminate with a semicolon to end the command, otherwise it will include the next line.
我无法得到任何工作答案,但添加 @Hassan Voyeau 的
-Command
和;
与 @Ed Palmer 的格式配合使其起作用:在这里,
;
告诉 powershell.exe 被调用当前命令行到此结束,可以进一步指定更多命令行,而^
告诉 CMD(为了简单起见;它是实际上称为“Windows 脚本宿主”)下一行可能有更多字符串输入。从我们的角度来看,CMD 通过 CR/LF 行结尾将多个命令行解析为一个命令行转义字符^
以便 powershell.exe 看到单行命令行-Command set-location ...;设置位置...; ...
相反。powershell
参数,请在-Command
之前执行此操作,因为 powershell.exe 将-Command
后面的命令行解释为>-Command
的单个代码块参数。使用大括号显式分隔代码块,例如-Command {set-location ...; ...}
也不允许随后指定powershell
参数,例如-Command {...} -NoExit
。"
来分隔-Command
的参数,例如 @Hassan Voyeau 的,因为 CMD 将未闭合的"
内的^
解释为文字插入符字符。^
作为第一行之后任何行的第一个字符也会使 CMD 将其解释为文字插入符字符。在 1 之前使用空格可以在语义上指定一个空行。
<# .. #>
(PowerShell 的内联注释#
将所有后续字符表示为其注释的一部分无法转义,因为^
将多个命令行解析为一个命令行。)但是,如果没有"
,如 @Hassan Voyeau的来封装<
和>
,CMD会解析它们作为 I/O 重定向运算符,我们可以转义使用前缀插入符,如^<# COMMENT GOES HERE #^>
如果在空行上插入块注释,^
将会。成为该行的第一个字符,并将被解释为文字插入符字符,因此我们像之前一样通过插入前缀空格来解决这个问题:<前><代码>⋮
更多的?命令 ARG1 ARG2 ...; ^<# 行内块注释#^> ^
更多的? ^<# 单独行块注释#^> ^
更多的?命令 ARG1 ARG2 ...;^<# 不需要前缀/后缀空格 #^>^
⋮
`
。由于反引号不是 其转义字符之一(^
表示 I/ O 重定向运算符,\
用于双引号,%
用于百分比),CMD 不会消耗反引号。\
。 (引用/转义1/2) 例如:将导致:
I couldn't get any answers to work, but adding @Hassan Voyeau's
-Command
and;
's in tandem with @Ed Palmer's formatting made it work:Here,
;
tells powershell.exe being called that the current command-line ends there and more command-lines can be specified further while^
tells CMD (for the sake of simplicity; it's actually called "Windows Script Host") that there may be more string input on the next line. From our perspective, CMD parses multiple command-lines into one command-line via CR/LF line-endings escape character^
so that powershell.exe sees the one-line command-line-Command set-location ...; set-location ...; ...
instead.powershell
parameters, do so before-Command
since powershell.exe interprets command-lines subsequent to-Command
as-Command
's single code-block argument. Using curly-braces to explicitly delimit the code block like-Command {set-location ...; ...}
doesn't allow specifyingpowershell
parameters afterward, e.g.-Command {...} -NoExit
, either."
to delimit-Command
's argument like @Hassan Voyeau's since CMD interprets^
inside unclosed"
as a literal caret character.^
as the first character on any line after the first line will also make CMD interpret it as a literal caret character. Use a whitespacebefore one to semantically designate an empty line instead.
<# .. #>
(PowerShell's in-line comment#
which denotes all subsequent characters as part of its comment can't be escaped since, again,^
parses multiple command-lines into one command-line.) However, without"
as in @Hassan Voyeau's to encapsulate<
and>
, CMD will parse them as I/O redirection operators which we can escape by using a prefixing-caret like^<# COMMENT GOES HERE #^>
. If you insert a block-comment on an empty line,^
will become the first character of the line and will be interpreted as a literal caret character so we work around that like earlier by inserting a prefixing whitespace to get:`
. Since backticks aren't one of its escape characters (^
for I/O redirection operators,\
for double-quotes,%
for percent), CMD won't consume backticks.\
. (Quoting/Escaping 1/2) For instance:will result in:
在寻找将 powershell 脚本放入批处理文件的可能性时,我找到了这个线程。 walid2mi 的想法对于我的脚本来说并没有 100% 有效。但是通过一个临时文件,包含它所制定的脚本。这是批处理文件的框架:
Looking for the possibility to put a powershell script into a batch file, I found this thread. The idea of walid2mi did not worked 100% for my script. But via a temporary file, containing the script it worked out. Here is the skeleton of the batch file: