[英]PowerCLI Get VMs those fit some conditions
I'm trying to get our some Linux distros from vCenter by using PowerCLI. 我正在尝试使用PowerCLI从vCenter获得一些Linux发行版。 But I don't want to get Appliance VMs.
但是我不想获得设备虚拟机。 So I have 2 different successful PowerCLI scripts those can find these machines.
因此,我有2种成功的PowerCLI脚本,它们可以找到这些机器。 I want merge these scripts but I'm new on PowerCLI and it's syntax.
我想合并这些脚本,但是我是PowerCLI的新手,它是语法。
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: Linux列表:
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. 也许这是一个XY问题。 If you have a better way to get Linux VMs those are not appliance, you can say me this method.
如果您有更好的方法来获取不是设备的Linux VM,则可以说这种方法。
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. 现在,您可以根据需要将LinuxVMs变量传递到Export-Csv cmdlet中,并在第二个脚本中引用它。
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 您可以将每个脚本块放在一个脚本中,然后使用此处的一种方法合并生成的csv: 使用PowerShell将多个CSV文件合并为一个
Stinkyfriend's code: 臭友的代码:
Get-ChildItem -Filter *.csv | Select-Object -ExpandProperty FullName | Import-Csv | Export-Csv .\merged\merged.csv -NoTypeInformation -Append
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.