检查Windows服务是否存在并在PowerShell中删除

时间:2011-02-11 09:39:14

标签: powershell windows-services

我目前正在编写一个安装许多Windows服务的部署脚本。

服务名称是版本化的,因此我想删除以前的Windows服务版本作为新服务安装的一部分。

我怎样才能在PowerShell中做到最好?

14 个答案:

答案 0 :(得分:198)

您可以使用WMI或其他工具,因为在Powershell 6.0(See Remove-Service doc)

之前没有Remove-Service cmdlet

例如:

$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
$service.delete()

或使用sc.exe工具:

sc.exe delete ServiceName

最后,如果您有权访问PowerShell 6.0:

Remove-Service -Name ServiceName

答案 1 :(得分:119)

使用正确的工具进行工作没有坏处,我发现正在运行(来自Powershell)

sc.exe \\server delete "MyService" 

最可靠的方法,没有很多依赖。

答案 2 :(得分:78)

如果您只想查看服务存在:

if (Get-Service "My Service" -ErrorAction SilentlyContinue)
{
    "service exists"
}

答案 3 :(得分:20)

我使用了“-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
}

答案 4 :(得分:12)

PS的更新版本具有Remove-WmiObject。小心$ service.delete()的静默失败...

%UserProfile%\.theanorc

对于我的情况,我需要运行'作为管理员'

答案 5 :(得分:6)

要在Powershell 5.0中删除多个服务,因为此版本中不存在删除服务

运行以下命令

  android:configChanges="orientation|screenSize"

答案 6 :(得分:4)

结合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
    }       
}

答案 7 :(得分:4)

可以使用Where-Object

if ((Get-Service | Where-Object {$_.Name -eq $serviceName}).length -eq 1) { "Service Exists" }

答案 8 :(得分:3)

要检查名为MySuperServiceVersion1的Windows服务是否存在,即使您可能不确定其确切名称,也可以使用通配符,使用子字符串,如下所示:

 if (Get-Service -Name "*SuperService*" -ErrorAction SilentlyContinue)
{
    # do something
}

答案 9 :(得分:3)

对于单个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."}

}

答案 10 :(得分:2)

改编它以获取服务器的输入列表,指定主机名并提供一些有用的输出

            $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)}

答案 11 :(得分:2)

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
}

答案 12 :(得分:2)

  • 对于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取代。

答案 13 :(得分:1)

Windows Powershell 6将具有删除服务 cmdlet。 截至目前,Github的发布显示了PS v6 beta-9

来源: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-service?view=powershell-6