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

PowerShell different services to run in particular sequence (Remotely)

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

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

工作流目标:

  1. 停止服务 A
  2. 重新启动服务 B
  3. 启动服务 A

代码:

#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:\PowerShell\ScriptExample\ScriptExampleFix.ps1:10 char:9
$service.WaitForStatus($status, '00:00:30')

+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TimeoutException

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

Start-Service : A parameter cannot be found that matches parameter name 'force'. At C:\PowerShell\ScriptExample\ScriptExampleFix.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.
}