Perfect, it works. Thank you
Original Message:
Sent: Aug 02, 2024 09:38 AM
From: DennisK
Subject: Get-VMHostHardware |
Sure, here it is. Any improvements to make the script better, be sure to let me know.
If (-not (Get-Module -Name 'VMware.PowerCLI')) {
Import-Module VMware.PowerCLI
}
$Report = New-Object System.Collections.ArrayList
$DateTime = $((Get-Date).ToString('yyyy-MM-dd_hh-mm-ss'))
$OutputPath = "$env:USERPROFILE\Documents\Reports"
If ( -Not (Test-Path -Path $OutputPath)) {
New-Item -ItemType directory -Path $OutputPath
}
$Outputfile = "All-ESXi-hosts-$DateTime"
Function MyLogger {
Param(
[Parameter(Mandatory = $True)] [String] $Message,
[Parameter(Mandatory = $False)] [Boolean] $Log = $False
)
$TimeStamp = Get-Date -Format "dd-MMM-yyyy HH:mm:ss"
Write-Host -NoNewline -ForegroundColor White "[$timestamp]"
Write-Host -ForegroundColor Green " $message"
$logMessage = "[$timeStamp] $message"
If ($Log -eq $True) {
$logMessage | Out-File -Append -LiteralPath $LogFile
}
}
Function MyLoggerError {
Param(
[Parameter(Mandatory = $True)] [String] $Message,
[Parameter(Mandatory = $False)] [Boolean] $Log = $False
)
$TimeStamp = Get-Date -Format "dd-MMM-yyyy HH:mm:ss"
Write-Host -NoNewline -ForegroundColor White "[$timestamp]"
Write-Host -ForegroundColor Red -BackgroundColor Yellow " $message"
$logMessage = "[$timeStamp] $message"
If ($Log -eq $True) {
$logMessage | Out-File -Append -LiteralPath $LogFile
}
}
Clear-Host
$Credentials = Get-Credential -Message "Please enter the username and password to use to connect to the vCenter(s)"
$ListOfvCenters = (
"<FQDN/IP address of vCenter #1",
"vcenter01.yourdomain.local",
"10.10.10.50"
)
Connect-VIServer $ListOfvCenters -Credential $Credentials -ErrorAction Continue
$ListOfVMHosts = Get-VMHost | Sort-Object Name
$Count = $ListOfVMHosts.Count
$Counter = 1
Foreach ($VMHost in $ListOfVMHosts) {
$TestVMHostConnection = $null
$TestVMHostConnection = Get-VMHost -Name $VMHost
If ($TestVMHostConnection) {
MyLogger -Message "Processing host $VMHost [$Counter/$Count]"
$HostNetwork = $VMHost | Get-VMHostNetwork
$HostNetworkvMotionAdapter = $VMHost | Get-VMHostNetworkAdapter -VMKernel | Where-Object { $_.VMotionEnabled -eq $true }
$HostNetworkManagementTrafficAdapter = $VMHost | Get-VMHostNetworkAdapter -VMKernel | Where-Object { $_.ManagementTrafficEnabled -eq $true }
$HostNetworkvSANTrafficAdapter = $VMHost | Get-VMHostNetworkAdapter -VMKernel | Where-Object { $_.VsanTrafficEnabled -eq $true }
$MemoryTotalGB = [math]::Round($VMHost.MemoryTotalGB)
$Object = [PSCustomObject]@{
SerialNumber = ($VMHost.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo | Where-Object { $_.IdentifierType.Key -eq "ServiceTag" }).IdentifierValue
vCenter = $VMHost.Uid.Substring($VMHost.Uid.IndexOf('@') + 1).Split(":")[0]
DataCenter = ($VMHost | Get-Datacenter)
Cluster = $VMHost.Parent
HostName = $VMHost.Name
Manufacturer = $VMHost.Manufacturer
Model = $VMHost.Model
Bios = $VMHost.ExtensionData.Hardware.BiosInfo.BiosVersion
Processor = $VMHost.ProcessorType
CPU = $VMHost.ExtensionData.Hardware.CpuInfo.NumCpuPackages
CPUCores = $VMHost.ExtensionData.Hardware.CpuInfo.NumCpuCores
MemoryGB = $MemoryTotalGB
ESXVersion = $VMHost.ExtensionData.Summary.Config.Product.FullName
ESXLicenseKey = $VMHost.LicenseKey
DNS1 = $HostNetwork.DnsAddress[0]
DNS2 = $HostNetwork.DnsAddress[1]
Gateway = $HostNetwork.VMKernelGateway
ManagementDevice = $HostNetworkManagementTrafficAdapter.name
ManagementIP = $HostNetworkManagementTrafficAdapter.ip
ManagementMTU = $HostNetworkManagementTrafficAdapter.mtu
vMotionDevice = $HostNetworkvMotionAdapter.name
vMotionIP = $HostNetworkvMotionAdapter.ip
vMotionMTU = $HostNetworkvMotionAdapter.mtu
vSANDevice = $HostNetworkvSANTrafficAdapter.name
vSANIP = $HostNetworkvSANTrafficAdapter.ip
vSANMTU = $HostNetworkvSANTrafficAdapter.mtu
}
$Report.add($Object) | Out-Null
} Else {
MyLoggerError -Message "There was an error processing host $VMHost.Name [$Counter/$Count]"
}
$Counter++
}
$Report | Export-Csv "$Outputfile.csv" -NoTypeInformation -UseCulture
$DefaultVIServers | Disconnect-VIServer -Confirm:$false
Original Message:
Sent: Aug 02, 2024 08:30 AM
From: iDIGIT
Subject: Get-VMHostHardware |
Hi Dennis K,
Thank you. Can you please post your complete final script? I am having an issue in pulling array report.
Best,
S
Original Message:
Sent: Aug 02, 2024 02:42 AM
From: DennisK
Subject: Get-VMHostHardware |
In my original script I define it like this:
$Credentials = Get-Credential -Message "Please enter the username and password to use to connect to the vCenter(s)"
$ListOfvCenters = (
"<FQDN/IP address of vCenter #1",
"vcenter01.yourdomain.local",
"10.10.10.50"
)
Connect-VIServer $ListOfvCenters -Credential $Credentials -ErrorAction Continue
$ListOfVMHosts = Get-VMHost | Sort-Object Name
$Count = $ListOfVMHosts.Count
$Counter = 1
Original Message:
Sent: Aug 01, 2024 12:48 PM
From: iDIGIT
Subject: Get-VMHostHardware |
How can I define $ListOfVMHosts? I tried something like this but getting output only for one host.
# Initialize the counter and report array
$Counter = 1
$Report = @()
$hostListFilePath = "path to esxihosts_hw.txt"
$hostNames = Get-Content -Path $hostListFilePath
$ListOfVMHosts = Get-VMHost $hostNames
$Count = $ListOfVMHosts.Count
Foreach ($VMHost in $ListOfVMHosts) {
Write-Host "Processing host $($VMHost.Name) [$Counter/$Count]"
.....
......
$Report += $Object
$Counter++
}
# Output the report or save it to a file
$Report | Format-Table -AutoSize
Original Message:
Sent: Mar 11, 2023 10:35 PM
From: LucD
Subject: Get-VMHostHardware |
You can do
Foreach ($VMHost in $ListOfVMHosts) { Write-Output -Message "Processing host $VMHost.Name [$Counter/$Count]" $HostNetwork = $VMHost | Get-VMHostNetwork $HostNetworkvMotionAdapter = $VMHost | Get-VMHostNetworkAdapter -VMKernel | Where-Object { $_.VMotionEnabled -eq $true } $HostNetworkManagementTrafficAdapter = $VMHost | Get-VMHostNetworkAdapter -VMKernel | Where-Object { $_.ManagementTrafficEnabled -eq $true } $HostNetworkvSANTrafficAdapter = $VMHost | Get-VMHostNetworkAdapter -VMKernel | Where-Object { $_.VsanTrafficEnabled -eq $true } $MemoryTotalGB = [math]::Round($VMHost.MemoryTotalGB) $Object = [PSCustomObject]@{ SerialNumber = $VMHost.ExtensionData.Hardware.SystemInfo.SerialNumber vCenter = $VMHost.Uid.Substring($VMHost.Uid.IndexOf('@') + 1).Split(":")[0] DataCenter = ($VMHost | Get-Datacenter) Cluster = $VMHost.Parent HostName = $VMHost.Name Manufacturer = $VMHost.Manufacturer Model = $VMHost.Model Bios = $VMHost.ExtensionData.Hardware.BiosInfo.BiosVersion Processor = $VMHost.ProcessorType CPU = $VMHost.ExtensionData.Hardware.CpuInfo.NumCpuPackages CPUCores = $VMHost.ExtensionData.Hardware.CpuInfo.NumCpuCores MemoryGB = $MemoryTotalGB ESXVersion = $VMHost.ExtensionData.Summary.Config.Product.FullName ESXLicenseKey = $VMHost.LicenseKey DNS1 = $HostNetwork.DnsAddress[0] DNS2 = $HostNetwork.DnsAddress[1] Gateway = $HostNetwork.VMKernelGateway ManagementDevice = $HostNetworkManagementTrafficAdapter.name ManagementIP = $HostNetworkManagementTrafficAdapter.ip ManagementMTU = $HostNetworkManagementTrafficAdapter.mtu vMotionDevice = $HostNetworkvMotionAdapter.name vMotionIP = $HostNetworkvMotionAdapter.ip vMotionMTU = $HostNetworkvMotionAdapter.mtu vSANDevice = $HostNetworkvSANTrafficAdapter.name vSANIP = $HostNetworkvSANTrafficAdapter.ip vSANMTU = $HostNetworkvSANTrafficAdapter.mtu } $Report.add($Object) | Out-Null $Counter++}