VMware Cloud Community
DericVMW
Contributor
Contributor

Get all VMs with Running OS Like Linux or Other Script

Having a hard time here...how do i return results for just linux and OS's called "Other" instead of just all OSs. I have the script below:

get-vm |  Select Name,@{N="Configured OS";E={$_.ExtensionData.Config.GuestFullname}},@{N="Running OS";E={$_.Guest.OsFullName}}, @{N="Powered On";E={ $_.PowerState -eq “PoweredOn”}}, @{N="disktype";E={(Get-Harddisk $_).Storageformat}} | Export-CSV -NoTypeInformation C:\Users\iaasadmin\Desktop\getvms.csv

Any ideas to add the filter to just return Linux and Other OSs?

Thank you

4 Replies
LucD
Leadership
Leadership

Try like this

Get-VM |

where{$_.ExtensionData.Config.GuestFullname -notmatch "win"} |

Select Name,

    @{N="Configured OS";E={$_.ExtensionData.Config.GuestFullname}},

    @{N="Running OS";E={$_.Guest.OsFullName}},

    @{N="Powered On";E={ $_.PowerState -eq “PoweredOn”}},

    @{N="disktype";E={(Get-Harddisk $_).Storageformat}}


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

sajal1
Hot Shot
Hot Shot

Modify your query as given below:

get-vm |  Select Name,@{N="Configured OS";E={$_.ExtensionData.Config.GuestFullname}},@{N="Running OS";E={$_.Guest.OsFullName}}, @{N="Powered On";E={ $_.PowerState -eq “PoweredOn”}}, @{N="disktype";E={(Get-Harddisk $_).Storageformat}} | Where { ($_."Configured OS").contains("Other") -or ($_."Configured OS").contains("Linux") } | Export-CSV -NoTypeInformation C:\Users\iaasadmin\Desktop\getvms.csv

Reply
0 Kudos
ScottDriver42
Enthusiast
Enthusiast

I agree with Luc. Some linux distributions do not list their OS correctly, whereas I haven't run across a situation where Windows OS is incorrect. I will typically use something like the below to operate on windows machines. If you're looking to gather non-windows you can just negate the if clause, or use an else.

foreach($objCluster in Get-Cluster){
   
foreach ($objvm in $($objCluster|get-vm)){
         
if($objvm.guestid.contains("windows")){
             
#### windows actions or data collection goes here
          }else{
             
### non-windows actions or data collection goes here
          }
    }
}
Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
Reply
0 Kudos
wowitsdave
Enthusiast
Enthusiast

This was exactly what I needed, LucD​! I removed some of the parameters, but it was perfect. Thanks, as always!

Reply
0 Kudos