首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从批处理文件运行PowerShell脚本

如何从批处理文件运行PowerShell脚本
EN

Stack Overflow用户
提问于 2013-10-12 21:57:43
回答 7查看 906.4K关注 0票数 232

我正在尝试在PowerShell中运行此脚本。我已经将下面的脚本作为ps.ps1保存在我的桌面上。

代码语言:javascript
复制
$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query $query -Action { invoke-item "C:\Program Files\abc.exe"}

我已经创建了一个批处理脚本来运行此PowerShell脚本

代码语言:javascript
复制
@echo off
Powershell.exe set-executionpolicy remotesigned -File  C:\Users\SE\Desktop\ps.ps1
pause

但是我得到了这个错误:

EN

回答 7

Stack Overflow用户

发布于 2013-11-19 06:23:36

我将解释为什么要从批处理文件中调用PowerShell脚本,以及如何通过in my blog post here调用它。

这基本上就是你要找的东西:

代码语言:javascript
复制
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Users\SE\Desktop\ps.ps1'"

如果您需要以管理员身份运行PowerShell脚本,请使用以下命令:

代码语言:javascript
复制
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Users\SE\Desktop\ps.ps1""' -Verb RunAs}"

我建议将批处理文件和PowerShell脚本文件放在同一个目录中,而不是硬编码PowerShell脚本的整个路径,正如我的博客所描述的那样。

票数 129
EN

Stack Overflow用户

发布于 2016-05-03 03:26:54

如果您希望在没有完全限定路径的情况下从当前目录运行,您可以使用:

代码语言:javascript
复制
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& './ps.ps1'"
票数 26
EN

Stack Overflow用户

发布于 2014-03-30 03:38:20

如果您以管理员身份运行名为PowerShell的批处理文件,则最好像这样运行它,这样可以省去所有的麻烦:

代码语言:javascript
复制
powershell.exe -ExecutionPolicy Bypass -Command "Path\xxx.ps1"

最好使用Bypass...

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19335004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档