【问题标题】:How to restart a Win32_Service remotely along with all of its dependencies如何远程重启 Win32_Service 及其所有依赖项
【发布时间】:2019-03-05 15:37:06
【问题描述】:

如果我使用

$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 给出错误。

【问题讨论】:

  • 您不能在方法调用上使用开关。通过Invoke-Command 使用Restart-Service cmdlet。
  • 你是说我不能像上面写的那样使用开关吗?因为该脚本工作正常,所以在变量 $StopResponse 上使用了该开关,该变量从方法中获取返回数据。
  • 我是说您不能将 -Force 开关与您在原始问题中的方法调用 ($service.stopservice() -force) 一起使用。
  • 啊,好吧,抱歉,您的意思是下面使用的开关,我删除了 -force,因为它没有帮助,因为代码块应该显示单个服务的工作方法。

标签: powershell wmi get-wmiobject


【解决方案1】:

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

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

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

Use Windows PowerShell to Display Service Dependencies

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

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

How to restart services with dependencies via PowerShell Scripting (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 的“自动”。

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多