Last active
August 29, 2015 14:20
-
-
Save toenuff/0c24232727bd953ff9ce to your computer and use it in GitHub Desktop.
Example of putting -Credential in function
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
$class = new-object psobject @{credential=$null} | |
function samplecred { | |
param( | |
[Parameter(Mandatory=$false)] | |
[ValidateScript({("String","PSCredential") -contains $_.gettype().name})] | |
[Object] $Credential | |
) | |
if ($Credential) { | |
switch ($Credential.gettype().name) { | |
"String" { | |
$class.credential = Get-Credential $credential | |
} | |
"PSCredential" { | |
$class.credential = $credential | |
} | |
} | |
} | |
if (!$class.credential) { | |
$class.credential = Get-Credential | |
} | |
$class.credential | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an example of my credential parameter. If the credential argument is used, it sets the credential on the underlying object being used (generally this is encapsulated in a module). Otherwise, it gets a new cred using get-credential.