Reputation: 14695
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 $Credentials
in 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
Upvotes: 1
Views: 5322
Reputation: 201602
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
Upvotes: 5