Checking if credential was provided

Welcome Forums General PowerShell Q&A Checking if credential was provided

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

 
Participant
1 year, 6 months ago.

  • Author
    Posts
  • #72661

    Participant
    Points: 1
    Rank: Member

    The following code checks if someone using a function; has entered a saved PScredential, if the function needs to prompt the user for a password, or just uses the users current account. Is there an easier way?

        Write-Verbose "Checking Credential Param"
        if ($Credential.Count -ne 0)
        {       
            if ($Credential.ToString() -contains 'System.Management.Automation.PSCredential')
            {
                $PSCredential = $Credential           
            }
                else
                {
                    Write-Verbose "Getting Credential"
                    $PSCredential = (Get-Credential -Credential $Credential)       
                } 
        }
    
  • #72662

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    You could check $PSBoundParameters.ContainsKey('parameterName') to see if the parameter was specified. You could also code the parameter definition to accept objects of the type [PSCredential], and mark it as mandatory. That way if a credential isn't specified, the shell will prompt for one.

  • #72664

    Participant
    Points: 1
    Rank: Member

    Thank You... the solution I had took hours to figure out. Seeing other solutions then your own really helps. This forum is great for learning! Never ending unique practice questions.

The topic ‘Checking if credential was provided’ is closed to new replies.