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:
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
}
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:
removed "Group Cluster" line, as that does nothing
How does that do for you™?
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:
removed "Group Cluster" line, as that does nothing
How does that do for you™?
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