Create PSCredential inside function from function parameters

Welcome Forums General PowerShell Q&A Create PSCredential inside function from function parameters

This topic contains 6 replies, has 2 voices, and was last updated by

 
Participant
9 months ago.

  • Author
    Posts
  • #94188

    Participant
    Points: 0
    Rank: Member

    Hey ho everyone,

    I am currently consistentlly failing to get some rather simple things done. So your help would be much appreciated. I am trying to build a function that takes some parameters from which PSCredentials are created. Looks like this:

    function compare-Passwords {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true)]
    [String] $userName,
    [Parameter(Mandatory=$true)]
    [Security.SecureString] $pwd1,
    [Parameter(Mandatory=$true)]
    [Security.SecureString] $pwd2
    )

    Write-Host "Pwd Check...";

    $Credentials = New-Object System.Management.Automation.PSCredential ($userName, $pwd1) | Out-Null;
    $CredentialsConfirm = New-Object System.Management.Automation.PSCredential ($userName, $pwd2) | Out-Null;

    ——-
    Well, actually the function goes on with code to compare the passwords, but creating the Credential objects fails and retreiving the passwords later on with $Credentials.GetNetworkCredential().Password returns NULL causing things to fail. I struggle to see why the credentials are not created.

    If I create myself a username and SecureString pwd variable and just run the code in the function with those variables it works. So passing the parameters into the function and using them for Credential creations seems to be the problem. But I can't see what's wrong.

    Thanks & Cheers
    Mark

  • #94194

    Keymaster
    Points: 1,644
    Helping HandTeam Member
    Rank: Community Hero

    So, when you create the credential, you then send it to Out-Null, thereby destroying the credential you just created. Is that the intent?

  • #94195

    Participant
    Points: 0
    Rank: Member

    No, not at all. I added this statements to keep the script from creating output when the function is called from another script that shall run automatically. So far I thought it does not alter the variable just prevents it be written to the console.

    I commented that part out at some point to make sure it is not part of the problem. But I will try that again and by rather annoyed if it really was that simple to fix the problem.

  • #94197

    Keymaster
    Points: 1,644
    Helping HandTeam Member
    Rank: Community Hero

    When you do:

    $var = Do-Something | Select-Whatever | Out-Null

    The variable stores _the end result of the pipeline_, which in this case is nothing. So while that may not be your _only_ problem, it is in this case at least _one_ of your problems. Using a variable will capture the entire pipeline (except with a few poorly written commands), which would prevent output from "showing up" anyway.

  • #94200

    Participant
    Points: 0
    Rank: Member

    Well it was that simple. Thanky you very much. I will spend some time feeling stoopid and some more to figure out exactly when I will need to add the "out-null" part. Eventhough I am kinda suspicious that it is need only when code is not assigned to a variable. I'll see...

  • #94203

    Keymaster
    Points: 1,644
    Helping HandTeam Member
    Rank: Community Hero

    It is. This is just understanding the pipeline.

    At the end of any pipeline, even with just one command, PowerShell has to decide what to do with whatever's in the pipeline after it's executed. If you've assigned the pipeline to a variable, then that's where any output objects go. If you didn't, then PowerShell passes your pipeline objects to Out-Default, which passes them to Out-Host, which invokes the formatting subsystem to render the objects into text for on-screen display.

    See "Learn Windows PowerShell in a Month of Lunches" for more.

    The exception would be errors, warnings, verbose, and debug messages, which do not go into the pipeline, and which have their own mechanisms for coming to the host display.

    A rare additional exception is a command – typically a compiled cmdlet – that outputs objects to the pipeline, but also writes messages directly to the host window. You can't capture/redirect those hardcoded host messages, but it's _exceedingly_ rare to find commands that do that.

  • #94204

    Participant
    Points: 0
    Rank: Member

    Sorry, I did not see your second post yet. That makes things rather clear. Thanks again for your very quick and useful advice.

    Cheers
    Mark

The topic ‘Create PSCredential inside function from function parameters’ is closed to new replies.