Invoke-Command -ScriptBlock { $Variables?? }

This topic contains 6 replies, has 5 voices, and was last updated by Profile photo of Peter Jurgens Peter Jurgens 3 weeks, 4 days ago.

  • Author
    Posts
  • #56936
    Profile photo of M ODonnell
    M ODonnell
    Participant

    Hello. I am having a hard time using variables in this one line of code.
    Originally I was just editing the path for each use, but now I want to use variables to change the Param File.

    After & cmd /c is the complete executable path for an installation. This is what I would like to add some variables to.
    Invoke-Command -ComputerName $computer -ScriptBlock { & cmd /c 'c:\temp\wfi\disk1\setup.exe -L0x409 -opt c:\temp\wfi\param1.txt' }

    I've tried combinations of:
    $paramName = 'c:\temp\wfi\param1.txt'
    Invoke-Command -ComputerName $computer -ScriptBlock { & cmd /c 'c:\temp\wfi\disk1\setup.exe -L0x409 -opt ' $paramName }

    Thanks in advance.

  • #56951
    Profile photo of Christian Sandfeld
    Christian Sandfeld
    Participant

    When using Invoke-Command like that, you are running the command in another PowerShell process, and thus it does not know the content of the variable. You can get around that with the 'Using' scope modifier. In your case you would change $paramName in your scriptblock to $Using:paramName.

    As an example ...

    Without $Using:

    $foo = 'World'
    Invoke-Command -ComputerName REMOTEPC -ScriptBlock { "Hello $foo!" }
    

    With $Using:

    $foo = 'World'
    Invoke-Command -ComputerName REMOTEPC -ScriptBlock { "Hello $Using:foo!" }
    

    for more check out 'The Using scope modifier' section of about_Scopes
    https://technet.microsoft.com/en-us/library/hh847849.aspx

  • #56974
    Profile photo of Daan Weda
    Daan Weda
    Participant

    You could use the $using like described above but I believe only if your running PS Version 3 or higher.
    I noticed for myself $using sometimes seems a little bit buggy, so I prefer the old school way with the ArgumentList.
    $paramName = 'c:\temp\wfi\param1.txt'
    Invoke-Command -ComputerName $computer -ScriptBlock {param($paramName)
    & cmd /c 'c:\temp\wfi\disk1\setup.exe -L0x409 -opt ' $paramName
    }-args $paramName

    • #57100
      Profile photo of Peter Jurgens
      Peter Jurgens
      Participant

      Using -ArgumentList on invoke-command is also my personal preference over $using, but I also like to take a more "PowerShell-y" approach to starting a process:

      $paramName = 'c:\temp\wfi\param1.txt'
      Invoke-Command -ComputerName $computer -ScriptBlock {
      param($param)
      Start-Process c:\temp\wfi\disk1\setup.exe ArgumentList "-L0x409 -opt $param"
      } -ArgumentList $paramName
      

      I changed the names of the variables a bit just to point out that it's not necessary to have the same variable names inside and outside of the script block, but if course inside the script block you do need to use the same name when naming the parameter in your param declaration as well as when you reference that value in the rest of the code.

  • #57056
    Profile photo of Jeff Scharfenberg
    Jeff Scharfenberg
    Participant

    I'm trying to do something very close to the same with my sophos install that has been turned into a powershell script from a batch script.

    Here is the part that doesn't seem to pull the credentials from the variable Password that I have set to have the Administrator type in. I'd prefer that the password pull from an AD field, which I can do, but it doesn't seem to pass the variable to the cmd.exe part.

    Would Invoke-Command work here?

    Set-Location \\Chmis3\SophosUpdate\CIDs\S003\SAVSCFXP\
    	cmd.exe Setup.exe -updp "\\Chmis3\SophosUpdate\CIDs\S003\SAVSCFXP" -user hc\sophos -pwd $Password.GetNetworkCredential().password -mng yes
    	}
    	Exit
    
  • #57058
    Profile photo of Jeff Scharfenberg
    Jeff Scharfenberg
    Participant

    I'm trying to do something very close to the same with my sophos install that has been turned into a powershell script from a batch script.

    Here is the part that doesn't seem to pull the credentials from the variable Password that I have set to have the Administrator type in. I'd prefer that the password pull from an AD field, which I can do, but it doesn't seem to pass the variable to the cmd.exe part.

    Would Invoke-Command work here?

    Set-Location \\server\SophosUpdate\CIDs\S003\SAVSCFXP\
    	cmd.exe Setup.exe -updp "\\server\SophosUpdate\CIDs\S003\SAVSCFXP" -user hc\sophos -pwd $Password.GetNetworkCredential().password -mng yes
    	}
    	Exit
    
    • #57098
      Profile photo of Peter Jurgens
      Peter Jurgens
      Participant

      Please create a new thread for your question so we can keep things tidy here. In your question please also include how you're defining the $paasword variable, that should help anyone answering your question.

You must be logged in to reply to this topic.