Unable to pass variable to invoke-sshcommand

Welcome Forums General PowerShell Q&A Unable to pass variable to invoke-sshcommand

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

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #105281

    Participant
    Points: 0
    Rank: Member

    Hello People

    I'm trying to pass local variable to execute a command in Linux OS, it is not working. Could you help me what did I miss?

    $Path = "/na/testy/vol/home_01/home/" + "$AccName"
    write-host "Creating home directory for $AccName"
    Try
    {
    $result = $null
    $ScriptBlock = {
    param ($Path)
    sudo mkdir $Path }
    $result = Invoke-SSHcommand -ComputerName $ComputerName -ScriptBlock $ScriptBlock -ArgumentList $Path
    }

  • #105287

    Participant
    Points: 217
    Helping Hand
    Rank: Participant

    You cannot, by design use a local variable in a remote call without ensuing the scope meets that use case.
    Take a look at the about_Scopes help information and the way to use the 'Using' option.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-6

    Specifically the section on 'Using', which leads you too 'Remote Variables'
    'docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-6'

    All the above comes down to, this...

    USING REMOTE VARIABLES
    Windows PowerShell assumes that the variables used in remote commands are defined in the session in which the command runs.

    In the following example, the $ps variable is defined in the temporary session in which the Get-WinEvent command runs.

    USING LOCAL VARIABLES
    You can also use local variables in remote commands, but you must indicate that the variable is defined in the local session.
    Beginning in Windows PowerShell 3.0, you can use the Using scope modifier to identify a local variable in a remote command.
    The syntax of Using is as follows:

    $Using:SomeVariableName
  • #105436

    Participant
    Points: 0
    Rank: Member

    Did this resolve your issue friend?

The topic ‘Unable to pass variable to invoke-sshcommand’ is closed to new replies.