Passing $Credential object as parameter to function

Welcome Forums General PowerShell Q&A Passing $Credential object as parameter to function

Viewing 2 reply threads
  • Author
    Posts
    • #211332
      Participant
      Topics: 5
      Replies: 7
      Points: 90
      Rank: Member

      Hi Guys,

      Easy question – what is the problem here? I cannot pass $Creds object to function – it doesn’t see it in function. Object is created outside.

      It opens PSCreditional pop-up.

       

      function Read($Computer,$Creds)
      {
      Write-Host “This is COMPUTER VAR:” $Computer
      Write-Host “This is CREDS VAR:” $Creds
      Invoke-Command -Credential $Creds -ComputerName $Computer -ScriptBlock {Get-Service WINRM}
      Return $Results
      }

      $User=”XXXX”
      $Password=”YYYYY”
      $Pass=ConvertTo-SecureString “$Password” -AsPlainText -Force
      $Creds=New-Object System.Management.Automation.PSCredential($User,$Pass)

      $Computer = “SOME IP”
      Read “$Computer” “$Creds”

      • This topic was modified 2 months, 1 week ago by matekubi75.
      • This topic was modified 2 months, 1 week ago by matekubi75.
    • #211350
      Participant
      Topics: 12
      Replies: 1607
      Points: 2,495
      Helping Hand
      Rank: Community Hero

      The biggest issue is wrapping qoutes around you variables (e.g. “$Creds”), but there were several other things like it appeared you were trying to do a string format on the Write-Host, but there was a missing -f and returning $results (the ‘return’ keyword is not required) but not assigning a variable. Try starting with something like this:

      function Get-WinRm {
          [CmdletBinding()]
          param (
              [string]$Computer,
              [PSCredential]$Creds
          )
      
          Write-Verbose (“This is COMPUTER VAR: {0}” -f $Computer)
          Write-Verbose (“This is CREDS VAR: {0}” -f $Creds.UserName)
          $results = Invoke-Command -Credential $Creds -ComputerName $Computer -ScriptBlock {Get-Service WINRM}
          $results
      }
      
      $User=”XXXX”
      $Password=”YYYYY”
      $Pass=ConvertTo-SecureString “$Password” -AsPlainText -Force
      $Creds=New-Object System.Management.Automation.PSCredential($User,$Pass)
      
      $Computer = “computer123”
      Get-WinRm -Computer $Computer -Creds $Creds -Verbose
      

      Also, naming the function Read is asking for issues as it’s probably a reserved phrase or word, so name your function like other cmdlets is a best practice verb-noun.

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-verb?view=powershell-7

    • #211392
      Participant
      Topics: 5
      Replies: 7
      Points: 90
      Rank: Member

      Hi Rob,

      Thanks for your time. The reason was “$Creds” – quotes around the variable. Other mistakes were during “fixing\stage” 🙂

      I thought that I can use quotes there – especially it works with $Computer variable – I don’t understand why I don’t work with $Creds.

      Thanks again – helped a lot 🙂

       

Viewing 2 reply threads
  • You must be logged in to reply to this topic.