# Passing variables with Invoke

**URL:** <https://forums.powershell.org/t/passing-variables-with-invoke/9508>\
**Category:** PowerShell Help\
**Created:** [October 12, 2017, 3:44pm UTC](https://forums.powershell.org/t/passing-variables-with-invoke/9508 "2017-10-12T15:44:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian-clanton](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@brian-clanton](https://forums.powershell.org/u/brian-clanton)\
**Post date:** [October 12, 2017, 3:44pm UTC](https://forums.powershell.org/t/passing-variables-with-invoke/9508/1 "2017-10-12T15:44:16Z")

</div>

I am having problems getting accurate information from a invoke command and I isolated the issue to how I am using the scriptblock with a local variable. I have to use invoke since to retrieve a list of ip addresses, I need to remote to a server that is on the same domain as my terminal servers.

I have a Session created on my domain controller.

```
$TSsession = New-PSSession -ComputerName 10.221.21.2 -Credential $credential
```

This is the correct answer for this particular server’s IP address.

```
(Invoke-Command -Session $TSsession -ScriptBlock {[System.Net.Dns]::GetHostAddresses("w12-r12-01s")}).ipaddresstostring **10.221.20.227**
```

However, i want to loop through an array of servers, but when I have the value of that server saved as a string to a variable, I receive erroneous results where it spits out the name of the domain controller and not the server 'w12-r12-01s.

Value of $TerminalServer, which is a string

```
PS C:\Users\bclanton\Google Drive\Code\PS_Code> $TerminalServer
W12-R12-01S
PS C:\Users\bclanton\Google Drive\Code\PS_Code> $TerminalServer | Get-Member

   TypeName: System.String

Name MemberType Definition                                                                                                                                                       
---- ---------- ----------                                                                                                                                                       
Clone Method System.Object Clone(), System.Object ICloneable.Clone()                                                                                                          
CompareTo Method int CompareTo(System.Object
```

Now when I pass this variable to the same invoke command, I get the wrong IP address as well as the IPV6 address.

```
PS C:\Users\bclanton\Google Drive\Code\PS_Code> (Invoke-Command -Session $TSsession -ScriptBlock {[System.Net.Dns]::GetHostAddresses($TerminalServer)}).ipaddresstostring
fe80::a853:de5a:51:cbc1%12
10.221.21.2
```

Anyone see my error?

---

<div class="post-metadata">

**Author:** ![will-anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/will-anderson/32/234_2.png) [@will-anderson](https://forums.powershell.org/u/will-anderson)\
**Post date:** [October 12, 2017, 4:07pm UTC](https://forums.powershell.org/t/passing-variables-with-invoke/9508/2 "2017-10-12T16:07:53Z")

</div>

Hey there Brian,

Try $using inside a Invoke-Command:

PS C:\Users\bclanton\Google Drive\Code\PS\_Code\> (Invoke-Command -Session $Using:TSsession -ScriptBlock {[System.Net.Dns]::GetHostAddresses($TerminalServer)}).ipaddresstostring

---

<div class="post-metadata">

**Author:** ![jmmurrah](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jmmurrah](https://forums.powershell.org/u/jmmurrah)\
**Post date:** [October 12, 2017, 4:08pm UTC](https://forums.powershell.org/t/passing-variables-with-invoke/9508/3 "2017-10-12T16:08:02Z")

</div>

variables you create on your local machine don’t get passed over to the remote session. You’re effectively passing $Null to the GetHostAddresses method which causes it to return the local IP (local in the remote session if that makes sense).

Couple of different ways to pass that variable, this one should work

```
(Invoke-Command -Session $TSsession -ArgumentList $Terminalserver -ScriptBlock {[System.Net.Dns]::GetHostAddresses($args[0])}).ipaddresstostring
```

You can also do a param() block in your scriptblock code and receive the -argumentlist variables that way, but $args works also

---

<div class="post-metadata">

**Author:** ![will-anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/will-anderson/32/234_2.png) [@will-anderson](https://forums.powershell.org/u/will-anderson)\
**Post date:** [October 12, 2017, 4:23pm UTC](https://forums.powershell.org/t/passing-variables-with-invoke/9508/4 "2017-10-12T16:23:51Z")

</div>

Not true. You can pass variables into an Invoke-Command session. The Using modifier was introduced in PowerShell 3. Refer to example 9 in the following article.

> **[Invoke-Command (Microsoft.PowerShell.Core) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-5.1)**
>
> The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote...

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:37pm UTC](https://forums.powershell.org/t/passing-variables-with-invoke/9508/5 "2024-05-16T20:37:23Z")

</div>


