Currently, have two domains in which our users will have machines existing in either or. I am writing a powershell script in which I collect the user name and machine name. I think perform a test-connection to determine which domain the machine resides by pinging $computer.domain.com then if fails ping $computer.otherdomain.com.
Once I determine the domain I then take the username and place "domain\" + $username to prefill the domain name. I do the same with the provided and stored $machinename by performing this: $machinename + ".domain.com".
I don't want to store user's passwords
I've been trying to use cmdkey to associate the username with the machine and call mstsc (see example below):
cmdkey /generic:TERMSRV/$computername /user:"username" mstsc /v:$computername
however when i run this, I get a logon attempt failure which isn't ideal.
I thought about trying to use the /prompt field after mstsc /v:$computername so: mstsc /v:$computername /prompt
but this won't use the stored username, it will instead use the user profile i am currently running the command under.
I simply want my script to take the user's username and machine name, test and confirm which domain the machine resides in the back round, then have prefilled the username with the domain and just prompting the user to enter their password to continue.
Any advice on this would be greatly appreciated!
Here is the code i am working on below: ******
#Storing the provided username within variable $Username
$UserName = $UsernameTextbox.Text
Write-host "Username entered =" $UserName
#Storing the user's provided comuter name into $Machinename
$MachineName = $HostnameTextbox.Text
Write-host "Machinename entered =" $MachineName
#Deletes any previously stored cmdkeys that may interfere with the
cmdkey /list | ForEach-Object{if($_ -like "*Target:*"){cmdkey /del:($_ -
replace " ","" -replace "Target:","")}}
if (Test-Connection -ComputerName "$MachineName.domain.com" -Quiet ) {
$User = "domain\" + $UserName
$MachineName = $MachineName + ".domain.com"
Write-Host "Computer responded in domain"
}
else {
$User = "domain2\" + $UserName
$MachineName = $MachineName + ".domain2.com"
Write-Host "Computer is most likely in domain2"
}
cmdkey /add:$MachineName /user:$user
Write-host "Username with discovered domain name =" $User
Write-host "machine name after domain check =" $MachineName
$argumentslist = "/v:$MachineName " #took out /prompt
Start-Process -FilePath C:\Windows\System32\mstsc.exe -ArgumentList
"$argumentslist"