VMware Cloud Community
BBB36
Enthusiast
Enthusiast
Jump to solution

Get-VirtualPortGroup : Cannot validate argument on parameter 'VM'. The argument is null or empty.

I did get most of this script earlier from this site, but when I ran it, I was getting the error below. 

FYI: I'm trying to generate a report to grab the vCenter name, Cluster, Hosts, portgroup names and VLAN IDs. We're using vDS, and I was trying to run this for two vCenters at the same time. 

********************************************************************************************************

 

$report = foreach ($vCenter in $vCenters) {
Connect-VIServer $vCenter -Protocol https -Credential $Creds | Out-Null
   Get-Cluster  -PipelineVariable cluster |
    Get-VMHost |
    Get-VM -PipelineVariable vm |
    Where-Object { $_.ExtensionData.Config.ManagedBy.extensionKey -NotLike "com.vmware.vcDr*" } |
    foreach-object -process {
      $obj = [ordered]@{
        vCenter = ([uri]$vm.ExtensionData.Client.ServiceUrl).Host
        Cluster = $cluster.Name
        VMName = $vm.Name
        Portgroup1 = ''
        VLAN1 = ''
        Portgroup2 = ''
        VLAN2 = ''
        Portgroup3 = ''
        VLAN3 = ''
        Portgroup4 = ''
        VLAN4 = ''
      }
    }
  $i = 1
  Get-VirtualPortGroup -VM $vm |
  foreach-object -Process {
    $obj."Portgroup$i" = $_.Name
    $obj."VLAN$i" = $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
    $i++
  }
  New-Object -Type PSObject -Property $obj
}
$report | Export-Csv -Path H:\Files\Scripts\Outputs\VlanIds.csv -Append -NoTypeInformation -UseCulture

 

 The error I got was below:

 

Get-VirtualPortGroup : Cannot validate argument on parameter 'VM'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:25 char:28
+   Get-VirtualPortGroup -VM $vm |
+                            ~~~
    + CategoryInfo          : InvalidData: (:) [Get-VirtualPortGroup], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetVirtualPortGroup
 
Get-VirtualPortGroup : Cannot validate argument on parameter 'VM'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:25 char:28
+   Get-VirtualPortGroup -VM $vm |
+                            ~~~
    + CategoryInfo          : InvalidData: (:) [Get-VirtualPortGroup], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetVirtualPortGroup

 

What aren't I getting right?

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Looks like a curly brace line is in the wrong location.

Note that this script only reports on the first 4 portgroups, that's why i added the Select-Object -First 4 line

$report = foreach ($vCenter in $vCenters) {
    Connect-VIServer $vCenter -Protocol https -Credential $Creds | Out-Null
    Get-Cluster  -PipelineVariable cluster |
        Get-VMHost |
        Get-VM -PipelineVariable vm |
        Where-Object { $_.ExtensionData.Config.ManagedBy.extensionKey -NotLike "com.vmware.vcDr*" } |
        ForEach-Object -Process {
            $obj = [ordered]@{
                vCenter    = ([uri]$vm.ExtensionData.Client.ServiceUrl).Host
                Cluster    = $cluster.Name
                VMName     = $vm.Name
                Portgroup1 = ''
                VLAN1      = ''
                Portgroup2 = ''
                VLAN2      = ''
                Portgroup3 = ''
                VLAN3      = ''
                Portgroup4 = ''
                VLAN4      = ''
            }
            $i = 1
            Get-VirtualPortGroup -VM $vm |
                Select-Object -First 4 |
                ForEach-Object -Process {
                    $obj."Portgroup$i" = $_.Name
                    $obj."VLAN$i" = $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
                    $i++
                }
                New-Object -Type PSObject -Property $obj
            }
}
$report | Export-Csv -Path H:\Files\Scripts\Outputs\VlanIds.csv -Append -NoTypeInformation -UseCulture

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Looks like a curly brace line is in the wrong location.

Note that this script only reports on the first 4 portgroups, that's why i added the Select-Object -First 4 line

$report = foreach ($vCenter in $vCenters) {
    Connect-VIServer $vCenter -Protocol https -Credential $Creds | Out-Null
    Get-Cluster  -PipelineVariable cluster |
        Get-VMHost |
        Get-VM -PipelineVariable vm |
        Where-Object { $_.ExtensionData.Config.ManagedBy.extensionKey -NotLike "com.vmware.vcDr*" } |
        ForEach-Object -Process {
            $obj = [ordered]@{
                vCenter    = ([uri]$vm.ExtensionData.Client.ServiceUrl).Host
                Cluster    = $cluster.Name
                VMName     = $vm.Name
                Portgroup1 = ''
                VLAN1      = ''
                Portgroup2 = ''
                VLAN2      = ''
                Portgroup3 = ''
                VLAN3      = ''
                Portgroup4 = ''
                VLAN4      = ''
            }
            $i = 1
            Get-VirtualPortGroup -VM $vm |
                Select-Object -First 4 |
                ForEach-Object -Process {
                    $obj."Portgroup$i" = $_.Name
                    $obj."VLAN$i" = $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
                    $i++
                }
                New-Object -Type PSObject -Property $obj
            }
}
$report | Export-Csv -Path H:\Files\Scripts\Outputs\VlanIds.csv -Append -NoTypeInformation -UseCulture

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BBB36
Enthusiast
Enthusiast
Jump to solution

Ah! ok. Got it. Thanks so much!

0 Kudos