简体   繁体   English

PowerCLI获取满足某些条件的VM

[英]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.

相关问题 如何在Horizo​​n View PowerCLI中创建新的VM? - How to create new VMs in Horizon View PowerCLI? PowerCLI:使用多个 vCenter 服务器上的虚拟机 - PowerCLI: Work with VMs on multiple vCenter servers 无需连接到vCenter的powercli访问VM - powercli access VMs without connecting to a vCenter VMWare vSphere Powercli Get-Stat在某些主机上不起作用 - VMWare vSphere Powercli Get-Stat does not work on some host 在循环通过 powershell 中的 Azure 虚拟机时,我只想获得具有特定标签的虚拟机 - While looping through Azure VMs in powershell I want to get only VMs which have specific tag available in those 所有VM PowerCLI信息输出/控制台和自定义对象均无法正常工作 - All VMs PowerCLI Info output /Console and Custom Object Not Working Properly 如何使用PowerCLI在VM和模板视图中移动VM? - How to move a VM within VMs and Templates view using PowerCLI? 如何使用PowerShell(PowerCLI)确定VM自定义规范状态? - How to determine a VMs customization specification state with PowerShell (PowerCLI)? 如何在PowerCLI中快速找到具有串行端口的VM - How can I quickly find VMs with serial ports in PowerCLI 如何使用powercli并行移动多个VM? - How can I move multiple VMs in parallel using powercli?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM