Created
June 1, 2016 03:03
-
-
Save craig-martin/8054b7d232319dc2bff3dc71b841ea06 to your computer and use it in GitHub Desktop.
This file contains 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