This is faster, thanks LucD.
Original Message:
Sent: Dec 03, 2024 10:54 AM
From: LucD
Subject: How to get vm vlan-id into script ?
Try with
Datastore = (Get-View -Id $vm.DatastoreIdList -Property Name).Name -join '|'
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Dec 03, 2024 08:37 AM
From: dbutch1976
Subject: How to get vm vlan-id into script ?
Hi LucD,
After making some changes to the script above to include a field called lastmodified date everything is working, but the script is running extremely slowly. I'm almost certain it's the way I'm getting the Datastore, specifically this line:
Datastore = (($Datastores = Get-Datastore) | select Name, Id | where {$_.ID -match ((Get-View $vm).Datastore | Select Value).Value} | Select Name).Name
The Script:
Get-VM -PipelineVariable vm |
ForEach-Object {
Get-NetworkAdapter -VM $vm -PipelineVariable vnic |
ForEach-Object {
[PSCustomObject] @{
Name = $vm.Name
Powersate = $vm.Powerstate
GuestOS = $vm.Guest.OSFullName
Cluster = $vm.VMhost.Parent.Name
VMHostName = $vm.VMHost.Name
IP = ($vm.Guest.Nics | Where-Object {$_.Device.Name -eq $vnic.Name}).IPAddress -join ','
MAC = $vnic.MacAddress
NetworkName = $vnic.NetworkName
Datastore = (($Datastores = Get-Datastore) | select Name, Id | where {$_.ID -match ((Get-View $vm).Datastore | Select Value).Value} | Select Name).Name
NumCPU = $vm.NumCPU
MEMGB = [Math]::Round(($vm.MemoryGB),2)
DiskGB = [Math]::Round($vm.ProvisionedSpaceGB,2)
VLanID = & {
if ($vnic.NetworkName) {
Get-VirtualPortGroup -Name $vnic.NetworkName -VM $vm |
ForEach-Object -Process {
if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
if ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]) {
$_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
} elseif ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]) {
if ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]) {
[string]::Join(',', ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | ForEach-Object {"$($_.Start)-$($_.End)"}))
} else {
$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
}
}
}
else {
$_.VlanId
}
}
}
}
NicType = $vnic.Type
Notes = $vm.Notes
LastActiveDate = if ($Vm.PowerState -eq "PoweredOn") {
"ACTIVE"
}
else {
$vm.ExtensionData.Storage.Timestamp
}
}
}
}
Is there a better/faster way to pull this information?
Original Message:
Sent: Dec 02, 2024 06:56 PM
From: dbutch1976
Subject: How to get vm vlan-id into script ?
Thanks LucD!
Original Message:
Sent: Dec 02, 2024 10:01 AM
From: LucD
Subject: How to get vm vlan-id into script ?
Sure, it summarises all the values in the CapacityKB property for all HardDisks.
Then it converts that sum (in KB) to GB.
So the reference to $vm.HardDisks is correct
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Dec 02, 2024 08:56 AM
From: dbutch1976
Subject: How to get vm vlan-id into script ?
Found another mistake above:
$VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2) <----- This is returning 0 for all results.
Looks like the mistake is $vm.HardDisks should be $VMInfo.DiskGb
However, it looks like this is breaking something else, could you help me to understanding the part I underlined after the pipe?
$VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)
Original Message:
Sent: Dec 02, 2024 08:08 AM
From: LucD
Subject: How to get vm vlan-id into script ?
Yes, you are right, that should be the VMHost property, not the Host property.
The latest migration did really mess up the links.
I couldn't find that specific thread anymore, but I suspect it might have been the same code I used in Unable to get the VLanID and VMHostname from VM running with Distributed vSwitch? | Automation
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Dec 02, 2024 07:55 AM
From: dbutch1976
Subject: How to get vm vlan-id into script ?
Hi LucD,
I think there may have been mistakes in the original script for cluster and hostname:
$VMInfo.Host = $vm.host.name
$VMInfo.Cluster = $vm.host.Parent.Name
should be:
$VMInfo.Host = $vm.VMhost.name
$VMInfo.Cluster = $vm.VMhost.Parent.Name
For my purposes I want to pull information from a specific set of VMs contained in a csv file, After adding the VLANID I came up with something like this:
$VMnames = Import-Csv C:\output\sourceVMs.csv
$ExportFilePath = "c:\output\Import_export_VMs_VLANs.csv"
$Report = @()
$Datastores = Get-Datastore | select Name, Id
$VMHosts = Get-VMHost | select Name, Parent
ForEach ($VMname in $VMnames) {
$VM = get-VM -name $VMname.name
$vmvlan = [string]::Join(',',(Get-VirtualPortGroup -VM $vm | %{$_.VlanId}))
$VMView = $VM | Get-View
$VMInfo = {} | Select VMName,Powerstate,OS,Description,IPAddress,IPAddress2,ToolsStatus,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,DiskFree,DiskUsed,VLAN,Notes
$VMInfo.VMName = $vm.name
$VMInfo.Powerstate = $vm.Powerstate
$VMInfo.OS = $vm.Guest.OSFullName
$VMInfo.Description = $vm.Description
$VMInfo.IPAddress = $vm.Guest.IPAddress[0]
$VMInfo.IPAddress2 = $vm.Guest.IPAddress[1]
$VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
$VMInfo.Host = $vm.VMhost.name
$VMInfo.Cluster = $vm.VMhost.Parent.Name
$VMInfo.Datastore = ($Datastores | where {$_.ID -match ($vmview.Datastore | Select Value).Value} | Select Name).Name
$VMInfo.NumCPU = $vm.NumCPU
$VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)
$VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)
$VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB),2)
$VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
$VMinfo.VLAN = $vmvlan
$VMinfo.Notes = $vm.Notes
$Report += $VMInfo
}
$Report = $Report | Sort-Object VMName
IF ($Report -ne "") {
$report | Export-Csv $ExportFilePath -NoTypeInformation
}
Looks like the URL you linked here All VM's with VLAN's is broken after the switch to Broadcom, do you have an working link for both standard and distributed switches?
Original Message:
Sent: May 09, 2012 12:07 PM
From: LucD
Subject: How to get vm vlan-id into script ?
You are passing an array, convert the array to a single string.
$vm = Get-VM MyVM
$vmvlan = [string]::Join(',',(Get-VirtualPortGroup -VM $vm | %{$_.VlanId}))