Hi,
The below scripts works good for one vcenter, If I want to query multiple vcenter, it is not working
I would like to check from multiple vCenters, if VM exists and if found then VM name should be sent respective vcenter named output file.
Please help!!
$vCenters = @(
"vcenter1"
"vcenter2"
"vcenter3"
)
Connect-viserver -Server $vCenters
$VMS = Import-Csv "D:\serverlist.csv"
$array = @()
foreach ($VM in $VMS) {
$info = Get-VM -Name $VM.Name -ErrorAction SilentlyContinue
If ($info) {
Write-Host "$($VM.Name) is present"
$array += $VM | Select Name
} Else {
Write-Host "$($VM.Name) does not exist"
$array1 += $VM | Select Name
}
}
$array | Export-Csv "D:\VM_Exists.csv" -NoTypeInformation -UseCulture
$array1 | Export-Csv "D:\VM_Not_Exists.csv" -NoTypeInformation -UseCulture
The VirtualMachine object is in $info, not $VM
$array += $VM | Select-Object @{N = "Folder"; E = { $info.Folder.Name } },
Name,
@{N = 'vCenter'; E = { ([uri]$info.ExtensionData.Client.ServiceUrl).Host } },
@{N = "IP_Address"; E = { @($info.guest.IPAddress[0]) } },
@{N = "OS"; E = { @($info.guest.OSFullName) } }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Add a calculated property
$array += $VM | select Name,@{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I tried as below, expect the Name, rest all the fields shows blank in the output file.
$VMS = Import-Csv "D:\serverlist.csv"
$array = @()
$array1 = @()
foreach ($VM in $VMS) {
$info = Get-VM -Name $VM.Name -ErrorAction SilentlyContinue
If ($info) {
Write-Host "$($VM.Name) is present"
$array += $VM | Select @{N="Folder"; E={$_.Folder.Name}}, Name, @{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host}}, @{N="IP_Address";E={@($_.guest.IPAddress[0])}}, @{N="OS"; E={@($_.guest.OSFullName)}}
} Else {
Write-Host "$($VM.Name) does not exist"
$array1 += $VM | Select Name
}
}
$array | Export-Csv "D:\VM_Exists.csv" -NoTypeInformation -UseCulture
$array1 | Export-Csv "D:\VM_Not_Exists.csv" -NoTypeInformation -UseCulture
The VirtualMachine object is in $info, not $VM
$array += $VM | Select-Object @{N = "Folder"; E = { $info.Folder.Name } },
Name,
@{N = 'vCenter'; E = { ([uri]$info.ExtensionData.Client.ServiceUrl).Host } },
@{N = "IP_Address"; E = { @($info.guest.IPAddress[0]) } },
@{N = "OS"; E = { @($info.guest.OSFullName) } }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much. that worked perfectly now. 🙂