VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Output File shows blank after validation

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'
}
}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

That worked. thank you very much 🙂

0 Kudos