为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

ibeautiful 2019-10-15 14:22:50
我正在尝试在PowerShell中运行此脚本。我将以下脚本另存为ps.ps1我的桌面。$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"Register-WMIEvent -Query $query -Action { invoke-item "C:\Program Files\abc.exe"}我已经制作了一个批处理脚本来运行此PowerShell脚本@echo offPowershell.exe set-executionpolicy remotesigned -File  C:\Users\SE\Desktop\ps.ps1pause但我收到此错误:
查看完整描述

3 回答

?
弑天下

TA贡献1818条经验 获得超7个赞

您需要-ExecutionPolicy参数:


Powershell.exe -executionpolicy remotesigned -File  C:\Users\SE\Desktop\ps.ps1

否则,PowerShell 会将参数视为要执行的行,尽管Set-ExecutionPolicy 它是cmdlet,但没有-File参数。


查看完整回答
反对 回复 2019-10-15
?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

我在这里的博客文章中解释了为什么要从批处理文件调用PowerShell脚本以及如何执行该脚本。


这基本上就是您要寻找的:


PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Users\SE\Desktop\ps.ps1'"

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


PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Users\SE\Desktop\ps.ps1""' -Verb RunAs}"

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


查看完整回答
反对 回复 2019-10-15
  • 3 回答
  • 0 关注
  • 3102 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信