简体   繁体   中英

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.

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

You have just mashed the scripts together, piping the first into the second. This won't work.

You can have each script block in a single script, then merge the resulting csv's using one of the methods here: Merging multiple CSV files into one using PowerShell

Stinkyfriend's code:

Get-ChildItem -Filter *.csv | Select-Object -ExpandProperty FullName | Import-Csv | Export-Csv .\merged\merged.csv -NoTypeInformation -Append

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM