I am writing a script to launch MSTSC from a grid of buttons. These buttons will each open up a different server connection. I have a separate button that gets and stores admin creds, that I borrowed from another script. When clicked, the button calls the getAdmin
function:
function getAdmin {
$returnObject = $null; $credValid = $null;
$cred = Get-Credential #Read credentials
$username = $cred.username
$password = $cred.GetNetworkCredential().password
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
if ($domain.name -eq $null)
{
$credValid = $false
$cred = $null
} else {
$credValid = $true
}
$returnobject = new-object psobject -property @{ Valid = $credValid; Credentials = $cred }
return $returnObject }
Then I have my "server buttons:"
$btnServ = new-object system.windows.forms.button
$btnServ.location = new-object system.drawing.point (10,200)
$btnServ.text = "Server Name (SERVER)"
$btnServ.width=250
$btnServ.height = 30
$btnServ.add_click({ Start-Process mstsc -ArgumentList "/v:SERVER" -Credential $cred })
Now, when I store creds using my first button, then I click the btnServ
button, it still prompts me for "Windows PowerShell Credential Request" instead of grabbing the stored creds from the previous action. If I remove -Credential $cred
from the call, whether I click on the first getAdmin
button or not, it just says that my logon attempt has failed and that I provided the incorrect information.
Ideally, I want to store my creds, then no matter which server button I press it will log me in instead of making my type in my creds twice.