用于重新启动服务的 PowerShell 脚本

发布于 2024-09-01 10:00:05 字数 316 浏览 8 评论 0原文

我的任务是按键盘序列(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲世九天 2024-09-08 10:00:05

一种方法是在脚本中启动另一个提升的 PowerShell 会话,如下所示:

Start-Process PowerShell.exe -arg '-nologo -noprofile script.ps1' -verb runas

这应该会提示提升新的 PowerShell 会话。我认为您应该能够设置 -WindowStyle 参数,以便新窗口不会出现(如果您需要该行为)。请注意,您需要指定现有脚本的完整路径。

One approach is to start another elevated PowerShell session within your script like so:

Start-Process PowerShell.exe -arg '-nologo -noprofile script.ps1' -verb runas

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.

木落 2024-09-08 10:00:05

您建议您不喜欢用批处理文件(例如 net start)解决这个问题,我认为因为批处理文件本质上比 powershell 脚本更受限制。不过,您可以做的是将 Ps 脚本包装在批处理文件中,以实现您既定的目标 - 使用键盘快捷键运行 powershell 脚本而不会出现访问权限问题。在批处理文件中尝试一下:

 powershell set-executionpolicy remotesigned
 powershell myscript.ps1

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:

 powershell set-executionpolicy remotesigned
 powershell myscript.ps1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文