I am trying to write a powershell script that opens a remote desktop connection for each machine name saved in a text file. When I run the script, it only connects to the first machine in the list and outputs to the console: CMDKEY: Credential added successfully
once (not once for each machine). mstcs
seems to terminate the process after executing, and I'm not sure I'm adding credentials the right way. Can anyone point me in the right direction?
Here are some tests I've tried to figure out what's going on:
- Print after
mstsc
. Doesn't print. Process seems to terminate aftermstcs
is called. This seems to be the crux of the issue. cmdkey /list
shows all the credentials I have stored and their targets. The output does not include all the targets defined in the text file. Even if I comment outmstsc
, thencmdkey /add:$MachineName /user:$User /pass:$Password
only seems to execute for the first line, evidenced by the lack of more console outputs andcmdkey /list
not yielding the expected targets. In addition, I have added a print statement after thiscmdkey
line and it prints for each line, so it doesn't terminate after running (which I already knew becausemstcs
executes after this line when it's not commented out).
# Read from file
$Lines = Get-Content -Path .\machines.txt | Out-String
# For each machine ...
foreach($Line in $Lines){
# Split line, save name and domain
$Tokens = $Line.Split(".")
$MachineName = $Tokens[0]
$Domain = $Tokens[1]
$User = "someDomain\someUsername"
$Password="somePassword"
# Switch username if someOtherDomain
if ($Domain -eq "someOtherDomain"){
$User = "someOtherDomain\someOtherUsername"
}
#set credentials and open connection
cmdkey /add:$MachineName /user:$User /pass:$Password
mstsc /v:$MachineName /console
}
EDIT: I have also tried replacing mstsc /v:$MachineName
with Start-Process -FilePath "$env:windir\system32\mstsc.exe" -ArgumentList "/v:$MachineName" -Wait
. The result is opening the session and then the script does not finish in the console but nothing additional happens.