030*_*030 7 windows services powershell
介绍
根据此文档,可以通过执行以下命令来检查 Windows 上哪些服务已停止:
Get-Service | Where-Object {$_.status -eq "stopped"}
在 PowerShell 中。
题
需要在 PowerShell 中发出哪个命令才能检查某个服务(例如 tomcat8)是否存在?
Tim*_*ill 16
您可以使用该-Name属性指定服务名称。默认情况下,如果它没有看到匹配的服务,它将给出一个错误。使用-ErrorAction SilentlyContinue你可以得到一个空变量。
$service = Get-Service -Name W32Time -ErrorAction SilentlyContinue
Run Code Online (Sandbox Code Playgroud)
一旦你有了它,你就可以看到长度是否大于 0。
if ($service.Length -gt 0) {
# Do cool stuff
}
Run Code Online (Sandbox Code Playgroud)
Kel*_*art 11
这是一个比接受的答案稍微干净的解决方案:
$service = Get-Service -Name MSSQLSERVER -ErrorAction SilentlyContinue
if($service -eq $null)
{
# Service does not exist
} else {
# Service does exist
}
Run Code Online (Sandbox Code Playgroud)
在我看来,检查 NULL 在语义上比检查 length 属性更有意义。
这已经过测试并且可以在此版本的 PowerShell 中运行:
Major Minor Patch PreReleaseLabel BuildLabel
----- ----- ----- --------------- ----------
7 0 3
Run Code Online (Sandbox Code Playgroud)
我无法与其他版本的 PowerShell 交谈,但如果您有问题,请发表评论。
| 归档时间: |
|
| 查看次数: |
27858 次 |
| 最近记录: |