powershellremote-desktoppowershell-remotingmstsc

Open multiple remote sessions using 'mstsc' in powershell script


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:

# 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.


Solution

  • This behavior is cause by your use of Out-String.

    Get-Content outputs multiple strings, one per line in the file - but Out-String stitches them back together into a single multi-line string:

    PS C:\> $machines = Get-Content machines.txt
    PS C:\> $machines.GetType().Name   # definitely an array
    Object[]
    PS C:\> $machines.Count            # multiple strings in there
    4
    PS C:\> $machines = Get-Content machines.txt | Out-String
    PS C:\> $machines.GetType().Name   # now it's just a single string
    String
    

    So your foreach(){} loop only runs once, and the value of $MachineName is no longer the name of a single machine, but a multi-line string with all of them at once - which is probably why mstsc exits immediately :)

    Remove |Out-String from the first line and your loop will work