VMware Cloud Community
Myersc
Contributor
Contributor
Jump to solution

Get-VMHostService

I know this is going to be an easy answer but its Friday afternoon and my brain is toast...

I am using the Get-VMHostService to start, stop, ect,  What I don't like about the code it that it doesn't check to see if the services are running first and then apply the stop action.  The number of errors becasue of already stopped services makes this script not so friendly.  Any help is appreciated.

Craig

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Just add a condition in the Where-clause

function StopServices {
    $tgtServices = "TSM-SSH","TSM"
    $VMHost = Get-VMHost
    foreach ($VMHost in $VMHost) {
       Get-VMHostService -VMHost $VMHost | where {$tgtServices -contains $_.Key-and $_.Running} | %{
             Stop-VMHostService -HostService $_
             Set-VMHostService -HostService $_ -Policy off
        }
  }
}
StopServices

The Running property will be $true when the service is running

You can use the -contains operator to test for any of the servicenames in the array $tgtServices


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

Just add a condition in the Where-clause

function StopServices {
    $tgtServices = "TSM-SSH","TSM"
    $VMHost = Get-VMHost
    foreach ($VMHost in $VMHost) {
       Get-VMHostService -VMHost $VMHost | where {$tgtServices -contains $_.Key-and $_.Running} | %{
             Stop-VMHostService -HostService $_
             Set-VMHostService -HostService $_ -Policy off
        }
  }
}
StopServices

The Running property will be $true when the service is running

You can use the -contains operator to test for any of the servicenames in the array $tgtServices


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos