PowerShell, Remoting, Scripting
comments 3

Executing script existing only on a remote system using Invoke-Command

This is more of a personal note and may sound trivial to many of you out there. But just wanted to make sure I share it here. A while back, in my remoting series, I wrote about executing commands / scripts on a remote machine using Invoke-Command cmdlet. Talking specifically about scripts, you can use -FilePath parameter to execute scripts on a remote machine.

Note that the script you provide as a value to -FilePath must exist on the local machine or at a place accessible to the local machine. So, what if you want to run a script that exists only on the remote server? You can use script block for that.

Invoke-Command -ComputerName SP2010-WFE -scriptBlock { C:\scripts\Test.PS1 }

This way, you can execute the script present on a remote machine but not on the local system.

I’ve published a free book on PowerShell 2.0 remoting. You can download it at:

[download id=”22″ format=”4″]

Filed under: PowerShell, Remoting, Scripting

by

Ravikanth is a principal engineer and the lead architect for Microsoft and VMware virtualized and hybrid cloud solutions within the Infrastructure Solutions Group at Dell EMC. He is a multi-year recipient of Microsoft Most Valuable Professional (MVP) award in Windows PowerShell (CDM) and Microsoft Azure. Ravikanth is the author of Windows PowerShell Desired State Configuration Revealed (Apress) and leads Bangalore PowerShell and Bangalore IT Pro user groups. He can be seen speaking regularly at local user group events and conferences in India and abroad.