以管理员身份运行PowerShell脚本并隐藏PowerShell窗口

问题描述:

我有一个PowerShell脚本,需要以管理员身份运行时,我单击.bat文件启动它,但是,我还需要隐藏PowerShell窗口。我有一个脚本,目前启动它作为管理员,但我需要它来隐藏窗口以及。我试图隐藏--windowstyle,但它不起作用。这里是我的启动脚本:以管理员身份运行PowerShell脚本并隐藏PowerShell窗口

@ECHO OFF 
SET ThisScriptsDirectory=%~dp0 
SET PowerShellScriptPath=%ThisScriptsDirectory%PMCS_Full_InProgress.ps1 
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}"; 

任何人都可以帮我隐藏窗口吗?谢谢你的时间。

+0

'PowerShell -WindowStyle hidden'将隐藏窗口。使用'-NoProfile'和'-ExecutionPolicy Bypass'应该已经以admin部分的身份执行运行。因此,您的Endcode将是:'PowerShell -WindowStyle隐藏的-NoProfile -ExecutionPolicy Bypass -File C:\ PMCS_Full_InProgress.ps1'。不需要任何更多 – SimonS

+0

它看起来不像管理员那样运行不幸,是否有另一种方式来安排它或单词呢? –

已解决。我没有把-windowstyle hidden放在the "& {Start-Process PowerShell -Arguments . . .}之内,所以它根本没有认出它。我试图把它放在外面。谢谢你的帮助!

+0

所以我假设你只是右键点击.bat文件并说“以管理员身份运行”对不对?否则,他们是在调用时使用提升权限运行脚本的方法。对于最高海拔,我将使用Sysinternals的PsExec作为系统运行。 https://technet.microsoft.com/en-us/sysinternals/psexec.aspx – krousemw

+0

您不需要在批处理文件中用'start-process'启动powershell。只需使用我在您的问题的评论中编写的代码调用脚本即可。这不像现在这样好和正确 – SimonS