简体   繁体   中英

powershell foreach name execute invoke-command

I have the folowing code that outpusts names:

foreach ($comp in $maschines.name) { $comp }

but the folowing does not work, why?

foreach ($comp in $maschines.name) { invoke-command -computer comp1 -ScriptBlock { get-vm –VMName $comp | Select-Object VMId | Get-VHD | ft path } }

try the following:

 foreach ($comp in $maschines.name) { invoke-command -computer comp1 -ScriptBlock { get-vm –VMName $using:comp | Select-Object VMId | Get-VHD | ft path } }

added the using: to your scriptblock to make use of that local variable you want to use on that other computer.

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