locked
PS Remote - Carrying variables to remote machine RRS feed

  • Question

  • Hi all,

    Wondering if someone could help me. I am trying to update trend across multiple servers using PS remoting however, I am having issues carrying the variable across. The update works on the machine I am running the script from but it does not update the other remote machines.

    #Credentians for remote sessions
    	$Cred = "USERNAME"
    
    #Path To Trend
    	$TrendPath = "c:\"; cd "C:\Program Files (x86)\Trend Micro\OfficeScan Client"
    
    #Update Command
    	$update = ./pccntmon -u
    
    #Creating Remote PS Session on Servers
    	$RemoteServersSessions = New-PSSession -computername '10.2.34.34', '10.2.34.35' -Credential $Cred
    
    	Write-Warning "Open Remote PS Sessions"
    	$RemoteServersSessions
    
    #Invoking Remote Command to change path and update trend
    #Invoke-Command -Session $RemoteServersSessions -ScriptBlock{$TrendPath; $update}
    	Invoke-Command -Session $RemoteServersSessions -ScriptBlock { $Using:TrendPath + $Using:update }
    
    	Write-Warning "Trend on servers should now be updated"
    
    #Disconnect PS remote sessions
    	Write-Warning "Disconnecting remote Sessions"
    	Disconnect-PSSession -Session $RemoteServersSessions

    Wednesday, August 23, 2017 8:17 AM

All replies

  • You don't need a session to do this kind of remoting.  Just use the system names.

    Don't build the command.  Just place it into the script block.

    This is all you need:


    $sb = { & 'C:\Program Files (x86)\Trend Micro\OfficeScan Client/pccntmon -u c:\' } Invoke-Command -ScriptBlock $sb -computername '10.2.34.34', '10.2.34.35' -Credential 'USERNAME'


    \_(ツ)_/




    • Edited by jrv Wednesday, August 23, 2017 8:38 AM
    Wednesday, August 23, 2017 8:37 AM
  • to asnwer your original question here is how to pass variables to remote machines while using invoke-command

    $TrendPath = "c:\"; cd "C:\Program Files (x86)\Trend Micro\OfficeScan Client"

    Invoke-Command -ComputerName machine -ScriptBlock { param($ABC) command $abc} -Args $TrendPath


    This will result inserting $trendpath to $abc variable that will be interpreted on the remote machine.

    hope it helps


    --------------------- Leos

    • Proposed as answer by Leoš Marek Wednesday, August 23, 2017 1:12 PM
    Wednesday, August 23, 2017 12:25 PM
  • The mixed between jrv's version and Leos :

    #requires -Version 2.0
    $command = 'C:\Program Files (x86)\Trend Micro\OfficeScan Client/pccntmon -u'
    $computers = 'X.X.X.X', 'X.X.X.X'
    $credential = Get-Credential
    Invoke-Command -ComputerName $computers -Credential $credential -ScriptBlock {
      Param(    
        [Parameter(Mandatory=$true)]
        [string]$command
      )
      
      & $command
    } -ArgumentList $command
    In conclusion you will have the same result.


    The key of learning is practice.

    Wednesday, August 23, 2017 2:02 PM