Powershell – Is there a way to use $_ for two different objects that are piped one after another

huangapple go评论79阅读模式
英文:

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 = &#39;VmId&#39;; E = { $vm.VMId } },
                    @{N = &#39;HWVersion&#39;; 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代码,用于获取虚拟机的视图信息,并选择其中的HWVersionPowerState属性。

英文:

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 = &#39;HWVersion&#39;; E = { $_.Guest.HwVersion } },@{N=&#39;PowerState&#39;;E={$vm.PowerState}}
}

huangapple
  • 本文由 发表于 2023年7月27日 17:54:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定