VMware Cloud Community
1NetPro
Contributor
Contributor
Jump to solution

Limit script to run only against hosts in "Connected" and "Maintenance Mode" states.

Greetings Community!

Hoping someone with the expertise would help me with the following:

I work in a medium-to-large environment of ESXi hosts managed from over 25 vCenters.

Discovered major time drifts on many hosts due to a number of reasons and corrected them.

To check time sync on a regular basis, did the usual Google thing and found a nice script (posted below) from LucD --Thanks Man--

It works great except that due to various reasons, we have a large number of hosts waiting to be decommissioned that are at the moment either in a Not Responding or Disconnected state.

These, of course, cause the "Unable to communicate with the remote host, since it is disconnected." error and I was wondering how I can make the script run ONLY against hosts "Connected" and in "Maintenance mode". (or ignore hosts NOT in these two connection states)

I could include here a list of things I have been trying for last week or two (without success) but that would make the post too long and risk not being read due to excessive length.

My PoweerCLI skills are not up to par yet hence my request for help.

Thanks a bunch for any and all suggestions!

========================================================================================================================================================

$allowedDifferenceSeconds = 1

get-view -ViewType HostSystem -Property Name, ConfigManager.DateTimeSystem | %{   

    #get host datetime system

    $dts = get-view $_.ConfigManager.DateTimeSystem

   

    #get host time

    $t = $dts.QueryDateTime()

   

    #calculate time difference in seconds

    $s = ( $t - [DateTime]::UtcNow).TotalSeconds

   

    #check if time difference is too much

    if([math]::abs($s) -gt $allowedDifferenceSeconds){

        #print host and time difference in seconds

        $row = "" | select HostName, Seconds

        $row.HostName = $_.Name

        $row.Seconds = $s

        $row

    }

    else{

        Write-Host "Time on" $_.Name "within allowed range"

    }

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The Filter option of the Get-View cmdlet would allow you to do that.
PS: I used splatting to avoid a long line with Get-View

Something like this

$allowedDifferenceSeconds = 1

$sView = @{

   ViewType = 'HostSystem'

   Property = 'Name', 'ConfigManager.DateTimeSystem', 'Runtime.ConnectionState'

   Filter = @{

   'Runtime.ConnectionState' = 'connected|maintenance'

   }

}

Get-View @sView | % { 

   #get host datetime system

   $dts = Get-View $_.ConfigManager.DateTimeSystem

 

   #get host time

   $t = $dts.QueryDateTime()

 

   #calculate time difference in seconds

   $s = ( $t - [DateTime]::UtcNow).TotalSeconds

 

   #check if time difference is too much

   if ([math]::abs($s) -gt $allowedDifferenceSeconds)

   {

   #print host and time difference in seconds

   $row = "" | select HostName, Seconds

   $row.HostName = $_.Name

   $row.Seconds = $s

   $row

   }

   else

   {

   Write-Host "Time on" $_.Name "within allowed range"

   }

}


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The Filter option of the Get-View cmdlet would allow you to do that.
PS: I used splatting to avoid a long line with Get-View

Something like this

$allowedDifferenceSeconds = 1

$sView = @{

   ViewType = 'HostSystem'

   Property = 'Name', 'ConfigManager.DateTimeSystem', 'Runtime.ConnectionState'

   Filter = @{

   'Runtime.ConnectionState' = 'connected|maintenance'

   }

}

Get-View @sView | % { 

   #get host datetime system

   $dts = Get-View $_.ConfigManager.DateTimeSystem

 

   #get host time

   $t = $dts.QueryDateTime()

 

   #calculate time difference in seconds

   $s = ( $t - [DateTime]::UtcNow).TotalSeconds

 

   #check if time difference is too much

   if ([math]::abs($s) -gt $allowedDifferenceSeconds)

   {

   #print host and time difference in seconds

   $row = "" | select HostName, Seconds

   $row.HostName = $_.Name

   $row.Seconds = $s

   $row

   }

   else

   {

   Write-Host "Time on" $_.Name "within allowed range"

   }

}


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

0 Kudos
1NetPro
Contributor
Contributor
Jump to solution

Happy to report it works as requested.

Truly amazing!

Thanks a million, LucD!!!

0 Kudos