Hello, I hope all are well
I have been tweaking a script which LucD has provided some valuable help previously, and I am trying to get the ESX host that the VM resides on and I am having some issues. The current script is as follows:
$reportvmdata_html = @()
foreach ($dc in Get-Datacenter) {
foreach ($cluster in Get-Cluster -Location $dc){
$vms = Get-view -ViewType VirtualMachine -SearchRoot $cluster.ExtensionData.MoRef
foreach ($vm in $vms){
$infovmdata_html = "" | select Datacenter, Name, ToolsStatus, NumCpu, MemoryGB, guestos, IPAddress, Datastore, DatastoreUsedGB, CurrentESXHost
$infovmdata_html.IPAddress = ($vm.Guest.net.IPAddress | where{$_} | Sort-Object -Unique) -join [Environment]::NewLine
$infovmdata_html.Datastore = (Get-View -Id $vm.Datastore -Property Name).Name -join [Environment]::NewLine
$infovmdata_html.DatastoreUsedGB = [math]::Round(($vm.Storage.PerDatastoreUsage.Committed | Measure-Object -Sum).Sum/1GB,1)
$infovmdata_html.datacenter = $dc.name
$infovmdata_html.Name = $vm.name
$infovmdata_html.toolsstatus = $vm.guest.toolsstatus
$infovmdata_html.NumCpu = $vm.Summary.config.NumCpu
$infovmdata_html.MemoryGB = $vm.Summary.config.memorySizeMB / 1024
$infovmdata_html.guestos = $vm.guest.guestfullname
$infovmdata_html.CurrentESXHost = (Get-VMHost -VM $vm.name).Name
$reportvmdata_html += $infovmdata_html
}
}
}
It is the $infovmdata_html.CurrentESXHost value that is causing my current issue. I have tried formatting the command as follows - non of which appear to work:
(Get-VMHost -VM $vm.name).Name
(Get-VMHost -VM $vm).Name
$vm.Summary.config.esxhost
Please can any guidance be given? Thanks in advance.
The trick is then to insert the <br/> after the ConvertTo-Html.
Use a -join with a character that is not otherwise present in the output, and replace that with <br/>.
Something like this for example
foreach ($dc in Get-Datacenter) {
foreach ($cluster in Get-Cluster -Location $dc){
$vms = Get-view -ViewType VirtualMachine -SearchRoot $cluster.ExtensionData.MoRef
foreach ($vm in $vms){
$infovmdata_html = "" | select Datacenter, Name, ToolsStatus, NumCpu, MemoryGB, guestos, IPAddress, Datastore, DatastoreUsedGB, CurrentESXHost
$infovmdata_html.IPAddress = ($vm.Guest.net.IPAddress | where{$_} | Sort-Object -Unique) -join "|"
$infovmdata_html.Datastore = (Get-View -Id $vm.Datastore -Property Name).Name -join "|"
$infovmdata_html.DatastoreUsedGB = [math]::Round(($vm.Storage.PerDatastoreUsage.Committed | Measure-Object -Sum).Sum/1GB,1)
$infovmdata_html.datacenter = $dc.name
$infovmdata_html.Name = $vm.name
$infovmdata_html.toolsstatus = $vm.guest.toolsstatus
$infovmdata_html.NumCpu = $vm.Summary.config.NumCpu
$infovmdata_html.MemoryGB = $vm.Summary.config.memorySizeMB / 1024
$infovmdata_html.guestos = $vm.guest.guestfullname
$infovmdata_html.CurrentESXHost = (Get-VMHost -VM $vm.name).Name
$reportvmdata_html += $infovmdata_html
}
}
}
($reportvmdata_html | ConvertTo-Html).Replace('|','<br/>') | Out-File -FilePath .\report.html
Invoke-Item -Path .\report.html
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Not sure what problem you are seeing?
The line is correct for me
$infovmdata_html.CurrentESXHost = (Get-VMHost -VM $vm.name).Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks, I traced the error the script was producing to an orphaned VM. I removed that and the script runs without errors
I m not sure if it ever worked, but the two lines with [Environment]::NewLine on them do not split the string up and perform the new line as required - there is only a space in-between the string output and I'd like it to be on separate lines.
For example, for the VMs with multiple IP addresses, or VMDKs on more than one datastore, I want the output to be on separate lines. e.g.:
172.16.1.1 172..16.1.2
becomes
172.16.1.1
172.16.1.2
and
datastore1 datastore2
becomes
datastore1
datastore2
Can you offer any insight why newline is not working please?
Did you already try with backtick-n?
$infovmdata_html.Datastore = (Get-View -Id $vm.Datastore -Property Name).Name -join "`n"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes I also tried that and the "'r" option but it still translates into a space. I have also tried <br> as the output is html but that causes an error
The trick is then to insert the <br/> after the ConvertTo-Html.
Use a -join with a character that is not otherwise present in the output, and replace that with <br/>.
Something like this for example
foreach ($dc in Get-Datacenter) {
foreach ($cluster in Get-Cluster -Location $dc){
$vms = Get-view -ViewType VirtualMachine -SearchRoot $cluster.ExtensionData.MoRef
foreach ($vm in $vms){
$infovmdata_html = "" | select Datacenter, Name, ToolsStatus, NumCpu, MemoryGB, guestos, IPAddress, Datastore, DatastoreUsedGB, CurrentESXHost
$infovmdata_html.IPAddress = ($vm.Guest.net.IPAddress | where{$_} | Sort-Object -Unique) -join "|"
$infovmdata_html.Datastore = (Get-View -Id $vm.Datastore -Property Name).Name -join "|"
$infovmdata_html.DatastoreUsedGB = [math]::Round(($vm.Storage.PerDatastoreUsage.Committed | Measure-Object -Sum).Sum/1GB,1)
$infovmdata_html.datacenter = $dc.name
$infovmdata_html.Name = $vm.name
$infovmdata_html.toolsstatus = $vm.guest.toolsstatus
$infovmdata_html.NumCpu = $vm.Summary.config.NumCpu
$infovmdata_html.MemoryGB = $vm.Summary.config.memorySizeMB / 1024
$infovmdata_html.guestos = $vm.guest.guestfullname
$infovmdata_html.CurrentESXHost = (Get-VMHost -VM $vm.name).Name
$reportvmdata_html += $infovmdata_html
}
}
}
($reportvmdata_html | ConvertTo-Html).Replace('|','<br/>') | Out-File -FilePath .\report.html
Invoke-Item -Path .\report.html
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks, that works perfectly
Sorry to bother you again but I am trying to add the power state of the VM to the report but the added line to get the powerstate just displays as blank - any ideas please:
$reportvmdata_html = @()
foreach ($dc in Get-Datacenter) {
foreach ($cluster in Get-Cluster -Location $dc){
$vms = Get-view -ViewType VirtualMachine -SearchRoot $cluster.ExtensionData.MoRef
foreach ($vm in $vms){
$infovmdata_html = "" | select Datacenter, Name, PowerState, ToolsStatus, NumCpu, MemoryGB, guestos, IPAddress, Datastore, DatastoreUsedGB, CurrentESXHost
$infovmdata_html.IPAddress = ($vm.Guest.net.IPAddress | where{$_} | Sort-Object -Unique) -join "|"
$infovmdata_html.Datastore = (Get-View -Id $vm.Datastore -Property Name).Name -join "|"
$infovmdata_html.DatastoreUsedGB = [math]::Round(($vm.Storage.PerDatastoreUsage.Committed | Measure-Object -Sum).Sum/1GB,1)
$infovmdata_html.datacenter = $dc.name
$infovmdata_html.Name = $vm.name
$infovmdata_html.PowerState = $vm.powerstate
$infovmdata_html.toolsstatus = $vm.guest.toolsstatus
$infovmdata_html.NumCpu = $vm.Summary.config.NumCpu
$infovmdata_html.MemoryGB = $vm.Summary.config.memorySizeMB / 1024
$infovmdata_html.guestos = $vm.guest.guestfullname
$infovmdata_html.CurrentESXHost = (Get-VMHost -VM $vm.name).Name
$reportvmdata_html += $infovmdata_html
}
}
}
Since you are working with the VirtualMachine object (Get-View) not the VirtualMachine object returned by Get-VM, you should use
$infovmdata_html.PowerState = $vm.Runtime.Powerstate
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference