1

I'm writing a function to create a variable called $Credentials which I use in another function to verify if it's valid or not. By using return $Credentialsin the function I hoped the variable would've been available after the Function Import-Password has ran, but this isn't the case..

I think this could be solved by using $script:Credentials, but I don't think it's needed if the function can output this variable.

Thank you for your help.

Function Import-Password

# Check the password file
Function Import-Password ($UserName,$PasswordFile,[switch]$SendMail) {
    try {
        if (!(Test-Path $PasswordFile)) {
            Write-Host "$env:COMPUTERNAME > Check password file: $PasswordFile > The password file can not be found`n
            - Password file :`t $PasswordFile
            - Server name   :`t $env:COMPUTERNAME" -ForegroundColor Yellow
            if ($SendMail) {
                Send-Mail "FAILED AD Authentication" "The password file can not be found" "- Password file : $PasswordFile<br>- Server name   : $env:COMPUTERNAME"
            }
            break
        }
        $Password = cat $PasswordFile | ConvertTo-SecureString -Force -ErrorAction Stop
        $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName,$Password 
        return $Credentials
    }
    catch {
        Write-Host "$env:COMPUTERNAME > Check password file: $PasswordFile > The password has been hashed with another account than the account used to run this script (all 3 users/owners need to be the same)`n
        - Script account:`t $env:USERDNSDOMAIN\$env:USERNAME
        - Password user :`t $UserName
        - Password file :`t $PasswordFile
        " -ForegroundColor Yellow
        if ($SendMail) {
            Send-Mail "FAILED AD Authentication" "The password has been hashed with another account than the account used to run this script (all 3 users/owners need to be the same)" "- Script account: $env:USERDNSDOMAIN\$env:USERNAME<br>- Password user : $UserName<br>- Password file : $PasswordFile"
        }
        break
    } 
}
Import-Password $UserName $PasswordFile
3
  • Is it possible that your code ends up in the exception handler? No return value is specified there. Scoping should not play a role here. Commented Jul 9, 2014 at 7:36
  • Yes, the code sometimes ends up in the Catch block when the ConvertTo-SecureString fails. This might happen if the incorrect user is running the script. Commented Jul 9, 2014 at 8:10
  • Thanks for the update. I think I misunderstood what you were asking, but I see that @KeithHill provided an answer. Commented Jul 9, 2014 at 8:17

1 Answer 1

5

You have to capture the output of the function in order to use it later in your script e.g.:

$cred = Import-Password $UserName $PasswordFile
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your feedback. But is there an option to have the function return the variable $Password also? Because I use $Credentials for the Invoke-Command later on in the script and $Password in another function to verify it.
The SecureString version of the password will be available as a property on the PSCredential you return.
@DarkLite1: What Keith said. However, for the sake of completeness, you can return an array with multiple elements (e.g. return $Credentials, $password) and capture the elements of the returned array in separate variables: $cred, $passwd = Import-Password .... The last left-hand variable captures all remaining elements of the returned array if there are more elements than variables.
Thanks for the help guys, this was exactly what I was looking for. Works like a freight train! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.