8 Replies Latest reply on Feb 11, 2020 5:33 AM by jvm2016

    defining get-credential in param block

    jvm2016 Expert

      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.

        • 1. Re: defining get-credential in param block
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: defining get-credential in param block
            jvm2016 Expert

            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

             

             

                   }

            • 3. Re: defining get-credential in param block
              LucD Guru
              Community WarriorsvExpertUser Moderators

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

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: defining get-credential in param block
                jvm2016 Expert

                but its giving me syntax error.

                • 5. Re: defining get-credential in param block
                  LucD Guru
                  User ModeratorsCommunity WarriorsvExpert

                  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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: defining get-credential in param block
                    jvm2016 Expert

                    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

                          

                     

                     

                           )

                    • 7. Re: defining get-credential in param block
                      LucD Guru
                      Community WarriorsvExpertUser Moderators

                      No, you need to define that per parameter.

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: defining get-credential in param block
                        jvm2016 Expert

                        thnaks luc.