VMware Cloud Community
VMSavvy
Enthusiast
Enthusiast
Jump to solution

using if or where statement??

Hi LucD and other scripters,

I have a daily check script which is running very well but giving me a big chunk of info in the output.. To shorten this I've decided to pull out only the ones which requires some action..

This script pulls out the list of esx hosts, cluster name, powerstate, status etc..

I want to pull out only the ones which are powered off, disconnected, in-maintenance, NTPD not running etc..

Can someone help to tweak this script with if or where statements to get a report I wanted..?

here is the script..

$Report = @()

Get-VMHost | ForEach-Object {
$VMHosts = Get-View $_.ID

      $hosts = "" | Select-Object -Property "ESX Name", "Cluster Name", PowerState, Status, MaintananceMode, "NTPD Status"
      $hosts."ESX Name" = $_.Name

      $hosts."Cluster Name" = (Get-Cluster -VMHost $_).name
      $hosts.PowerState = $VMHosts.runtime.PowerState
      $hosts.Status = $VMHosts.Runtime.ConnectionState
      $hosts.MaintananceMode = $VMHosts.Runtime.InMaintenanceMode
  if ((Get-VmHostService -VMHost $_ | Where-Object {$_.key -eq "ntpd"}).Running -eq "True") {
    $hosts."ntpd status" = "Running"
  }
  else {
    $hosts."ntpd status" = "Not Running"
  }
  $Report += $hosts
  }
  $Report | ConvertTo-Html -title "ESX Server Statistics" -body "<H2>ESX Server Statistics</H2>" | Out-File -Append $filelocation

Appreciate your efforts!!

VMSavvy Smiley Happy

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The simplest solution (but probably not the only one) would be to split up the one-liner.

Something like this

$selected = $Report | where {$VMHosts.Runtime.PowerState -eq "PoweredOff"}

if($selected) {$selected | ConvertTo-Html -title "Powered Off Hosts" -body  "<H2>Powered Off Hosts</H2>" | Out-File -Append  $filelocation}

else{ConvertTo-Html -title "Powered Off Hosts" -body  "<H2>All hosts running</H2>" | Out-File -Append  $filelocation}

The $selected array will be empty ($null) when all hosts are powered on.

In the If statement, if there is something in $selected it will evaluate to $true, otherwise the Else block is executed.

I hope that helps.


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

View solution in original post

0 Kudos
6 Replies
andreasbrunner
Contributor
Contributor
Jump to solution

Hi VMSavvy,

apart from the idea to solve this with a script. I think that it makes more sense to work in such a case with alarms and to send you e-mails for such issues. You need to be informed immediately rather then running a (scheduled) script.

regards

Andreas

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The easiest is to use a Where-clause once you have compiled the data in the $report array.

Just change the last line where you output the results.

You can check for a single condition like this

$Report | where {$_.PowerState -eq "PoweredOff"} |`

ConvertTo-Html -title "ESX Server Statistics" -body  "<H2>ESX Server Statistics</H2>" | Out-File -Append  $filelocation

or for combined conditions like this

$Report | where {$_.PowerState -eq "PoweredOff" -or $_.Status -eq "Disconnected"} |`

ConvertTo-Html -title "ESX Server Statistics" -body   "<H2>ESX Server Statistics</H2>" | Out-File -Append   $filelocation


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

0 Kudos
VMSavvy
Enthusiast
Enthusiast
Jump to solution

Andreas,

Thats a good approach too.. But this is just to get a glimpse on the infrastructure in the morning to see if something has gone wrong.. We have monitoring and email alerts but as the infrastructure is way big with 4 vcenters and 220+hosts.. aah.. too many emails that we might overlook.. hence this script work..

LucD.. I'm running your code now.. Awaiting result.. Its your code..I'm sure it works.. Smiley Happy

Thanks..VMSavvy

0 Kudos
VMSavvy
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

That worked as expected.. One small addition.. If no discrepancies found.. can we print "All running" something like that.. do we have a print statement here?

for ex: It shows the list when there any powered off VMs.. if all are powered on and running.. "All hosts running" in place of the output?

Here is the code type I used...

$Report | where {$VMHosts.Runtime.PowerState -eq "PoweredOff"} | ConvertTo-Html -title "Powered Off Hosts" -body  "<H2>Powered Off Hosts</H2>" | Out-File -Append  $filelocation

we can't use "else" statement with "where" right..so kind of stuck there..

Thanks,

VMSavvy..

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The simplest solution (but probably not the only one) would be to split up the one-liner.

Something like this

$selected = $Report | where {$VMHosts.Runtime.PowerState -eq "PoweredOff"}

if($selected) {$selected | ConvertTo-Html -title "Powered Off Hosts" -body  "<H2>Powered Off Hosts</H2>" | Out-File -Append  $filelocation}

else{ConvertTo-Html -title "Powered Off Hosts" -body  "<H2>All hosts running</H2>" | Out-File -Append  $filelocation}

The $selected array will be empty ($null) when all hosts are powered on.

In the If statement, if there is something in $selected it will evaluate to $true, otherwise the Else block is executed.

I hope that helps.


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

0 Kudos
VMSavvy
Enthusiast
Enthusiast
Jump to solution

That worked LucD..

Thanks bunch..

VMSavvy Smiley Happy

0 Kudos