Search code examples
powershellvmwarepowercli

PowerCLI Get VMs those fit some conditions


I'm trying to get our some Linux distros from vCenter by using PowerCLI. But I don't want to get Appliance VMs. So I have 2 different successful PowerCLI scripts those can find these machines. I want merge these scripts but I'm new on PowerCLI and it's syntax.

I'm sharing these scripts at below:

Non-Appliance List:

Get-VM | `
Get-Annotation | `
Where-Object {$_.name -eq "Appliance"} | `
Where-Object {$_.value -eq 'No'} | `
Export-Csv C:\Users\me\Documents\non-appliance-list.csv -NoTypeInformation -UseCulture

Linux List:

Get-View -Property @("Name", "Config.GuestFullName","Guest.GuestFullName") | `
Select -Property Name, @{N="COS";E={$_.Config.GuestFullName}}, @{N="ROS";E={$_.Guest.GuestFullName}} | `
Where-Object ({$_.ROS -like 'Centos*' -or $_.ROS -like 'Suse*' -or $_.ROS -like 'Ubuntu*'}) | `    
Select AnnotatedEntity,Name,Value | `
Export-Csv C:\Users\me\Documents\linux-list.csv -NoTypeInformation -UseCulture

Script I imagined but doesn't worked:

Get-VM | `
Get-Annotation | `
Where-Object {$_.name -eq "Appliance"} | `
Where-Object {$_.value -eq 'No'} | `
Get-View -Property @("Name", "Config.GuestFullName","Guest.GuestFullName") | `
Select -Property Name, @{N="COS";E={$_.Config.GuestFullName}}, @{N="ROS";E={$_.Guest.GuestFullName}} | `
Where-Object ({$_.ROS -like 'Centos*' -or $_.ROS -like 'Suse*' -or $_.ROS -like 'Ubuntu*'}) | `    
Select AnnotatedEntity,Name,Value | `
Export-Csv C:\Users\me\Documents\linux--list.csv -NoTypeInformation -UseCulture

Maybe It has been a XY-Question. If you have a better way to get Linux VMs those are not appliance, you can say me this method.


Solution

  • You might be better off making use of some variables along the way to help make this a bit easier.

    Example:

    $LinuxVMs = Get-VM | `
    Get-Annotation | `
    Where-Object {$_.name -eq "Appliance"} | `
    Where-Object {$_.value -eq 'No'}
    

    Now you have the ability to pipeline the LinuxVMs variable into the Export-Csv cmdlet if you need as well as reference it for your second script.

    Example:

    $LinuxVMs | Get-View -Property @("Name", "Config.GuestFullName","Guest.GuestFullName") | `
    Select -Property Name, @{N="COS";E={$_.Config.GuestFullName}}, @{N="ROS";E={$_.Guest.GuestFullName}} | `
    Where-Object ({$_.ROS -like 'Centos*' -or $_.ROS -like 'Suse*' -or $_.ROS -like 'Ubuntu*'}) | `    
    Select AnnotatedEntity,Name,Value | `
    Export-Csv C:\Users\me\Documents\linux-list.csv -NoTypeInformation -UseCulture