Author Posts

February 20, 2018 at 3:14 pm

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

February 20, 2018 at 3:16 pm

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

February 20, 2018 at 3:21 pm

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.

February 20, 2018 at 3:24 pm

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.

February 20, 2018 at 3:25 pm

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...

February 20, 2018 at 3:29 pm

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.

February 20, 2018 at 3:29 pm

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