Answer

How can I use Powershell remoting to pass info to remote computers?

When using PowerShell remoting, it's important to differentiate between locally executed code and remotely executed code. If you need to execute something locally and then pass that along to one or more remote computers, there's a specific way to do so -- and it isn't always obvious.

For example, suppose you've created a credential object and want to pass that along to the remote computer to use in a command:

    $cred = Get-Credential

You would do this by creating a parameter block in the remote command script block. Then, specify the data to be passed in the –ArgumentList parameter:

    Invoke-Command –ScriptBlock (param($c) Add-Computer –Credential $c }
    -ComputerName

    Requires Free Membership to View

SERVER2 –ArgumentList $cred

The items given to –ArgumentList, which should be in a comma-separated list, are substituted by position into the Param() block. The first –ArgumentList item goes to the first Param() variable, the second goes to the second, and so on. The Param() variables can then be used inside the script block, just like any other variable.

This technique is shown in the built-in help for Invoke-Command. Here's another technique:

    Invoke-Command –FilePath MyScript.ps1 –ComputerName SERVER2,SERVER2
    -ArgumentList $this,$that

The assumption here is that MyScript.ps1 internally defines a Param() block with two parameters -- $this will be sent to the first parameter, and $that will be sent to the second. It's basically the same technique as using a –ScriptBlock, but everything is defined in the script file instead.

About the author
Don Jones is a well-known and respected PowerShell expert and educator. He's co-author of three books on PowerShell (see 
PowerShellBooks.com for a list). You can find his content online, including his PowerShell Q&A forums, by visiting DonJones.com.

This was first published in February 2013

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: