Local functions and remote sessions

The following example executes a function created on the local machine in a remote system using positional arguments:

function Get-FreeSpace { 
    param ( 
        [Parameter(Mandatory = $true)] 
        [String]$Name 
    ) 
 
    [Math]::Round((Get-PSDrive $Name).Free / 1GB, 2) 
} 
Invoke-Command ${function:Get-FreeSpace} -Session $session -ArgumentList c 

This technique succeeds because the body of the function is declared as a script block. ArgumentList is used to pass a positional argument into the DriveLetter parameter.

If the function depends on other locally defined functions, the attempt will fail.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset