user146
Contributor
Contributor

Get-VM output question

Jump to solution

Hello,

I'm wondering if someone could explain, or point me in the right direction to understand, why the  "@{UpgradePolicy=manual}" part of the output is contained in what looks like a hashtable (or something) and how I might use only the "manual" part of the string?

Output example: VM: server1 current VMtools policy is @{UpgradePolicy=manual}

######################
## Creating variable containing all the VMs in the choosen vDC.
$vms = get-vm * -Location $DatacenterName | Sort-Object

## Script block to find VMs that match the conditions and output VMtools upgrade policy setting.
foreach ($vm in $vms) {
    if ($vm.GuestId -match 'windows' -and $vm.ExtensionData.Config.Tools.ToolsUpgradePolicy -match 'manual'){
        $current_policy = $vm | Select-Object @{N="UpgradePolicy";E={$_.Extensiondata.Config.Tools.toolsUpgradePolicy}}
        Write-Host -ForegroundColor Gray "VM: $vm current VMtools policy is $current_policy"
    }
}
######################
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

ExpandPrperty only accepts level 1 properties.
But you really don't need Select-Object for what you are trying to do.

Try like this

## Creating variable containing all the VMs in the choosen vDC.
Get-VM -Location $DatacenterName -pipelinevariable vm |
Where-Object { $vm.GuestId -match 'windows' -and $vm.ExtensionData.Config.Tools.ToolsUpgradePolicy -match 'manual'} |
foreach-object -process {
  $current_policy = $vm.Extensiondata.Config.Tools.toolsUpgradePolicy
  Write-Host -ForegroundColor Gray "VM: $($vm.Name) current VMtools policy is $current_policy"
}

 


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

View solution in original post

4 Replies
LucD
Leadership
Leadership

That is because you used Select-Object and a calculated property.
If you only want the value you should use the ExpandProperty parameter on the Select-Object cmdlet


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

0 Kudos
user146
Contributor
Contributor

Hello,

I had tried that but it fails, "Select-Object : Property "$_.Extensiondata.Config.Tools.toolsUpgradePolicy" cannot be found". I assume I'm not using the parameter correctly?

## Creating variable containing all the VMs in the choosen vDC.
$vms = get-vm * -Location $DatacenterName | Sort-Object

## Script block to find VMs that match the conditions and output VMtools upgrade policy setting.
foreach ($vm in $vms) {
    if ($vm.GuestId -match 'windows' -and $vm.ExtensionData.Config.Tools.ToolsUpgradePolicy -match 'manual'){
        $current_policy = $vm | Select-Object -ExpandProperty {$_.Extensiondata.Config.Tools.toolsUpgradePolicy}
        Write-Host -ForegroundColor Gray "VM: $vm current VMtools policy is $current_policy"
    }
}
0 Kudos
LucD
Leadership
Leadership

ExpandPrperty only accepts level 1 properties.
But you really don't need Select-Object for what you are trying to do.

Try like this

## Creating variable containing all the VMs in the choosen vDC.
Get-VM -Location $DatacenterName -pipelinevariable vm |
Where-Object { $vm.GuestId -match 'windows' -and $vm.ExtensionData.Config.Tools.ToolsUpgradePolicy -match 'manual'} |
foreach-object -process {
  $current_policy = $vm.Extensiondata.Config.Tools.toolsUpgradePolicy
  Write-Host -ForegroundColor Gray "VM: $($vm.Name) current VMtools policy is $current_policy"
}

 


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

View solution in original post

user146
Contributor
Contributor

Thanks that works nicely and I learned a bit about "-pipelinevariable".

Something I noticed before I seen your response was that if I exported the results from my original script to .csv the output was fine, I had two columns, one column of names and the other of update policy status. This also worked with out-gridview.

I had intended to offer the user running the script the choice to output to the terminal or .csv and that was how I found the problem with the terminal output.

0 Kudos