VMware Cloud Community
joshhilditch
Contributor
Contributor
Jump to solution

getting windows OS version (standard/datacentre)

I have this code:

$result = @()
$vms = Get-view -ViewType VirtualMachine -filter @{'guest.GuestFamily'='windowsGuest';'Runtime.PowerState'='poweredOn';'name'='.*cust.hoistcloud.com$'}
foreach ($vm in $vms) {
$obj = new-object psobject
$obj | Add-Member -MemberType NoteProperty -Name name -Value $vm.Name
$obj | Add-Member -MemberType NoteProperty -Name vCPUs -Value $vm.config.hardware.NumCPU
$obj | Add-Member -MemberType NoteProperty -Name CPUSocket -Value $vm.config.hardware.NumCPU
$obj | Add-Member -MemberType NoteProperty -Name Corepersocket -Value $vm.config.hardware.NumCoresPerSocket
$result += $obj
}
$result

What I would like to do is add another $obj line which adds the VMs OS, but it will also state the version (standard/datecentre)

 

I have this which I can run separate to get that version info:

$WindowsVMS = get-view -viewtype virtualmachine -property 'name','guest.guestFamily' -filter @{'guest.GuestFamily'='windowsGuest';'Runtime.PowerState'='poweredOn';'name'='.*cust.hoistcloud.com$'} 
$credential = get-credential
foreach ($vm in $WindowsVMS){

$vm | select name, powerstate, @{n='Edition';e={(Invoke-VMScript -GuestCredential $credential -ScriptType 'powershell' -ScriptText '(gwmi win32_operatingsystem).caption' -VM $vm.name).ScriptOutput} }

}

What I'm having trouble with is adding something like the above to the first code I have in this. What is the best way to get something like section two into $obj of section one?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You should be able to use the Expression from the calculated property (on the 2nd snippet) as the Value on the Add-Member.
Something like this for example

$result = @()
$vms = Get-view -ViewType VirtualMachine -filter @{'guest.GuestFamily'='windowsGuest';'Runtime.PowerState'='poweredOn';'name'='.*cust.hoistcloud.com$'}
foreach ($vm in $vms) {
    $obj = New-Object psobject
    $obj | Add-Member -MemberType NoteProperty -Name name -Value $vm.Name
    $obj | Add-Member -MemberType NoteProperty -Name vCPUs -Value $vm.config.hardware.NumCPU
    $obj | Add-Member -MemberType NoteProperty -Name CPUSocket -Value $vm.config.hardware.NumCPU
    $obj | Add-Member -MemberType NoteProperty -Name Corepersocket -Value $vm.config.hardware.NumCoresPerSocket
    $obj | Add-Member -MemberType NoteProperty -Name Edition -Value (Invoke-VMScript -GuestCredential $credential -ScriptType 'powershell' -ScriptText '(gwmi win32_operatingsystem).caption' -VM $vm.name).ScriptOutput
    $result += $obj
}
$result


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

You should be able to use the Expression from the calculated property (on the 2nd snippet) as the Value on the Add-Member.
Something like this for example

$result = @()
$vms = Get-view -ViewType VirtualMachine -filter @{'guest.GuestFamily'='windowsGuest';'Runtime.PowerState'='poweredOn';'name'='.*cust.hoistcloud.com$'}
foreach ($vm in $vms) {
    $obj = New-Object psobject
    $obj | Add-Member -MemberType NoteProperty -Name name -Value $vm.Name
    $obj | Add-Member -MemberType NoteProperty -Name vCPUs -Value $vm.config.hardware.NumCPU
    $obj | Add-Member -MemberType NoteProperty -Name CPUSocket -Value $vm.config.hardware.NumCPU
    $obj | Add-Member -MemberType NoteProperty -Name Corepersocket -Value $vm.config.hardware.NumCoresPerSocket
    $obj | Add-Member -MemberType NoteProperty -Name Edition -Value (Invoke-VMScript -GuestCredential $credential -ScriptType 'powershell' -ScriptText '(gwmi win32_operatingsystem).caption' -VM $vm.name).ScriptOutput
    $result += $obj
}
$result


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

0 Kudos
joshhilditch
Contributor
Contributor
Jump to solution

Cheers.

 

It doesn't seem to be outputting in the powershell window, do you know why that would be?

0 Kudos
joshhilditch
Contributor
Contributor
Jump to solution

ignore my last comment, works a charm! 

 

Cheers dude 

0 Kudos