February 2019
Intermediate to advanced
626 pages
15h 51m
English
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}, ...Read now
Unlock full access