1

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 
6
  • 2
    huh? You enter password, create securestring, create credentials and extract password? why? the password doesn't change.. Commented Dec 21, 2012 at 19:57
  • Sorry, that last line you're referring to should be omitted/commented out, it was for debugging purposes. Commented Dec 21, 2012 at 22:06
  • After some further debugging, it appears its not even a securestring issue - for some reason directory services won't take the string when it comes from the param, but it does when its prompted. Still unsure whats going on. Commented Dec 21, 2012 at 22:11
  • 1
    Can you post the directoryservices code that is not working, and the error message you're getting? Commented Dec 21, 2012 at 23:45
  • 1
    Look at my answer to this SO question: stackoverflow.com/questions/13994841/powershell-smtp-send It shows how to dump the contents of the secure string you get back from 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. Commented Dec 22, 2012 at 5:57

1 Answer 1

1

I recently created a script and was running into the same issue. The work around I found in my case was the following:

#Prompts for the username/password, enter the username in the form of DomainName\UserName $Credential = get-credential #Converts the password to clear text to pass it through correctly as passing through a secure string does not work. $Password = $credential.GetNetworkCredential().password #Converts the $Credential to just the DomainName/UsernName. $Account = $credential.UserName 

Hopefully this will work in your situation

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.