System.Management.Automation.Credential() in the Param block

Welcome Forums General PowerShell Q&A System.Management.Automation.Credential() in the Param block

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
8 months ago.

  • Author
    Posts
  • #95814

    Participant
    Points: 21
    Rank: Member

    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

  • #95847

    Participant
    Points: 4
    Rank: Member

    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.

  • #95852

    Participant
    Points: 207
    Helping Hand
    Rank: Participant

    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'

  • #95978

    Participant
    Points: 21
    Rank: Member

    Thank you very much.

The topic ‘System.Management.Automation.Credential() in the Param block’ is closed to new replies.