Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何确保已使用PowerShell停止或启动服务?_C#_.net_Powershell_Azure Devops - Fatal编程技术网

C# 如何确保已使用PowerShell停止或启动服务?

C# 如何确保已使用PowerShell停止或启动服务?,c#,.net,powershell,azure-devops,C#,.net,Powershell,Azure Devops,我正在VSTS/Azure DevOps管道中运行一项任务,以停止和卸载窗口服务列表。我在这里所做的是运行下面的代码&使用sleep方法来确保上述方法已经完成 Function DeleteService([string] $ServiceName) { TRY{ $Service = Get-WmiObject -Class Win32_Service -Filter "Name='$ServiceName'" if ($Service -ne

我正在
VSTS/Azure DevOps
管道中运行一项任务,以停止和卸载窗口服务列表。我在这里所做的是运行下面的代码&使用sleep方法来确保上述方法已经完成

Function DeleteService([string] $ServiceName) 
{
    TRY{

        $Service = Get-WmiObject -Class Win32_Service -Filter "Name='$ServiceName'"  

        if ($Service -ne $null) 
        {
            Write-Output "Stopping window service - '$ServiceName'"         
            $Service.StopService()     
            # Adding a sleep for ten seconds to let the process stop the service completely
            Start-Sleep -m 10000 
            Write-Output "Stopping Window service - '$ServiceName' completed"   


            Write-Output "Uninstalling window service - '$ServiceName'"         
            $Service.Delete()   
            # Adding a sleep for ten seconds to let the process stop the service completely
            Start-Sleep -m 10000
            Write-Output "Uninstalling window service - '$ServiceName' completed"   

        } 
        else 
        {
            Write-Output "Window service - '$ServiceName' does not exist. Uninstallation Complete"
        }
    }
    CATCH
    {
        $ErrorMessage = $_.Exception.Message    
        Write-Error " ********************** Error in uninstalling window service : $ServiceName with exception $ErrorMessage ********************** "
    }
}
PowerShell中是否有更好的方法来确认服务已停止,现在我可以继续。这样我就不必在代码中添加这样的补丁


因为,正如我从中所研究的,这些命令会将消息发送到
Windows服务控制器
。他们没有完成任务。因此,我怀疑如何编写能够与正确的实时执行同步运行的代码。

而不是使用
Get WmiObject
您可以使用
Get Service
并检查服务的状态:

$service = Get-Service -Name 'VSS'
Write-Host $service.Status
# Stopped/Running
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
    Remove-Service $ServiceName -Verbose
} 
else {
    Write-Output "Window service - '$ServiceName' does not exist. Uninstallation Complete"
}
因此,如果要确保服务停止,然后继续删除,可以使用while循环包装状态检查

while ($service.Status -ne 'Running')
{
  ....
}

如果您使用的是PS v6,您可以使用
删除服务
,因为这将停止并删除服务:

$service = Get-Service -Name 'VSS'
Write-Host $service.Status
# Stopped/Running
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
    Remove-Service $ServiceName -Verbose
} 
else {
    Write-Output "Window service - '$ServiceName' does not exist. Uninstallation Complete"
}
如果您使用的是较低版本,我会使用
停止服务
获取CimInstance
(而不是获取WmiObject):


获取服务、停止服务和删除服务()[编辑-PSv6仅用于删除服务,因此可能没有帮助]这些方法在移动到下一行之前完成其工作吗?它们是命令而不是方法,是的,它们是同步的。请注意,请编辑我关于PSv6的帖子-不确定管道支持/使用的版本。执行这一行后-“删除服务$ServiceName-Verbose”。我可以在下一行确定服务已从系统中删除吗?是的,如果命令完成,服务将被删除,如果无法删除服务,您将收到终止错误。