locked
Powershell 3.0 pops up Credential Window asking for Username and password with parameter type set as PSCredential for Credential paramater RRS feed

  • Question

  • Hi 

    Client m/n is installed with Windows 7 SP1 ( with .NET 4.6.1) Exclusively installed . Power shell version is 3.0.

    PSScriptAnalyzer flagged PSUsePSCredentialType with Description as below :

    The Credential parameter in '***' must be of type PSCredential For PowerShell 4.0 and earlier. please define a credential transformation attribute e.g.  [System.Management.Automation.Credential()]    

    To overcome this I have used PSCredential Type in Parameter as below :

    [parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)][PSCredential] 
            [System.Management.Automation.CredentialAttribute()]$Credential,

    Also to Mention , Cmdlet implementation makes use of Username and Password in our environment . when the Cmdlet is executed using Username and Password , Credential pop-up is thrown asking for username and password again in Powershell 3.0( When we press Escape key , Cmdlet is executed with expected result)

    Could any one tell me how to get suppress this Credential pop-up ( when username and password is already provded) in the Cmdlet. This is happening in Powershell 3.0

    Awaiting a quick response

    Regards

    Seshadri -

    Monday, August 8, 2016 9:27 AM

Answers

  • That is not how it works.  You must pass an account or a credential object:

    credtest -Credentail domain/user

    or

    $cred = Get-Credentail
    credtest -Credential $cred

    You cannot just pass arbitrary arguments and have PowerShell figure it out.


    \_(ツ)_/

    • Proposed as answer by Hello_2018 Tuesday, August 16, 2016 7:07 AM
    • Marked as answer by Mike Laughlin Friday, September 2, 2016 12:58 PM
    Tuesday, August 9, 2016 11:03 AM

All replies

  • It is not really possible o understand what you are doing to get the failure.  The following work as expected for me.

    function credtest {
    	param(
    		[Parameter()]
    		[PSCredential]
    		[System.Management.Automation.CredentialAttribute()]
    		$Credential
    	)
    }
    
    $cred = Get-Credential test
    credtest -Credential $cred


    \_(ツ)_/



    • Edited by jrv Monday, August 8, 2016 10:12 AM
    Monday, August 8, 2016 10:11 AM
  • Monday, August 8, 2016 10:23 AM


  • Hi 

    Thanks for Replying . Let me brief you about the problem

    I am implementing the Function  ( what you`ve implemented) except for the fact that we have additional parameters like Username and Password

    Function credtest {
    param(
    [Parameter()]
    [PSCredential]
    [System.Management.Automation.CredentialAttribute()]
    $Credential
    [Parameter()]$Username
                    [Parameter()]$Password

    )
    }

    Get-Cmdletname -Computer 1.2.3.4 -Username admin -Password ****** is executed ,

    Windows Powershell Credentials Request Pop-up is thrown asking for Credentials again even

    Though the username and password is alsready passed in the Cmdlet Execution

    Please Reply for any clarifications

    Regards

    Seshadri






    Tuesday, August 9, 2016 4:23 AM
  • That is not how it works.  You must pass an account or a credential object:

    credtest -Credentail domain/user

    or

    $cred = Get-Credentail
    credtest -Credential $cred

    You cannot just pass arbitrary arguments and have PowerShell figure it out.


    \_(ツ)_/

    • Proposed as answer by Hello_2018 Tuesday, August 16, 2016 7:07 AM
    • Marked as answer by Mike Laughlin Friday, September 2, 2016 12:58 PM
    Tuesday, August 9, 2016 11:03 AM