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

我正在尝试在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

但我收到此错误:

http://img3.mukewang.com/5da565ca0001693b06620161.jpg

ibeautiful
浏览 3129回答 3
3回答

弑天下

您需要-ExecutionPolicy参数:Powershell.exe -executionpolicy remotesigned -File  C:\Users\SE\Desktop\ps.ps1否则,PowerShell 会将参数视为要执行的行,尽管Set-ExecutionPolicy 它是cmdlet,但没有-File参数。

汪汪一只猫

我在这里的博客文章中解释了为什么要从批处理文件调用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脚本的整个路径。
打开App,查看更多内容
随时随地看视频慕课网APP