I'm having a lot of difficulty with a PowerShell script that I'm trying to call a DirectoryServices query from. Currently, if I do a
$password = read-host "Password" -asSecureString and subsequently
$credential = New-Object System.Management.Automation.PSCredential $username,$password everything works fine. However if I try to pass the string parameter with a param($password) and then convert it to a secure string with this code:
$password = ConvertTo-SecureString -AsPlainText -Force $password After extensive debugging I can see this is working fine in terms of converting the string to a securestring, but I get a bad user/password from DirectoryServices when I use the parameter. Everything works fine when read from the console. Any ideas on what I can do to accept a parameter OR take console input in the absence of a parameter?
This is what I was hoping would work, but doesn't:
if($password -eq $null) { $password = read-host "Password" -asSecureString } else { $password = ConvertTo-SecureString -AsPlainText -Force $password } $credential = New-Object System.Management.Automation.PSCredential $username,$password
Read-Host -AsSecureString. There are some funnies with Read-Host like if you remove the -AsSecureString and type in!foo<enter>at the prompt, Read-Host will complain.