VMware Cloud Community
DZ1
Hot Shot
Hot Shot
Jump to solution

Need help getting a warning to show at the beginning

I have this script that wrote to get some ESXi information, I want to write a warning at the very start of the script to let me know if a host is disconnected. I now the basic syntax is:

if (($host).ConnectionState -eq "Disconnected" )
{ Write-Warning "$host.name disconnected!" }

But I want this information to show at the top before my script, here is the script:

$AllReport = @()

Get-VMHost |`

Foreach {

$hosts = $_

$HNetWork = $hosts | Get-VMHostNetwork | Select VMKernelGateway

$DatastoreCount = ($hosts | get-datastore).count

$Cluster = $hosts | Get-Cluster

$NetAdapter = $hosts | Get-VMHostNetworkAdapter | where { $_.PortGroupName -eq "Management Network" } | Select IP

$MgtSubnetMask = $hosts | Get-VMHostNetworkAdapter | where { $_.PortGroupName -eq "Management Network" } | Select SubnetMask

$vMotion = $hosts | Get-VMHostNetworkAdapter | where { $_.PortGroupName -eq "vMotion" }

$vMotionMask = $hosts | Get-VMHostNetworkAdapter | where { $_.PortGroupName -eq "vMotion" } | Select SubnetMask

$NetWork = $hosts | Get-VMHostNetwork

$Notes = $hosts.CustomFields

$Report = "" | Select HostName, "Management IP", MgtMask, vMotion, vMotionMask, "Default GW", "Datastores connected", Cluster, Notes

$Report.Hostname = $Network.HostName

$Report."Management IP" = $NetAdapter.IP

$Report.MgtMask = $MgtSubnetMask.SubnetMask

$Report.vMotion = $vMotion.IP

$Report.vMotionMask = $vMotionMask.SubnetMask

$Report."Default GW" = $HNetwork.VMKernelGateway

$Report."Datastores connected" = $DatastoreCount

$Report.Cluster = $Cluster.Name

$Report.Notes = $hosts.CustomFields

Group Cluster

$AllReport += $Report

Write $Report

}

Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, DZ1-

You should be able to do this by adding something a line or two to the start of your script (below).  And, since this addition already grabs the VMHosts, you don't need to get them again for the body of the script.  So, I updated that, and a few other items on your script.  Something like:

## put hosts into an array, to be able check their ConnectionState first
$arrVMHosts = Get-VMHost
## write out warning about any disconnected hosts
foreach ($VMHost in $arrVMHosts) {if ($VMHost.ConnectionState -eq "Disconnected") {Write-Warning "$($VMHost.Name) disconnected!"}} ## end foreach

$arrVMHosts | Foreach {
   
$VMHost = $_
   
$NetWork = $VMHost | Get-VMHostNetwork
   
$DatastoreCount = @($VMHost | Get-Datastore).count
   
$Cluster = $VMHost | Get-Cluster
   
## array of VMHost NICs for later use
    $arrVMHostNetworkAdapters = Get-VMHostNetworkAdapter -VMHost $VMHost
   
$NetAdapter = $arrVMHostNetworkAdapters | where { $_.PortGroupName -eq "Management Network" } | Select IP
   
$MgtSubnetMask = $arrVMHostNetworkAdapters | where { $_.PortGroupName -eq "Management Network" } | Select SubnetMask
   
$vMotion = $arrVMHostNetworkAdapters | where { $_.PortGroupName -eq "vMotion" }
   
$vMotionMask = $arrVMHostNetworkAdapters | where { $_.PortGroupName -eq "vMotion" } | Select SubnetMask
   
$Notes = $VMHost.CustomFields
   
$Report = "" | Select HostName, "Management IP", MgtMask, vMotion, vMotionMask, "Default GW", "Datastores connected", Cluster, Notes
   
$Report.Hostname = $Network.HostName
   
$Report."Management IP" = $NetAdapter.IP
   
$Report.MgtMask = $MgtSubnetMask.SubnetMask
   
$Report.vMotion = $vMotion.IP
   
$Report.vMotionMask = $vMotionMask.SubnetMask
   
$Report."Default GW" = $Network.VMKernelGateway
   
$Report."Datastores connected" = $DatastoreCount
   
$Report.Cluster = $Cluster.Name
   
$Report.Notes = $VMHost.CustomFields
   
Write $Report
}
## end foreach-object

The other minor changes were a bit of clean-up, and for a bit more efficiency.  Details about the additional changes:

    1. took out $AllReport, since you were adding things to it, but never using it
    2. removed "Group Cluster" line, as that does nothing

    3. renamed variable "$hosts" to "$VMHost", to be a bit more clear (it is only one host at a time, not all of the hosts at once)
    4. stored output of Get-VMHostNetworkAdapter in an array, then used array four times, instead of calling Get-VMHostNetworkAdapter four times
    5. removed $HNetWork, since it was just a filtered version of the VMHostNetwork "$NetWork", which you were getting later; updated $Report."Default GW" to use $NetWork instead of $HNetWork
    6. made item in $DatastoreCount line an array by adding "@" before the parenthesis, so as to handle case where a host has only one (1) datastore (would return nothing before)

How does that do for you™?

View solution in original post

Reply
0 Kudos
2 Replies
mattboren
Expert
Expert
Jump to solution

Hello, DZ1-

You should be able to do this by adding something a line or two to the start of your script (below).  And, since this addition already grabs the VMHosts, you don't need to get them again for the body of the script.  So, I updated that, and a few other items on your script.  Something like:

## put hosts into an array, to be able check their ConnectionState first
$arrVMHosts = Get-VMHost
## write out warning about any disconnected hosts
foreach ($VMHost in $arrVMHosts) {if ($VMHost.ConnectionState -eq "Disconnected") {Write-Warning "$($VMHost.Name) disconnected!"}} ## end foreach

$arrVMHosts | Foreach {
   
$VMHost = $_
   
$NetWork = $VMHost | Get-VMHostNetwork
   
$DatastoreCount = @($VMHost | Get-Datastore).count
   
$Cluster = $VMHost | Get-Cluster
   
## array of VMHost NICs for later use
    $arrVMHostNetworkAdapters = Get-VMHostNetworkAdapter -VMHost $VMHost
   
$NetAdapter = $arrVMHostNetworkAdapters | where { $_.PortGroupName -eq "Management Network" } | Select IP
   
$MgtSubnetMask = $arrVMHostNetworkAdapters | where { $_.PortGroupName -eq "Management Network" } | Select SubnetMask
   
$vMotion = $arrVMHostNetworkAdapters | where { $_.PortGroupName -eq "vMotion" }
   
$vMotionMask = $arrVMHostNetworkAdapters | where { $_.PortGroupName -eq "vMotion" } | Select SubnetMask
   
$Notes = $VMHost.CustomFields
   
$Report = "" | Select HostName, "Management IP", MgtMask, vMotion, vMotionMask, "Default GW", "Datastores connected", Cluster, Notes
   
$Report.Hostname = $Network.HostName
   
$Report."Management IP" = $NetAdapter.IP
   
$Report.MgtMask = $MgtSubnetMask.SubnetMask
   
$Report.vMotion = $vMotion.IP
   
$Report.vMotionMask = $vMotionMask.SubnetMask
   
$Report."Default GW" = $Network.VMKernelGateway
   
$Report."Datastores connected" = $DatastoreCount
   
$Report.Cluster = $Cluster.Name
   
$Report.Notes = $VMHost.CustomFields
   
Write $Report
}
## end foreach-object

The other minor changes were a bit of clean-up, and for a bit more efficiency.  Details about the additional changes:

    1. took out $AllReport, since you were adding things to it, but never using it
    2. removed "Group Cluster" line, as that does nothing

    3. renamed variable "$hosts" to "$VMHost", to be a bit more clear (it is only one host at a time, not all of the hosts at once)
    4. stored output of Get-VMHostNetworkAdapter in an array, then used array four times, instead of calling Get-VMHostNetworkAdapter four times
    5. removed $HNetWork, since it was just a filtered version of the VMHostNetwork "$NetWork", which you were getting later; updated $Report."Default GW" to use $NetWork instead of $HNetWork
    6. made item in $DatastoreCount line an array by adding "@" before the parenthesis, so as to handle case where a host has only one (1) datastore (would return nothing before)

How does that do for you™?

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

Thanks, after seeing what you did, I see where my major error was.  I was trying to write the warning in the main part of the script, and not just write it beforehand.  Thanks a lot, the script looks much cleaner and achieves just what I want.  I actually was going to use the $Allreport += Report, I'm just going to out-string that right into an email, so I will make the adjustments.  Thanks again Smiley Happy

Reply
0 Kudos