I am looking for a PowerCLI command to get the following report
To list all VM's in a DC , the report shall contain VM-name, CPU , Memory, Disk Size, GuestOS, Port Group Name, Vlan ID, Cluster, Host name
Between -VMHost and $_.VMHost there should be a blank.
Probably disappeared during the copy/paste.
The file I attached is correct.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
Get-VM |
Select Name,NumCPU,MemoryGB,UsedSpaceGB,
@{N='GuestOS';E={$_.Guest.OSFUllName}},
@{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},
@{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost $_.VMHost).VlanId -join '|'}},
@{N='Cluster';E={(Get-Cluster -VM $_).Name}},
@{N='HostName';E={$_.VMHost.Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Are you connected to your vCenter (COnnect-VIServer)?
How do you run the script? From a .ps1 file?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$VMs = get-VM
$Report = @()
Foreach ($VM in $VMs) {
$NewObj = "" | Select VMName, CPU, Memory, DiskSize, GuestOS, PGName, VLANID, Cluster, HostName
$NewObj.VMName = $VM.Name
$NewObj.CPU = $VM.NumCPU
$NewObj.Memory = $VM.MemoryGB # Or swap out w/ $VM.MemoryMB if you want
$NewObj.DiskSize = $VM.ProvisionedSpaceGB # If you want just used $VM.UsedSpaceGB
$PG = $VM | Get-VirtualPortGroup
If ($PG.count -gt 1)
{
$PortGroups = $null
$VLANIDs = $null
Foreach ($Portgroup in $PG)
{
$PortGroups = $Portgroup.Name + " | " + $PortGroups
$VLANIDs = $Portgroup.VLANID + " | " + $VLANIDs
}
$NewObj.PGName = $PortGroups.trimend("|")
$NewObj.VLANIDs = $VLANIDs.trimend("|")
}
Else {
$NewObj.PGName = $PG.Name
$NewObj.VLANID = $PG.VLANID
}
$NewObj.GuestOS = $VM.Guestid
$NewObj.Cluster = $VM.vmhost.parent.name
$NewObj.hostname = $VM.vmhost.name
$Report += $NewObj
}
$Report
Hi Luc
I connected to vCenter server and I can execute the command , the output is appears like this.
We need to export the report to .CSV file in location (ex: C:\test\report.csv) , Could you please provide a script for report.
Name : GOCCTRAF2_2_ENV2
NumCpu : 4
MemoryGB : 8
UsedSpaceGB : 28.461758323945105075836181641
GuestOS : Microsoft Windows Server 2016 (64-bit)
PortgroupName : OCS-V1A-PG
PortgroupVlanId :
Cluster : OCC-Cluster01
HostName : 10.16.48.69
Name : TVS-Web02
NumCpu : 4
MemoryGB : 8
UsedSpaceGB : 0
GuestOS :
PortgroupName : CCTV-PG
PortgroupVlanId :
Cluster : OCC-Cluster01
HostName : 10.16.48.69
Hi Luc
I connected to vCenter server and I can execute the command , the output is appears like this.
We need to export the report to .CSV file in location (ex: C:\test\report.csv) , Could you please provide a script for report.
Name : GOCCTRAF2_2_ENV2
NumCpu : 4
MemoryGB : 8
UsedSpaceGB : 28.461758323945105075836181641
GuestOS : Microsoft Windows Server 2016 (64-bit)
PortgroupName : OCS-V1A-PG
PortgroupVlanId :
Cluster : OCC-Cluster01
HostName : 10.16.48.69
Name : TVS-Web02
NumCpu : 4
MemoryGB : 8
UsedSpaceGB : 0
GuestOS :
PortgroupName : CCTV-PG
PortgroupVlanId :
Cluster : OCC-Cluster01
HostName : 10.16.48.69
Sure, just send the result over the pipeline to Export-Csv
Get-VM |
Select Name,NumCPU,MemoryGB,UsedSpaceGB,
@{N='GuestOS';E={$_.Guest.OSFUllName}},
@{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},
@{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost $_.VMHost).VlanId -join '|'}},
@{N='Cluster';E={(Get-Cluster -VM $_).Name}},
@{N='HostName';E={$_.VMHost.Name}} |
Export-Csv -Path C:\Test\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc
The report is working, VLANID field is showing empty.
PortgroupName PortgroupVlanId Cluster HostName
OCS-V1A-PG Cluster01 10.16.2.140
OCS-V1A-PG Cluster02 10.16.48.69
CCTV-PG Cluster02 10.16.48.69
Can you attach the script as you are running it?
It might be that something went wrong with your copy/paste.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here is the script which I am running
PowerCLI C:\> Get-VM |
>> Select Name,NumCPU,MemoryGB,UsedSpaceGB,
>> @{N='GuestOS';E={$_.Guest.OSFUllName}},
>> @{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},
>> @{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost$_.VMHost).VlanId -join '|'}},
>> @{N='Cluster';E={(Get-Cluster -VM $_).Name}},
>> @{N='HostName';E={$_.VMHost.Name}} |
>> Export-Csv -Path C:\Test\report.csv -NoTypeInformation -UseCulture
PowerCLI C:\>
You're missing a blank in here -VMHost $_.VMHost
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LUC, I am not catching your point, What I am missing in the script
Get-VM |
Select Name,NumCPU,MemoryGB,UsedSpaceGB,
@{N='GuestOS';E={$_.Guest.OSFUllName}},
@{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},
@{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost $_.VMHost).VlanId -join '|'}},
@{N='Cluster';E={(Get-Cluster -VM $_).Name}},
@{N='HostName';E={$_.VMHost.Name}} |
Export-Csv -Path C:\Test\report.csv -NoTypeInformation -UseCulture
Between -VMHost and $_.VMHost there should be a blank.
Probably disappeared during the copy/paste.
The file I attached is correct.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc
Many thanks the script is working perfectly, this is the final script I am using. (Included few additional fields)
Connect-VIServer -Server 10.16.48.80 -User administrator@vsphere.local -Password r2020Rta18$
Get-VM |
Select Name,PowerState,Version,NumCPU,MemoryGB,ProvisionedSpaceGB,UsedSpaceGB,
@{N='GuestOS';E={$_.Guest.OSFUllName}},
@{N='PortgroupName';E={(Get-NetworkAdapter -VM $_).NetworkName -join '|'}},
@{N='PortgroupVlanId';E={(Get-VirtualPortGroup -Name (Get-NetworkAdapter -VM $_).NetworkName -VMHost $_.VMHost).VlanId -join '|'}},
@{N='Cluster';E={(Get-Cluster -VM $_).Name}},
@{N='HostName';E={$_.VMHost.Name}} |
Export-Csv -Path C:\Test\report.csv -NoTypeInformation -UseCulture