用于重新启动服务的 PowerShell 脚本
我的任务是按键盘序列(例如 Ctrl +Shift +R)来重新启动 Windows 服务。
我有一个脚本,当使用管理权限启动时,它可以在 PowerShell ISE 中正常工作。
当我尝试使用 PowerShell 脚本时,由于管理权限不足而失败。令人恼火的是,我可以让它与老式的 bat 文件一起工作,但不能与 PowerShell 一起工作。
问题的根源在于 PowerShell 脚本的快捷方式的管理权限框呈灰色。到目前为止,还没有解决方法可以克服这个特权问题。
有什么想法吗?
My mission is to press a keyboard sequence, such as Ctrl +Shift +R, to restart a Windows Service.
I have a script which works fine in the PowerShell ISE, when launched with administrative privileges.
When I try with a PowerShell script it fails due to insufficient Administrative Privileges. It’s galling that I can get it to work with an old-fashioned bat file, but not PowerShell.
The root of the problem is that shortcuts to a PowerShell script have their Administrative privileges box greyed out. So far no work-around has overcome this privilege problem.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是在脚本中启动另一个提升的 PowerShell 会话,如下所示:
这应该会提示提升新的 PowerShell 会话。我认为您应该能够设置 -WindowStyle 参数,以便新窗口不会出现(如果您需要该行为)。请注意,您需要指定现有脚本的完整路径。
One approach is to start another elevated PowerShell session within your script like so:
That should prompt to elevate the new PowerShell session. I think you should be able to set the -WindowStyle parameter such that the new window doens't appear (if you need that behavior). Note that you will need to specify the full path to your existing script.
您建议您不喜欢用批处理文件(例如 net start)解决这个问题,我认为因为批处理文件本质上比 powershell 脚本更受限制。不过,您可以做的是将 Ps 脚本包装在批处理文件中,以实现您既定的目标 - 使用键盘快捷键运行 powershell 脚本而不会出现访问权限问题。在批处理文件中尝试一下:
You suggest you don't like solving this problem with a batch file (e.g. net start), I think because batch files are inherently more limited than powershell scripts. What you can do is wrap your Ps script in a batch file, though, for the sake of accomplishing your stated objective -- running a powershell script with a keyboard shortcut without access permissions issues. Try this in a batch file: