我正在尝试在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 off
Powershell.exe set-executionpolicy remotesigned -File C:\Users\SE\Desktop\ps.ps1
pause
但是我得到了这个错误:
发布于 2013-11-19 06:23:36
我将解释为什么要从批处理文件中调用PowerShell脚本,以及如何通过in my blog post here调用它。
这基本上就是你要找的东西:
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脚本的整个路径,正如我的博客所描述的那样。
发布于 2016-05-03 03:26:54
如果您希望在没有完全限定路径的情况下从当前目录运行,您可以使用:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& './ps.ps1'"
发布于 2014-03-30 03:38:20
如果您以管理员身份运行名为PowerShell的批处理文件,则最好像这样运行它,这样可以省去所有的麻烦:
powershell.exe -ExecutionPolicy Bypass -Command "Path\xxx.ps1"
最好使用Bypass
...
https://stackoverflow.com/questions/19335004
复制相似问题