英文:
Powershell - Is there a way to use $_ for two different objects that are piped one after another
问题
我正在尝试
foreach ($vc in $vclist) {
Connect-Viserver $vc -Credential (Get-Credential)
$vms = Get-VM
$vmsView = $vms | Get-View | Select-Object @{N='HWVersion'; E={$_.Guest.HwVersion}}
}
$_.Guest.HwVersion
属于 Get-View
的数组操作
是否有一种方法/方式可以从 $vms
数组中获取变量,例如我想要 $vms[currentObject].Guest.VmId
我没有尝试很多,但我不想循环遍历所有的虚拟机,我只想在一行代码中从 $vms
数组和 Get-View
数组中选择对象,并返回给 $vmsView
。
英文:
I am trying
foreach ($vc in $vclist) {
Connect-Viserver $vc -Credentiol (get-Credentiol)
$vms = Get-VM
$vmsView = $vms | Get-View | Select-Object @{N = 'HWVersion'; E = { $_.Guest.HwVersion } }
}
$_.Guest.HwVersion
belongs to array op of get-view
is there a method/way I can also get variable from $vms
array for example I want $vms[currentObject].guest.VmId
I did not have much to try but i do not want to loop all vms and want it in just want line where I can select-Object
's from $vms
array and Get-View
array and return to $vmsView
答案1
得分: 2
您可以结合使用常见的-PipelineVariable
参数和Write-Output
命令来实现:
foreach ($vc in $vclist) {
Connect-Viserver $vc -Credential (Get-Credential)
$vms = Get-VM
$vmsView =
# 注意使用Write-Output和-PipelineVariable vm的方式,
# 它将$vms中的每个元素存储在自定义变量$vm中,
# 并将其输出到管道中,以便后续的脚本块可以引用它。
Write-Output $vms -PipelineVariable vm |
Get-View |
Select-Object @{N = 'VmId'; E = { $vm.VMId } },
@{N = 'HWVersion'; E = { $_.Guest.HwVersion } }
}
请注意,在这种情况下,所选择的自定义目标变量$vm
必须在-PipelineVariable
中传递时不带$
,即作为-PipelineVariable vm
。
正如iRon指出的,您可以通过在管道中直接使用Get-VM
命令来简化您的命令,将Write-Output $vms -PipelineVariable vm
替换为Get-VM -PipelineVariable vm
。
英文:
<!-- language-all: sh -->
You can use the common -PipelineVariable
parameter in combination with a Write-Output
call:
foreach ($vc in $vclist) {
Connect-Viserver $vc -Credentiol (get-Credentiol)
$vms = Get-VM
$vmsView =
# Note the use of Write-Output and -PipelineVariable vm,
# which stores each element of $vms in self-chosen variable $vm
# as it is being output to the pipeline, so that later script blocks
# can reference it.
Write-Output $vms -PipelineVariable vm |
Get-View |
Select-Object @{N = 'VmId'; E = { $vm.VMId } },
@{N = 'HWVersion'; E = { $_.Guest.HwVersion } }
}
Note that the self-chosen target variable -$vm
in this case - must be passed
without $
to -PipelineVariable
, i.e. as -PipelineVariable vm
As iRon points out, you can simplify your command by using Get-VM
directly in your pipeline, by replacing Write-Output $vms -PipelineVariable vm
with Get-VM -PipelineVariable vm
答案2
得分: 1
最简单的方法可能是使用ForEach-Object
将调用Get-View
包装起来:
$vmsView = $vms | ForEach-Object {
$vm = $_
$vm | Get-View | Select-Object @{N = 'HWVersion'; E = { $_.Guest.HwVersion } }, @{N='PowerState'; E={$vm.PowerState}}
}
请注意,这是一段PowerShell代码,用于获取虚拟机的视图信息,并选择其中的HWVersion
和PowerState
属性。
英文:
The easiest way is probably to use ForEach-Object
to wrap the calls to Get-View
:
$vmsView = $vms |ForEach-Object {
$vm = $_
$vm |Get-View | Select-Object @{N = 'HWVersion'; E = { $_.Guest.HwVersion } },@{N='PowerState';E={$vm.PowerState}}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论