gpt4 book ai didi

powershell - 等待状态服务 powershell

转载 作者:行者123 更新时间:2023-12-03 00:23:09 25 4
gpt4 key购买 nike

我写了一个脚本来停止任何服务器上的服务!但是我得到错误:

      YMethod invocation failed because[System.ServiceProcess.ServiceControllerStatus] does not contain a method   named 'WaitForStatus'.
At C:\Admin\Scripts\JBoss_StartStopp\stoppa_alla_IX2 och IX3.ps1:18 char:7
+ $getservicestatus.WaitForStatus("Stopped")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

当你在一个服务器上退出一个服务时,我想等到这个服务停止后再去下一个服务器上的下一个服务!

   $serverlist = Get-Content “.\server.txt”
$servicename = "JBoss_cambio"

$serverlistIX3 = Get-Content “.\ix3.txt”
$servicenameIX3 = "Laskopia_Cosmic"


foreach ($server in $serverlist) {

$getservicestatus = (Get-Service -Name $servicename -ComputerName $server).status
if ($getservicestatus -eq "Running") {
Set-Service -Name $servicename -ComputerName $server -Status Stopped

$getservicestatusIX3.WaitForStatus("Stopped")

Write-Host "$server $servicename Stoppad!" -ForegroundColor Green
}
else
{
Write-Host "$server $servicename var redan Stopped!" -ForegroundColor Yellow
}
}


foreach ($server in $serverlistIX3) {

$getservicestatusIX3 = (Get-Service -Name $servicenameIX3 -ComputerName $server).status
if ($getservicestatusIX3 -eq "Running") {
Set-Service -Name $servicenameIX3 -ComputerName $server -Status Stopped
$getservicestatusIX3.WaitForStatus("Stopped")
Write-Host "$server $servicenameIX3 Sttopad!" -ForegroundColor Green
}
else
{
Write-Host "$server $servicenameIX3 var redan Stoppad!" -ForegroundColor Yellow
}
}


Write-Host "." -ForegroundColor DarkBlue
Read-Host "Tryck ENTER för att avsluta"

最佳答案

WaitForStatus 方法是 System.ServiceProcess.ServiceController 类的成员,而不是 ServiceControllerStatus。例如:

$s = Get-Service spooler
$s.WaitForStatus("Stopped")

你可以将你的代码修改成这样:

$serviceIX3 = Get-Service -Name $servicenameIX3 -ComputerName $server
if($serviceIX3.status -eq "Running") {
Stop-Service $serviceIX3
$serviceIX3.WaitForStatus("Stopped")
Write-Host "$server $servicenameIX3 Sttopad!" -ForegroundColor Green
}

关于powershell - 等待状态服务 powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49916473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com