Using splatting with ArgumentList

The ArgumentList parameter of Invoke-Command does not offer a means of passing named arguments to a command.

The following example uses splatting to pass parameters. The function is defined on the local system, and the definition of the function is passed to the remote system:

# A function which exists on the current system 
function Get-FreeSpace { 
    param ( 
        [Parameter(Mandatory = $true)] 
        [String]$Name 
    ) 
 
    [Math]::Round((Get-PSDrive $Name).Free / 1GB, 2) 
} 
 
# Define parameters to pass to the function 
$params = @{ 
    Name = 'c' 
} 
 
# Execute the function with a named set of parameters 
Invoke-Command -ScriptBlock { 
    param ( $definition, $params ) 
 
    & ([ScriptBlock]::Create($definition)) @params
} -ArgumentList ${function:Get-FreeSpace}, $params -ComputerName $computerName 

In the preceding example, the definition of the Get-FreeSpace function is passed as an argument along with the requested parameters. The script block used with Invoke-Command converts the definition into a ScriptBlock and executes it.

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

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