Skip to content

Instantly share code, notes, and snippets.

@toenuff
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toenuff/0c24232727bd953ff9ce to your computer and use it in GitHub Desktop.
Save toenuff/0c24232727bd953ff9ce to your computer and use it in GitHub Desktop.
Example of putting -Credential in function
$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
}
@toenuff
Copy link
Author

toenuff commented May 4, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment