如何从批处理文件执行 PowerShell 命令? [英] How to execute PowerShell commands from a batch file?

查看:109
本文介绍了如何从批处理文件执行 PowerShell 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a PowerShell script to add a website to a Trusted Sites in Internet Explorer:

set-location "HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings"
set-location ZoneMapDomains
new-item TESTSERVERNAME
set-location TESTSERVERNAME
new-itemproperty . -Name http -Value 2 -Type DWORD

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

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.

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

The question is: How to execute PowerShell commands (or statements) from a batch file?

推荐答案

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

This is what the code would look like in a batch file(tested, works):

powershell -Command "& {set-location 'HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings'; set-location ZoneMapDomains; 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/

这篇关于如何从批处理文件执行 PowerShell 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆