I’m doing some scripting in PowerShell, and I was wondering if there’s a way to “declare” a parameter “X” the same way parameter “-Credential” is declared, for example in Get-WMIObject cmdlet.
Let me be more specific. The Credential parameter in almost all cmdlets is a PSCredential Object. But, the argument can be either a PSCredential Object or, a String Object with the username.
[CmdletBinding()]
param ([Parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential]
$Credential)
The problem comes when passing a string. Of course, an argument transformation on the parameter cannot be done. Cannot convert the type “System.String” into a type PSCrendential.
Give this a try:
As to parameter argument transformation, check this awesome script:
http://poshcode.org/3024