Passing variables with Invoke

Welcome Forums General PowerShell Q&A Passing variables with Invoke

This topic contains 3 replies, has 3 voices, and was last updated by

1 year, 4 months ago.

  • Author
  • #81943

    Points: 55
    Rank: Member

    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 -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

    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
    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

    Anyone see my error?

  • #81947

    Points: 91
    Team Member
    Rank: Member

    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

  • #81949

    Points: 1
    Rank: Member

    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

The topic ‘Passing variables with Invoke’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort