VMware Cloud Community
lledarby
Contributor
Contributor
Jump to solution

If-Then with Guest OS Details

Hi,

I have been parsing together some scripts trying to build a report that will show me the full WIM details for windows machines, else the Operating System default for the non-windows machines.

I have this:

Get-VM |Sort Name |Select Name, NumCPU, @{N=“OperatingSystem“;E={(Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem |Select Caption).Caption}}, @{N=“ServicePack“;E={(Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem |Select CSDVersion).CSDVersion} -Enable} | Export-Csv "C:\scripts\Output\ostest1.csv" -NoTypeInformation

This gets me the Windows names, but i'm not sure how to say if Operating System contains Microsoft, run the Get-WimObject commands, else just report the OS as listed in Get VM

Any help would be appreciated!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-VM |Sort Name |

Select Name, NumCPU,

    @{N=“OperatingSystem“;E={

        if($_.ExtensionData.summary.config.guestfullname -match "Windows"){

            Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem |Select -ExpandProperty Caption

        }

        else{

            $_.ExtensionData.summary.config.guestfullname

        }

    }},

    @{N=“ServicePack“;E={

        if($_.ExtensionData.summary.config.guestfullname -match "Windows"){

            Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem |Select -ExpandProperty CSDVersion

        }

        else{"na"}}} |

Export-Csv "C:\scripts\Output\ostest1.csv" -NoTypeInformation


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

View solution in original post

Reply
0 Kudos
2 Replies
kunaludapi
Expert
Expert
Jump to solution

Get-VM | Where-Object {$_.ExtensionData.summary.config.guestfullname -match "Windows"} | Sort Name |Select Name, NumCPU, @{N=“OperatingSystem“;E={(Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem |Select Caption).Caption}}, @{N=“ServicePack“;E={(Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem |Select CSDVersion).CSDVersion} -Enable} | Export-Csv "C:\scripts\Output\ostest1.csv" -NoTypeInformation

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-VM |Sort Name |

Select Name, NumCPU,

    @{N=“OperatingSystem“;E={

        if($_.ExtensionData.summary.config.guestfullname -match "Windows"){

            Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem |Select -ExpandProperty Caption

        }

        else{

            $_.ExtensionData.summary.config.guestfullname

        }

    }},

    @{N=“ServicePack“;E={

        if($_.ExtensionData.summary.config.guestfullname -match "Windows"){

            Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem |Select -ExpandProperty CSDVersion

        }

        else{"na"}}} |

Export-Csv "C:\scripts\Output\ostest1.csv" -NoTypeInformation


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

Reply
0 Kudos