Search code examples
powershellpowershell-2.0powershell-3.0powershell-remoting

Powershell tasks from local machine to remote machine


I am performing below tasks on remote machine from a local machine:

  • Creating/Deleting/Modifying some directory
  • Copying some folder from local to remote machine
  • Installing some .exe silently with noninteractive option
  • Exectuing some batch files

I want to write a script in PowerShell. Novice to PowerShell. I have done some basic investigation of terms like "PowerShell Remoting" etc.

What are the things I need to look for? Related exmple for this will help, where should I look for those?


Solution

  • Reading from docs on MSDN:

    To run a single command on a remote computer, use the ComputerName parameter. To run a series of related commands that share data, use the New-PSSession cmdlet to create a PSSession (a persistent connection) on the remote computer, and then use the Session parameter of Invoke-Command to run the command in the PSSession. To run a command in a disconnected session, use the InDisconnectedSession parameter. To run a command in a background job, use the AsJob parameter.

    So basically you should do something like:

    $session = New-PSSession    
    Invoke-Command -Session $session -FilePath <PathToScript>