Author Posts

March 13, 2018 at 3:43 pm

Good day.
i have this code that i downloaded from GitHub.
i don't understand the meaning of line 15.
its not a method, its not a type. What is it?

Function Test-Credential {
    [OutputType([Bool])]
    
    Param (
        [Parameter(
            Mandatory = $true,
            ValueFromPipeLine = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [Alias(
            'PSCredential'
        )]
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential,

        [Parameter()]
        [String]
        $Domain = $Credential.GetNetworkCredential().Domain
    )

    Begin {
...

Regards
Kamil

March 13, 2018 at 10:04 pm

I'd not seen this before either. Here's a good post that explains how to add credentials to PowerShell functions:

http://duffney.io/AddCredentialsToPowerShellFunctions

The purpose of Line 15 is to allow the user to pass in a username as a string; they will then receive a prompt to enter a password.

March 13, 2018 at 11:19 pm

I have seen this used, though I personally have not needed it. Yet, this has been a use for a while now for some.
See this discussion from an archive of stuff I keep around.

How to convert parameter type into a different object type

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.

param(
    [System.Management.Automation.Credential()]
    $Credential=[System.Management.Automation.PSCredential]::Empty
)

When the Credential parameter is PSCredential, you cannot pass a string to it: get-something -cred adi, you'll get a ' Cannot process argument transformation' error. using the 'Credential()' transformation solves this. Now you can do 'get-something -cred adi' and the have the credential dialog invoked and populated with the username.

'stackoverflow.com/questions/10036271/how-to-convert-parameter-type-into-a-different-object-type'

March 14, 2018 at 8:28 pm

Thank you very much.