简体   繁体   中英

Invoke-Command with Computers array, access single array element inside scriptblock

I am wondering how to accomplish the following and hope you can help:

I want to execute several tasks on different computers by providing a array of computernames using

Invoke-Command -ComputerName $ComputerNameArray -ScriptBlock { ...}

Inside the scriptblock, I want to access the current ComputerName (not the whole array).

How is this possible?

I tried enclosing the Invoke-Command within a foreach loop, something like this:

foreach ($Computer in $ComputerNameArray)
{
 Invoke-Command-ComputerName $Computer -ScriptBlock { ...}
}

which works, this way I can access the current ComputerName with $env:Computer but as the foreach isn't necessary here, I want to get rid of it.

Any ideas?

Thanks all!

这对我有用:

Invoke-Command $ComputerNameArray { $env:computername }

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