Help with SecureString

Why will ISE pop a box up for user input but the regular powershell console will not? I have a script that gathers a username and password (as secure string), when I run in ISE there is a pop-up window that will ask for the password (preferred) but when using regular Powershell it asks via the console. My script asks the user twice for the password, encrypts, compares, and if they do not match it will ask again. This does use “read-host” which I know is supposed to take input from the console, so I am wondering if there is a different approach I can use to get this password created and encrypted using a pop-up.

$UserName = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter a unique username", "New SQL User") While($SQLLogins -match $UserName) {Write-Host "User already exists" Clear-Variable -Name SQLLogins $UserName = [Microsoft.VisualBasic.Interaction]::InputBox("User already exists!`nPlease enter a unique username", "Already Exists") } # Asks for password twice, encrypts, compares, passes if they match do{ $SecurePassword = Read-Host -Prompt "Enter a secure password for the new user" -AsSecureString $SecurePasswordCheck = Read-Host -Prompt "Re-enter the password" -AsSecureString $SecurePassword_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)) $SecurePasswordCheck_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePasswordCheck)) } while ($SecurePassword_text -ne $SecurePasswordCheck_text) # Creates a new SQL Login with the given username and password $SQLLogin = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Login($server,$UserName) $SQLLogin.LoginType = [Microsoft.SqlServer.Management.SMO.LoginType]::SqlLogin $SQLLogin.PasswordPolicyEnforced = $False $SQLLogin.Create($SecurePassword) 

submitted by /u/cassato
[link] [comments]

Leave a Reply