1

如果我使用

$service.stopservice()

方法由于依赖关系而出现错误,我需要做的是重新启动服务及其所有依赖关系。

类似于如果使用 GUI 并且我选择了最上面的服务并按下重新启动,首先我会收到一个警告,然后是将重新启动的其他服务的列表,然后在重新启动最上面的服务之前停止每个依赖项,然后重新启动依赖项。

我需要在 powershell 中执行此操作

Try {
    Write-host "Connecting to remote computer"
    $service = Get-WmiObject -Class Win32_Service -ComputerName $ip -Credential $cred -Filter "Name='$servname'"
    if ($service.Status -eq 'Running'){$ServiceStarted = $true}

    if($ServiceStarted -eq $true) {


    $StopResponse = $service.stopservice()  
    $StopReturnCode = Switch ($StopResponse.ReturnValue) {
    0{ "The request was accepted" }
    5{ "The service is already stopped" }
    10{ "The service failed to stop - run the script again" }
    default{ "Something unexpected happened" }
}
}
     Write-Host $StopReturnCode
} catch {
Write-Host "script noped out bro" :fore RED
}

以上对单个服务有效(尽管速度很慢)。

我试过使用 -Force 但这不起作用,并且 -Force -Confirm 给出错误。

4

1 回答 1

0

关于这个用例有一些想法。

您可以通过几种方式获取依赖项......并根据需要启动,然后再尝试重新启动您所追求的。

# Review Dependent Services
Get-Service -Name Winmgmt -DependentServices

或者

使用 Windows PowerShell 显示服务依赖项

Get-Service -CN $env:COMPUTERNAME | 
    Where-Object { $_.status -eq ‘running’} | 
    ForEach-Object { 
    write-host -ForegroundColor 9 "Service name $($_.name)" 
    if ($_.DependentServices) 
    {
        write-host -ForegroundColor 3 "tServices that depend on $($_.name)"
        foreach ($s in $_.DependentServices) 
        { "tt" + $s.name } 
    } #end if DependentServices 
    if ($_.RequiredServices) 
    {
        Write-host -ForegroundColor 10 "tServices required by $($_.name)"
        foreach ($r in $_.RequiredServices) 
        { "tt" + $r.name } 
    } #end if DependentServices 
} #end foreach-object

解决它们可以有不同的方法,这里是关于该主题的讨论。

如何通过 PowerShell 脚本重新启动具有依赖关系的服务 (128574)

# For services with just a few dependencies, running 
Restart-Service winmgmt -Force -PassThru

可能就足够了,因为所有当前正在运行的依赖项都已重新启动。

但是,这是一种不安全的重启方式,因为某些服务可能先前已停止。

对于生产机器,推荐使用以下方法(示例使用 WMI 服务):

cls
Write-Host "Restarting Service with Dependencies`r`n" -f Green

# 1. Get wmi dependencies
$wmidependents = (get-service winmgmt).dependentservices  

if desired to get only the running dependent services, pipe | where {$_.status -eq "running"}


# 2. Get all necessary information about dependent services
$wmidependentservices = Get-WmiObject Win32_Service | 
Select-object name,state,startmode | 
Where-Object {$wmidependents.name -contains $_.name}

# 3. Stop wmi dependencies
Write-Host "`r`nStopping Services`r`n-----------------`r`n" -f Yellow


ForEach ($service in $wmidependentservices)
{
Write-Host "`r`nAnalyzing $($service.name)" -f Yellow

    if($service.startmode -eq "auto" -or $service.status -eq "Running")
    {
        Write-Host "Stopping $($service.name)"
        stop-service $service.name
        #you can add more logic in the block
    } 
    else
    {
        "$($service.name) is $($service.state) with the startmode: $($service.startmode)"
    }
}

#equivalent to stop-service $wmidependents.name

# 4. Stop the WMI service
Stop-Service winmgmt -force
Write-Host "`r`nStarting Services`r`n -----------------`r`n" -f Yellow

# 5. start dependencies

ForEach ($service in $wmidependentservices)
{
    Write-Host "`r`nAnalyzing $($service.name)" -f Yellow

    if($service.startmode -eq "auto")
    {
        "Starting $($service.name)"
        start-service $service.name
        #you can add more logic in the block
    } 
    else
    {
        "$($service.name) is $($service.state) with the startmode: $($service.startmode)"
    }
}

#equivalent to start-service $wmidependents.name

# 6. start WMI
Start-Service winmgmt

请注意,WMI 将延迟自动和自动启动类型都显示为“自动”。

于 2019-03-06T04:30:55.037 回答