-
-
Save nordineb/2e30463cfa48a44577ca8973c7898b1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
### Without splatting | |
### | |
Get-Process -Name dns -ComputerName localhost | |
### | |
### With splatting | |
### | |
$myparamas = @{ | |
Name = 'dns' | |
ComputerName = 'localhost' | |
} | |
Get-Process @myparamas | |
### | |
### Using Splatting with $PSBoundParameters | |
### | |
function Get-Foo | |
{ | |
Param | |
( | |
# NOTE: Name maps to the parameter -Name of Get-Process | |
$Name, | |
# NOTE: ComputerName maps to the parameter -ComputerName of Get-Process | |
$ComputerName, | |
# NOTE: Param1 does NOT map to any parameters of Get-Process | |
$Param1 | |
) | |
### Clean up the parameter that does NOT map | |
if ($PSBoundParameters.ContainsKey('Param1')) | |
{ | |
$PSBoundParameters.Remove('Param1') | |
} | |
### Splat away! | |
Get-Process @PSBoundParameters | |
} | |
### Demo | |
Get-Foo -Name dns -ComputerName localhost -Param1 baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment