检查Windows服务是否存在并在PowerShell中删除
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检查Windows服务是否存在并在PowerShell中删除相关的知识,希望对你有一定的参考价值。
我目前正在编写一个安装许多Windows服务的部署脚本。
服务名称是版本化的,因此我想删除以前的Windows服务版本作为新服务安装的一部分。
我怎样才能在PowerShell中做到最好?
您可以使用WMI或其他工具,因为在Powershell 6.0之前没有Remove-Service
cmdlet(See Remove-Service doc))
例如:
$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
$service.delete()
或者使用sc.exe
工具:
sc.exe delete ServiceName
最后,如果您有权访问PowerShell 6.0:
Remove-Service -Name ServiceName
对于单个PC:
if (Get-Service "service_name" -ErrorAction 'SilentlyContinue'){(Get-WmiObject -Class Win32_Service -filter "Name='service_name'").delete()}
else{write-host "No service found."}
PC列表的宏:
$name = "service_name"
$list = get-content list.txt
foreach ($server in $list) {
if (Get-Service "service_name" -computername $server -ErrorAction 'SilentlyContinue'){
(Get-WmiObject -Class Win32_Service -filter "Name='service_name'" -ComputerName $server).delete()}
else{write-host "No service $name found on $server."}
}
对此进行了修改以获取服务器的输入列表,指定主机名并提供一些有用的输出
$name = "<ServiceName>"
$servers = Get-content servers.txt
function Confirm-WindowsServiceExists($name)
{
if (Get-Service -Name $name -Computername $server -ErrorAction Continue)
{
Write-Host "$name Exists on $server"
return $true
}
Write-Host "$name does not exist on $server"
return $false
}
function Remove-WindowsServiceIfItExists($name)
{
$exists = Confirm-WindowsServiceExists $name
if ($exists)
{
Write-host "Removing Service $name from $server"
sc.exe \$server delete $name
}
}
ForEach ($server in $servers) {Remove-WindowsServiceIfItExists($name)}
PowerShell Core(v6 +)现在有一个Remove-Service
cmdlet。
我不知道计划将它反向移植到Windows PowerShell,从v5.1开始它不可用。
例:
# PowerShell *Core* only (v6+)
Remove-Service someservice
请注意,如果服务不存在,则调用将失败,因此只有在当前存在的情况下才将其删除,您可以执行以下操作:
# PowerShell *Core* only (v6+)
$name = 'someservice'
if (Get-Service $name -ErrorAction Ignore) {
Remove-Service $name
}
- 对于v6之前的PowerShell版本,您可以执行以下操作:
Stop-Service 'YourServiceName'; Get-CimInstance -ClassName Win32_Service -Filter "Name='YourServiceName'" | Remove-CimInstance
- 对于v6 +,您可以使用the Remove-Service cmdlet。
请注意,从Windows PowerShell 3.0开始,the cmdlet Get-WmiObject已被Get-CimInstance取代。
Windows Powershell 6将具有Remove-Service cmdlet。截至目前,Github发布了PS v6 beta-9
使用正确的工具进行工作没有什么害处,我发现正在运行(来自Powershell)
sc.exe \server delete "MyService"
最可靠的方法,没有很多依赖项。
如果您只想检查服务是否存在:
if (Get-Service "My Service" -ErrorAction SilentlyContinue)
{
"service exists"
}
我使用了“-ErrorAction SilentlyContinue”解决方案,但后来又遇到了它留下ErrorRecord的问题。所以这是另一种解决方案,只需使用“Get-Service”检查服务是否存在。
# Determines if a Service exists with a name as defined in $ServiceName.
# Returns a boolean $True or $False.
Function ServiceExists([string] $ServiceName) {
[bool] $Return = $False
# If you use just "Get-Service $ServiceName", it will return an error if
# the service didn't exist. Trick Get-Service to return an array of
# Services, but only if the name exactly matches the $ServiceName.
# This way you can test if the array is emply.
if ( Get-Service "$ServiceName*" -Include $ServiceName ) {
$Return = $True
}
Return $Return
}
[bool] $thisServiceExists = ServiceExists "A Service Name"
$thisServiceExists
但ravikanth有最好的解决方案,因为如果服务不存在,Get-WmiObject不会抛出错误。所以我决定使用:
Function ServiceExists([string] $ServiceName) {
[bool] $Return = $False
if ( Get-WmiObject -Class Win32_Service -Filter "Name='$ServiceName'" ) {
$Return = $True
}
Return $Return
}
所以提供更完整的解决方案:
# Deletes a Service with a name as defined in $ServiceName.
# Returns a boolean $True or $False. $True if the Service didn't exist or was
# successfully deleted after execution.
Function DeleteService([string] $ServiceName) {
[bool] $Return = $False
$Service = Get-WmiObject -Class Win32_Service -Filter "Name='$ServiceName'"
if ( $Service ) {
$Service.Delete()
if ( -Not ( ServiceExists $ServiceName ) ) {
$Return = $True
}
} else {
$Return = $True
}
Return $Return
}
更新版本的PS有Remove-WmiObject。小心$ service.delete()的无声失败......
PS D:> $s3=Get-WmiObject -Class Win32_Service -Filter "Name='TSATSvrSvc03'"
PS D:> $s3.delete()
...
ReturnValue : 2
...
PS D:> $?
True
PS D:> $LASTEXITCODE
0
PS D:> $result=$s3.delete()
PS D:> $result.ReturnValue
2
PS D:> Remove-WmiObject -InputObject $s3
Remove-WmiObject : Access denied
At line:1 char:1
+ Remove-WmiObject -InputObject $s3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Remove-WmiObject], ManagementException
+ FullyQualifiedErrorId : RemoveWMIManagementException,Microsoft.PowerShell.Commands.RemoveWmiObject
PS D:>
对于我的情况,我需要运行'作为管理员'
要在Powershell 5.0中删除多个服务,因为此版本中不存在删除服务
运行以下命令
Get-Service -Displayname "*ServiceName*" | ForEach-object{ cmd /c sc delete $_.Name}
结合Dmitri和dcx的答案我做了这个:
function Confirm-WindowsServiceExists($name)
{
if (Get-Service $name -ErrorAction SilentlyContinue)
{
return $true
}
return $false
}
function Remove-WindowsServiceIfItExists($name)
{
$exists = Confirm-WindowsServiceExists $name
if ($exists)
{
sc.exe \server delete $name
}
}
可以使用Where-Object
if ((Get-Service | Where-Object {$_.Name -eq $serviceName}).length -eq 1) {
"Service Exists"
}
以上是关于检查Windows服务是否存在并在PowerShell中删除的主要内容,如果未能解决你的问题,请参考以下文章
用于检查文件是不是存在于 Web 服务器上的 Oracle 过程