Powershell 如何远程重新启动Win32_服务及其所有依赖项
如果我使用Powershell 如何远程重新启动Win32_服务及其所有依赖项,powershell,wmi,get-wmiobject,Powershell,Wmi,Get Wmiobject,如果我使用 $service.stopservice() 方法由于依赖关系,我得到一个错误,我需要做的是重新启动服务及其所有依赖关系 与使用GUI和我选择顶行服务并按下restart类似,首先我会得到一个警告,然后是一个将被重新启动的其他服务的列表,然后在重新启动顶行服务之前停止每个依赖项,然后重新启动依赖项 我需要在powershell中执行此操作 Try { Write-host "Connecting to remote computer" $service = Get-
$service.stopservice()
方法由于依赖关系,我得到一个错误,我需要做的是重新启动服务及其所有依赖关系
与使用GUI和我选择顶行服务并按下restart类似,首先我会得到一个警告,然后是一个将被重新启动的其他服务的列表,然后在重新启动顶行服务之前停止每个依赖项,然后重新启动依赖项
我需要在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会给出一个错误。关于这个用例有一些想法 您可以通过几种方式获得依赖项。。。并根据需要启动,然后再尝试重新启动所需的
# Review Dependent Services
Get-Service -Name Winmgmt -DependentServices
或
解决这些问题可以有不同的方法,下面是关于这个主题的讨论
可能就足够了,因为所有当前运行的依赖项都已重新启动
但是,这是一种不安全的重启方式,因为某些服务可能会这样做
之前已经停止
对于生产机器,建议采用以下方法(以下为
示例是使用WMI服务):
请注意,延迟自动和自动启动类型都显示为
WMI的“自动”
关于这个用例有一些想法 您可以通过几种方式获得依赖项。。。并根据需要启动,然后再尝试重新启动所需的
# Review Dependent Services
Get-Service -Name Winmgmt -DependentServices
或
解决这些问题可以有不同的方法,下面是关于这个主题的讨论
可能就足够了,因为所有当前运行的依赖项都已重新启动
但是,这是一种不安全的重启方式,因为某些服务可能会这样做
之前已经停止
对于生产机器,建议采用以下方法(以下为
示例是使用WMI服务):
请注意,延迟自动和自动启动类型都显示为
WMI的“自动”
不能在方法调用上使用开关。通过
Invoke命令使用Restart-Service
cmdlet。你是说我不能使用写在那里的开关吗?由于该脚本工作正常,因此开关用于变量$StopResponse,该变量从方法中获取返回数据。我是说,您不能将-Force
开关用于方法调用($service.stopservice()-Force
),这是您在原始问题中使用的。啊,好的,对不起,您指的是下面使用的开关,我删除了-force,因为它没有帮助,因为代码块应该显示单个服务的工作方法。通过Invoke命令使用Restart-Service
cmdlet。你是说我不能使用写在那里的开关吗?由于该脚本工作正常,因此开关用于变量$StopResponse,该变量从方法中获取返回数据。我是说,您不能将-Force
开关用于方法调用($service.stopservice()-Force
),这是您在原始问题中使用的。啊,好的,对不起,您指的是下面使用的开关,我删除了-force,因为它没有帮助,因为代码块应该显示单个服务的工作方法。
# For services with just a few dependencies, running
Restart-Service winmgmt -Force -PassThru
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