Hi,
After validation, output file shows blank. Please help!!
$vCenters = @(
"myvcenter1"
"myvcenter2"
)
Connect-viserver -Server $vCenters
$servs = Get-Content ".\Server_List.txt"
foreach ($serv in $servs){
if (($serv.name).Length -gt 15) {
$report = Get-VM $serv | Select @{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host}},
@{N="Folder"; E={$_.Folder.Name}},
@{N="VM"; E={$_.Name}},
@{N="IP_Address";E={@($_.guest.IPAddress[0])}},
@{N="OS"; E={@($_.guest.OSFullName)}},
@{N="PowerState"; E={$_.PowerState}}
$report | Export-Excel -Path $reportlocation -WorksheetName 'Incorrect_VM_Name'
} else {
$report1 = Get-VM $serv | Select @{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host}},
@{N="Folder"; E={$_.Folder.Name}},
@{N="VM"; E={$_.Name}},
@{N="IP_Address";E={@($_.guest.IPAddress[0])}},
@{N="OS"; E={@($_.guest.OSFullName)}},
@{N="PowerState"; E={$_.PowerState}}
$report1 | Export-Excel -Path $reportlocation1 -WorksheetName 'Correct_VM_Name'
}
}
Since you seem to be passing strings to the $serv variable, there is no Name property.
Just test like this
if ($serv.Length -gt 15) {
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
$vCenters = @(
"myvcenter1"
"myvcenter2"
)
Connect-VIServer -Server $vCenters
$servs = Get-Content ".\Server_List.txt"
$report = @()
$report1 = @()
foreach ($serv in $servs) {
if (($serv.name).Length -gt 15) {
$report += Get-VM $serv |
select @{N = 'vCenter'; E = { ([uri]$_.ExtensionData.Client.ServiceUrl).Host } },
@{N = "Folder"; E = { $_.Folder.Name } },
@{N = "VM"; E = { $_.Name } },
@{N = "IP_Address"; E = { @($_.guest.IPAddress[0]) } },
@{N = "OS"; E = { @($_.guest.OSFullName) } },
@{N = "PowerState"; E = { $_.PowerState } }
} else {
$report1 += Get-VM $serv | select @{N = 'vCenter'; E = { ([uri]$_.ExtensionData.Client.ServiceUrl).Host } },
@{N = "Folder"; E = { $_.Folder.Name } },
@{N = "VM"; E = { $_.Name } },
@{N = "IP_Address"; E = { @($_.guest.IPAddress[0]) } },
@{N = "OS"; E = { @($_.guest.OSFullName) } },
@{N = "PowerState"; E = { $_.PowerState } }
}
}
$report | Export-Excel -Path $reportlocation -WorksheetName 'Incorrect_VM_Name'
$report1 | Export-Excel -Path $reportlocation1 -WorksheetName 'Correct_VM_Name'
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I am getting the output captured in the file but validation is not working. All the VMs which are more than 15 characters also getting captured in the same file - Correct_VM_Name.xlsx
Since you seem to be passing strings to the $serv variable, there is no Name property.
Just test like this
if ($serv.Length -gt 15) {
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked. thank you very much 🙂