Run Local Functions Remotely in PowerShell

Run Local Functions Remotely in PowerShell

Have you ever had functions loaded into your local PowerShell session and needed to run them on a remote system? The typical solution to this problem is to copy the code to the remote system and then load the functions on the remote system to use them. What if I told you it is possible to run functions you have stored on your local machine and execute them remotely with Invoke-Command? This post will teach you how to do this.

Execute Local Functions Remotely

To demonstrate how to do this, I’ll use a famous Hell-World example. I have written an extremely simple function that writes “Hello, World!” to the console. It is called MyFunction and has no parameters. I’ve loaded to into my local PowerShell session and to run it remotely I will use Invoke-Command. Here is the trick, place a $ symbol out side the curly braces of the scriptblock. Then put Function: inside the scriptblock before the name of the function as seen below.


MyFunction

Using Parameters

While the above example works, most functions aren’t that simple. Most require several parameters and how do you pass those parameters to the function running remotely? To illustrate, I’ve written another simple function called Get-NetConfig. Provided an InterfaceIndex and AddressFamily parameter, it will gather information about the network settings of the system it runs on. In the below example I am passing the values for the InterfaceIndex and AddressFamily by using the -ArgumentList parameter of Invoke-Command.


Get-NetConfig

Changing Parameter Order

Often you do not specify the parameters in the order they are listed in the function. This is a problem, because executing function remotely requires they be in the exact same order. This means that if I provided AddressFamily first and IPAddress second in the above example it would error out. There are ways around this of course. Method 1 would be to set default values for all the parameters, but that’s not an ideal solution. Method 2 is setting the position of the parameter to the order you want. In the below code I’m giving AddressFamily the first position by specify 0 and assigning position 1, the second position to AddressFamily.


PositionalParams

If the function you’re calling in the ScriptBlock of Invoke-Command calls another function within it? It says “The term <function name=””> is not recognized as a cmdlet or function…”

The reason you get that error is because that cmdlet isn’t available on the remote system because it wasn’t loaded in. The two options I see are one load in that module within the scriptblock or use implicit remoting. That however is best used with modules but you could easily create a module file with both functions in it. Here’s an article on it.

Source

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scripting
Install Vmware PowerCLI

PowerCLI is really easy to install now. From an Administrative Powershell window just enter: Install-Module -Name VMware.PowerCLI Minimum Powershell Version must be v3.   Offline Install of PowerCLI Accessing the PowerCLI Modules We’re now ready to download the PowerCLI modules. This task will require a system with internet access. This …

Scripting
Pass credentials with Powershell – 3 Ways

Interactive Here’s your typical scenario.   You have a script that requires credentials internally.  So to provide those credentials you would do something like $MyCredentials=GET-CREDENTIAL –credential “CONTOSO\Username” and you of course see a box like this normally on the screen Then you would type in the password and life would go …

Deployment
MDT – Joining a Computer to a Domain Using PowerShell

Using PowerShell scripts within a task sequence provides more flexibility than using the CustomSettings.ini file to join a computer to a domain. The parameters of the CustomSettings.ini file are common to any deployment you perform. That’s why creating a custom PowerShell script to join your domain will be customize to …