[PSCredential] Parameter in PSv2

This topic contains 2 replies, has 2 voices, and was last updated by  Justin King 1 year ago.

  • Author
    Posts
  • #49103

    Justin King
    Participant

    I swear Server 2008 is the new 2003 ... I cringe when I see it now.

    Anyway, I'm automating some processes for a client and their company has standardized on ... ugh ... 2008/PSv2. When writing a function under this version I noticed a curious behavior:

    If I try to define a parameter of type [PSCredential] it simply "doesn't work": The parameter seems to ever load, won't tab complete, etc. That said, if I prompt for credentials in the script directly with somehting like: [PSCredential]$creds = Get-Credential then everythgin is fine.

    Is it a known limitation that PSv2 can't accept PSCredentials as parameters? Or is my syntax just wrong? Do I have to request seperate username and password as securestring?

    Looking for advice in "dealing with" PSv2 (aside from telling them to upgrade).

  • #49184

    tommymaynard
    Member

    In my testing, as yours, this first example doesn't work in PowerShell 2.0. My second example, however, did work in PowerShell 2.0.

    Function Write-Cred1 {
        Param (
            [PSCredential]$Credential
        )
        $Credential
    }
    
    Function Write-Cred2 {
        Param (
            [System.Management.Automation.PSCredential]$Credential
        )
        $Credential
    }
    
  • #49223

    Justin King
    Participant

    I could have sworn I tested both examples, but I will retest tonight. Thanks for being a sounding board!

You must be logged in to reply to this topic.