PowerShell不同的服务以特定的顺序运行(远程)



第一篇!我为格式问题提前道歉。我刚刚开始熟悉PowerShell,我想先停止一个服务,重新启动另一个服务,然后启动初始服务。在转到下一个下一个服务之前,我想确保该服务在继续之前已经停止。

我正在使用这里提到的这个函数,并试图为我的代码定制它。

工作流的目标:

  1. 停止服务A
  2. 重启服务B
  3. 启动服务A
  4. 代码:

#Stops Service A and validates its in "Stopped" status
Get-Service 'ServiceNameA' -ComputerName 'ExampleServerA' | Stop-Service -force -PassThru
function WaitUntilServices1($searchString, $status)
{
# Get all services where DisplayName matches $searchString and loop through each of them.
foreach($service in (Get-Service -DisplayName $searchString))
{
# Wait for the service to reach the $status or a maximum of 30 seconds
$service.WaitForStatus($status, '00:00:30')
}
}
WaitUntilServices1 "ServiceDisplayNameA" "Stopped"
#Restarts Service B and validates its in "Running" status
Get-Service 'ServiceNameB' -ComputerName 'ExampleServerB' | Restart-Service -force -PassThru
function WaitUntilServices2($searchString, $status)
{
# Get all services where DisplayName matches $searchString and loop through each of them.
foreach($service in (Get-Service -DisplayName $searchString))
{
# Wait for the service to reach the $status or a maximum of 30 seconds
$service.WaitForStatus($status, '00:00:30')
}
}
WaitUntilServices2 "ServiceDisplayNameB" "Running"
#Start Service A and validates its in "Running" status
Get-Service 'ServiceA' -ComputerName 'ExampleServerA' | Start-Service -force -PassThru
Read-Host -Prompt "Press Enter to exit" 

我上面的代码给了我以下两个函数的错误:

Exception calling "WaitForStatus" with "2" argument(s): "Time out has expired and the operation has not been completed." At C:PowerShellScriptExampleScriptExampleFix.ps1:10 char:9
$service.WaitForStatus($status, '00:00:30')
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TimeoutException

然后在最后一部分启动服务时,我又得到了一个错误:

Start-Service : A parameter cannot be found that matches parameter name 'force'. At C:PowerShellScriptExampleScriptExampleFix.ps1:32 char:85
+ ... erName 'ServerNameExample' | Start-Service -force -PassTh ...
+                                                            ~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Start-Service], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.StartServiceCommand

任何帮助都会非常感激:)

在第一个语句中:

#Stops Service A and validates its in "Stopped" status
Get-Service 'ServiceNameA' -ComputerName 'ExampleServerA' | Stop-Service -force -PassThru

要求PowerShell在远程计算机上停止ServiceNameA

然后调用WaitUntilServices1,它试图等待本地计算机上同名的服务-显然不会很快停止,因为您请求停止上另一台计算机上的服务。

改变函数定义接受-ComputerName参数,通过Get-Service:

function Wait-ServiceStatus {
param(
[string]$Name,
[string]$ComputerName = $('.'),
[System.ServiceProcess.ServiceControllerStatus]$Status
)
foreach($service in Get-Service -Name $Name -ComputerName $ComputerName){
# If any call to WaitForStatus times out and throws, return $false
try { $service.WaitForStatus($Status, '00:00:30') } catch { return $false }
}
# No errors thrown while waiting, all is good, return $true
return $true
}

现在我们可以:

# request the remote SCM stop the service
Get-Service 'ServiceNameA' -ComputerName 'ExampleServerA' | Stop-Service -Force
$success = Wait-ServiceStatus -Name 'ServiceNameA' -ComputerName 'ExampleServerA' -Status Stopped
if(-not $success){
# output an error
Write-Error "failed to complete restart cycle, 'ServiceNameA' on 'ExampleServerA' failed to stop in a timely manner"
# return from this script/function for good measure
return
}
# ... if we've reached this point the wait must have been successful, continue with the restart cycle.
Get-Service 'ServiceNameB' -ComputerName 'ExampleServerB' | Restart-Service -force -PassThru
$success = Wait-ServiceStatus -Name 'ServiceNameB' -ComputerName 'ExampleServerB' -Status Running
if(-not $success){
# ... etc.
}