VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

defining get-credential in param block

hi luc,

many a times we are defining $username and $password in param block as mandatory parametrs.

can yu suggest how get-credential can be defined in a similar way.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You have a comma after $Credential

function test-credential

{

[cmdletbinding()]

    param (

        [parameter(mandatory = $true,

            valuefrompipeline = $true,

            valuefrompipelinebypropertyname = $true)]

       [string]$cisserver,

       [PSCredential]$Credential

    )

}


Test-Credential -Credential (Get-Credential)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

You mean like this?
PS: this also shows the weakness of a PSCredential password.

function Test-Credential{

    param(

        [PSCredential]$Credential

    )


    $Credential.GetNetworkCredential() | Select *

}


Test-Credential -Credential (Get-Credential -Message 'Test credentials')


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

do i need to create a second param block .or how to put in an existing param block as follows???

function test-credential

{

[cmdletbinding()]

    param (

        [parameter(mandatory = $true,

            valuefrompipeline = $true,

            valuefrompipelinebypropertyname = $true)]

     

      

       [string]$cisserver,

      

        

       [PSCredential]$Credential,

       $Credential.GetNetworkCredential() | Select *

       )

       

     

             

       Connect-CisServer -server $cisserver -Credential $credential

       }

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is the correct way, all in one param block,


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

but its giving me syntax error.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have a comma after $Credential

function test-credential

{

[cmdletbinding()]

    param (

        [parameter(mandatory = $true,

            valuefrompipeline = $true,

            valuefrompipelinebypropertyname = $true)]

       [string]$cisserver,

       [PSCredential]$Credential

    )

}


Test-Credential -Credential (Get-Credential)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

below works .however i was expecting comma in orange line but it works .

i somehow rememer that we need to define parameter mandatory once in entire param block is this not the case??

[cmdletbinding()]

    param (

        [parameter(mandatory = $true,

            valuefrompipeline = $true,

            valuefrompipelinebypropertyname = $true)]

     

      

       [string]$cisserver,

      

       [parameter(mandatory = $true)]

       [PSCredential]$Credential

      

       )

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, you need to define that per parameter.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thnaks luc.

Reply
0 Kudos