Invoke-Command -Session $Session question on passing in arguments

Welcome Forums General PowerShell Q&A Invoke-Command -Session $Session question on passing in arguments

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

 
Participant
2 months, 3 weeks ago.

  • Author
    Posts
  • #158102

    Participant
    Topics: 2
    Replies: 9
    Points: 21
    Rank: Member

    Anyone got a second to help me out? Thank you in advance.

    Trying to run a setup.exe with arguments on a remote server. I have it working locally, and I have it working when I strictly reference everything in Invoke-Command in the session. But when I start trying to pass in what I want with Arguments it doesn't actually execute on the remote server.

    Invoke-Command -Session $session -ScriptBlock { C:\scripts\MRTK-05-2019\setup.exe -s -k"11111-1111-11111" -xT -oT -pT -fT -hT -vT /f2"C:\scripts\setup.log" }

    This works ^ ^ ^

    Invoke-Command -Session $session -ScriptBlock { param($p1) $p1 } -ArgumentList $fullinstallparams -Verbose 

    This doesn't ^ ^ ^

    Invoke-Command -Session $session -ScriptBlock { param($p1,$p2) $p1 $p2 } -ArgumentList $setupexe, $installparams -Verbose 

    Doesn't work either ^ ^ ^

    Invoke-Command -Session $session -ScriptBlock { param($p1) & $p1 } -ArgumentList $fullinstallparams -Verbose 

    This actually errors out ^ ^ ^

    If you want to see how I'm constructing all the variables

    $filesharefolder = "\\fileshare\MRTK-05-2019"
    $localscriptspath = "C:\scripts"
    $foldername = Split-Path $filesharefolder -Leaf
    $localfolderpath = $localscriptspath + "\" + $foldername
    $setupexe = $localfolderpath + "\" + "setup.exe"
    $license = "11111-1111-11111"
    $installparams = " -s -k`"$license`" -xT -oT -pT -fT -hT -vT /f2`"C:\scripts\$foldername\setup.log`""
    $session = New-PSSession -ComputerName $ComputerName
    $fullinstallparams = $setupexe + $installparams

    I made sure $installparams constructs to what I want – I had to use some escape's in there which is not something I'm totally comfortable with but it does seem to work when I put everything together. Is it doing that properly on the remote server? I don't know how I'd find out but it works locally.

    I've tried skinning this thing like 8 different ways but I finally got it working by using Invoke-Command with a session and just typing everything out. Unfortunately thats only half the battle and ideally I'd be able to pass in all the info through variables that way I can get this function working without having to change code every time the software updates (every month or so.)

    Thanks in advance. Let me know if you need anymore detail.

  • #158114

    Senior Moderator
    Topics: 8
    Replies: 1009
    Points: 3,281
    Helping Hand
    Rank: Community Hero

    You could do it like below.

    An example.

    $p1 = 'cmd.exe'
    $arg1 = '/c'
    $arg2 = 'ipconfig'
    $arg3 = '/all'
    Invoke-Command -Session $session -ScriptBlock { param($p1,$arg1,$arg2,$arg3) & $p1 $arg1 $arg2 $arg3 } -ArgumentList $exec,$arg1,$arg2,$arg3 -Verbose 
    
  • #158115

    Participant
    Topics: 2
    Replies: 9
    Points: 21
    Rank: Member

    You could do it like below.

    An example.

    PowerShell
    6 lines

    1
    2
    3
    4
    5
    6
    $p1 = 'cmd.exe'
    $arg1 = '/c'
    $arg2 = 'ipconfig'
    $arg3 = '/all'
    Invoke-Command Session $session ScriptBlock { param($p1,$arg1,$arg2,$arg3) & $p1 $arg1 $arg2 $arg3 } ArgumentList $exec,$arg1,$arg2,$arg3 Verbose
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    I'm going to give this a shot and report back shortly. Thank you for the tip

  • #158123

    Participant
    Topics: 2
    Replies: 9
    Points: 21
    Rank: Member

    Here is what I ended up with. No luck.

    $arg1 = "-s"
    $arg2 = "-k`"$license`""
    $arg3 = "-xT"
    $arg4 = "-oT"
    $arg5 = "-pT"
    $arg6 = "-fT"
    $arg7 = "-hT"
    $arg8 = "-vT"
    $arg9 = "/f2`"C:\scripts\$foldername\setup.log`""
    
    $setupexe = C:\scripts\MRTK-05-2019\setup.exe
    
    Invoke-Command -Session $session -ScriptBlock { param($p0,$p1,$p2,$p3,$p4,$p5,$p6,$p7,$p8,$p9) & $p0 $p1 $p2 $p3 $p4 $p5 $p6 $p7 $p8 $p9 } -ArgumentList $setupexe,$arg1,$arg2,$arg3,$arg4,$arg5,$arg6,$arg7,$arg8,$arg9 -Verbose
    
    
  • #158153

    Participant
    Topics: 2
    Replies: 466
    Points: 1,058
    Helping Hand
    Rank: Community Hero

    That amount of splitting most likely isn't necessary.  All you should normally need is 1. the executable name or path, and 2. the arguments. Also, it's a lot easier to bypass doing your own `-ArgumentList` via the `$using:variable` method.

    Also, that most recent example you're actually unintentionally calling the executable directly and storing its output... and then trying to invoke whatever string the executable might output in your local command line. Bare strings that are file paths can frequently end up being invoked (unless they are being used as arguments directly). You need to enclose that string to avoid calling the executable itself and instead pass along the path to the file.

    I also opted to use a literal string with the `-f` operator to insert the variables. Command-line argument strings can get a bit messy with how you need to handle quotes, so I tend to find those easier to work with.

    $CommandArguments = '-s -k "{0}" -xT -oT -pT -fT -hT -vT /f2 "C:\scripts\{1}\setup.log"' -f $license, $foldername
    $SetupExePath = "C:\scripts\MRTK-05-2019\setup.exe"
    
    Invoke-Command -Session $session -Verbose -ScriptBlock {
        & $using:SetupExePath $using:CommandArguments
    }
    • #158256

      Participant
      Topics: 2
      Replies: 9
      Points: 21
      Rank: Member

      Oh man this is awesome stuff – really appreciate you sharing. No success for me yet at passing parameters to the setup.exe though.

      I've tried both these methods below and I can see setup.exe kickoff on the remote server but its just sitting there. I've seen this behavior in the past for silent installs if its expecting something and not getting it. So it just sits there permanently until I kill it. More importantly the setup.log doesn't get created so its not really progressing through the install. Which is different than when I just pass everything in without variables. So that's the key to understanding this whole thing – I'm just not sure how to troubleshoot it further.

      
      $setupexe = '{0}\setup.exe' -f $localfolderpath
      $installparams = ' -s -k{0} -xT -oT -pT -fT -hT -vT /f2"C:\scripts\{1}\setup.log"' -f $license, $foldername
      
      Invoke-Command -Session $session -Verbose -ScriptBlock {
      & $using:setupexe $using:installparams}
      
      
      
      $setupexe = '{0}\setup.exe' -f $localfolderpath
      $installparams = ' -s -k{0} -xT -oT -pT -fT -hT -vT /f2"C:\scripts\{1}\setup.log"' -f $license, $foldername
      
      $fullinstallparams = $setupexe + $installparams
      
      Invoke-Command -Session $session -Verbose -ScriptBlock {
      $using:fullinstallparams
      }
      
      

You must be logged in to reply to this topic.