powershell 的 Stop-Service 和 NET-STOP 有什么区别 [英] Whats the difference between powershell's Stop-Service and NET-STOP

查看:80
本文介绍了powershell 的 Stop-Service 和 NET-STOP 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 powershell 中,我看到了多种停止服务的方法

In powershell, I've seen multiple ways to stop a service

更现代的方式

Stop-Service wuauserv

还有更传统的方式

NET STOP WUAUSERV

传统方式自动化要困难得多,因为它本身不是幂等的.

我有使用打包程序构建 Windows 黄金图像的 powershell 脚本.最初脚本使用 NET STOP.我发现一旦我切换到 Stop-Service,在安装 Windows 更新后重新启动虚拟机时,我似乎更频繁地失败.

I have powershell scripts that builds a windows golden images using packer. Initially the scripts used NET STOP. I found once I switched to Stop-Service, I seemed to get more frequent failures when rebooting a VM after installing windows updates.

Stop-ServiceNET STOP 是否产生相同的结果?或者它们之间是否存在差异可以解释为什么传统的看起来更可靠?

Do both Stop-Service and NET STOP produce the same result? Or are there differences between them that might explain why the legacy one seems more reliable?

推荐答案

对于 Windows 服务:

For a Windows service that is:

  • 目前正在运行
  • 原则上可停止

net stopStop-Service 应该是一样的,即同步:

也就是说,它们向指定的服务发送停止请求并等待停止完成(net stop 总是等待,而在PSv5+,您可以使用 Stop-Service-NoWait 开关选择退出等待.

That is, they send the specified service a stop request and wait for stopping to complete (net stop invariably waits, while, in PSv5+, you can opt out of waiting with Stop-Service's -NoWait switch).

net stop(如果服务已经停止会报错)不同,Stop-Service幂等(展示期望状态逻辑):如果目标服务已经处于停止状态,则该命令是一个安静的空操作.

Unlike net stop (which reports an error if the service is already stopped), Stop-Service is idempotent (exhibits desired-state logic): If the target service is already in the stopped state, the command is a quiet no-op.

(顺便说一句:Start-Service 也是同步的,但总是如此,而且也是幂等的.)

(As an aside: Start-Service is also synchronous, but invariably so, and is also idempotent.)

Set-Service -Status Stopped 应该Stop-Service 一样,除了:

  • Stop-Service 不同,它不支持 -Force 以停止具有正在运行的依赖项的服务(依赖于该服务的其他服务正在运行)停止).

  • unlike Stop-Service, it doesn't support -Force in order to stop a service with running dependents (other services that depend on the service being stopped).

由于我认为是错误你甚至不能停止那些本身依赖于其他服务的服务(!).

due to what I presume to be a bug you cannot even stop services that themselves depend on other services(!).

实际上,从 Windows PowerShell v5.1/PowerShell Core v6.0-rc 开始,您只能使用 Set-Service -Status Stopped 停止没有依赖项的服务(没有依赖于它们的服务),也不依赖于其他服务.

in effect, as of Windows PowerShell v5.1 / PowerShell Core v6.0-rc, you can only stop services with Set-Service -Status Stopped that have no dependents (no services that depend on them), nor themselves depend on other services.

可选阅读:查看Stop-ServiceStart-Service源代码:

Optional reading: looking at the Stop-Service and Start-Service source code:

GitHub 上公开的源代码适用于跨平台的 Core PowerShell 版本,但看起来有问题的代码基本上未修改过 WindowsPowerShell 版本.

The publicly available source code on GitHub is for the cross-platform Core edition of PowerShell, but it looks like the code in question was essentially taken unmodified from the Windows PowerShell version.

  • Stop-Service: The part where the code waits for the service to be in the ServiceControllerStatus.Stopped state[1] , which is only bypassed if the -NoWait switch is explicitly specified, in which case variable waitForServiceToStop is set to false.

Start-Service:部分代码总是等待服务处于ServiceControllerStatus.Running状态.

Start-Service: The part where the code invariably waits for the service to be in the ServiceControllerStatus.Running state.

[1] 如果达到目标状态 需要超过 2 秒,等待循环发出一个警告( 2秒),而继续等待;如果服务意外地既不在目标状态-挂起状态也不在目标状态,则等待只会中止并出现错误.上>

[1] If reaching the target state takes longer than 2 seconds, the waiting loop issues a warning (every 2 seconds) while continuing to wait; waiting is only aborted with an error if the service is unexpectedly neither in the target-state-pending state nor in the target state.

这篇关于powershell 的 Stop-Service 和 NET-STOP 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆