VMware Cloud Community
bairstowscott
Contributor
Contributor

{get-view $_.Id} command

Hi

Any idea why I am getting the following error?

$vh01='hostname'                                                                           
$vh01moref=$vh01 | % {get-view $_.Id}

ca.PNG

0 Kudos
4 Replies
LucD
Leadership
Leadership

The string "hostname" is not a MoRef that Get-View expects on the ID parameter.

In fact a [string] object has no Id property.

You can do

Get-VMHost -Name hostname | %{Get-View -Id $_.Id}

The object returned by Get-VMHost has an Id property that can be used by Get-View


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

0 Kudos
bairstowscott
Contributor
Contributor

Here is the script I use  

     $vh01=$hostname6                                                                             
    $vh01moref=$vh01 | % {get-view $_.Id}
   
    $vh01morefconfig=$vh01moref.configmanager                                                                               
    $vh01netsys=$vh01morefconfig.networksystem
    $vh01netsysmoref=get-view $vh01netsys

    foreach ($vh01vsw0 in $vh01netsysmoref.NetworkConfig.Vswitch){   
        $swspec= $vh01vsw0.Spec
        $swspec.policy.security.allowPromiscuous=$false
        $swspec.policy.security.forgedTransmits=$true
        $swspec.policy.security.macChanges=$false
        $vh01netsysmoref.UpdateVirtualSwitch($vh01vsw0.name,$swspec)
    }

0 Kudos
LucD
Leadership
Leadership

Try it like this

$vh01Name = $hostname6 
$vh01 = Get-VMHost -Name $hostname6
$vh01morefconfig = $vh01.ExtensionData.configmanager 
$vh01netsys = $vh01morefconfig.networksystem 
$vh01netsysmoref
= get-view $vh01netsys foreach ($vh01vsw0 in $vh01netsysmoref.NetworkConfig.Vswitch){   $swspec = $vh01vsw0.Spec
  $swspec.policy.security.allowPromiscuous=$false
  $swspec.policy.security.forgedTransmits=$true
  $swspec.policy.security.macChanges=$false
  $vh01netsysmoref.UpdateVirtualSwitch($vh01vsw0.name,$swspec) }


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

0 Kudos
tdubb123
Expert
Expert

Hi

I dont understand why you need to do

get-vmhost -name vmhostname | % { get-view -id $_.id}

and just

get-vmhost -name vmhostname | get-view

why are you using the $_.id

0 Kudos