Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need to run multiple RDC sessions using Powershell

Posted on 2010-11-09
13
Medium Priority
?
1,139 Views
Last Modified: 2012-05-10
I'm attempting to write a script below using New-Item cmdlet but apparently it does not like the DOS / Console (MSTSC.exe /v:SERVERNAME) paramenter being passed in.
Does anyone know of a Cmdlet that will allow me to loop through a serverlist and execute Remote Desktop Connection, i.e. open up as many RDC dialog boxes as I need to?

Thanks for you help Experts,
Wallace
$a = Get-Content "c:\PowerShell Scripts\Servers.txt"

for ($i = 0; $i -lt $a.Length; $i++)
{
  Invoke-Item $env:systemroot\system32\mstsc.exe /v:$a[$i]
}

Open in new window

0
Comment
Question by:wally_davis
  • 7
  • 6
13 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 2000 total points
ID: 34098466
Try this and see if it works for you
$a = Get-Content "c:\PowerShell Scripts\Servers.txt" 
foreach($s in $a){
$sr = $env:systemroot
& $sr\system32\mstsc.exe /v:$s
}

Open in new window

0
 

Author Comment

by:wally_davis
ID: 34098747
I shortened it to this because it didn't recognize the & symbol.
$a = Get-Content "c:\PowerShell Scripts\Servers.txt"
foreach($s in $a)
{
   $env:systemroot\system32\mstsc.exe /v:$s
}

I received the output of
C:\Windows\System32\mstsc.exe /v:ALT_SRVDS
C:\Windows\System32\mstsc.exe /v:RMXSSRV1 and
C:\Windows\System32\mstsc.exe /v:CATFISH but it did not execute the RDC/Mstsc.exe at all.

So, I tried prefixing the command "Invoke-Item -Path" before "$env:systemroot\system32\mstsc.exe /v:catfish" and I get the error, "The given path's format is not supported. Invoke-Item cannot find path 'C:\Windows\System32\mstsc.exe /v:Catfish' because it does not exist.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34098796
Are you running PowerShell v2?
I tested the script I posted and it works on a few systems I test but they all have V2.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:wally_davis
ID: 34098804
Hey KenMcF,
I tried this and it worked:
$a = Get-Content "c:\PowerShell Scripts\Servers.txt"
ForEach ($s In $a)
{
   c:\windows\system32\mstsc.exe /v:$s
}

what I don't get is why the $sr = $env:systemroot couldn't be passed in to the script itself as in this part of the ForEach Loop --> $sr\system32\mstsc.exe /v$s. But it gives the error "Unexpected Token '\system32\mstsc.exe in expression or statement". However, if your in the PowerShell "Shell" and type in $sr = $env:systemroot\mstsc.exe and echo $sr it shows the path "C:\Windows\System32\mstsc.exe. I'll try some other stuff out and will give you most of the points since you almost had it and in my opinion should have worked. Either way, its done. :)
0
 

Author Comment

by:wally_davis
ID: 34098837
Yes, I echoed "echo $psversiontable" and I receive the value for CLRVersion = 2.0.50727.3615.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34098838
When I first tested it worked if I had the full path, I assumed you were using the variable becuase your system rootcould be different if you ran this on multiple computers.
0
 

Author Comment

by:wally_davis
ID: 34098843
and PSVersion = 2.0
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34098871
try this

foreach($s in $servers){
$sr = $env:systemroot
Invoke-Expression "$sr\system32\mstsc.exe /v:$s"
}
0
 

Author Comment

by:wally_davis
ID: 34098928
Interesting. When I run it, it only pops up one RDC Dialog box and I have three servers in the text file.
Srv1
Srv2
Srv3
Srv2 gets populated into the RDC Textbox.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34098938
that is strange, What does your servers.txt file look like. Is it one server per line?
0
 

Author Comment

by:wally_davis
ID: 34099006
yes, its one Server per line.

Ken, I tried this and it worked. I had to do a little bit of reading up on Invoke-Expression:

$servers = Get-Content "C:\PowerShell Scripts\Servers.txt"
ForEach ($s In $servers)
{
   Invoke-Expression "$env:systemroot\system32\mstsc.exe /v:$s"
}
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34099066
Glad it is working, not sure why the one I posted only started one rdp session. It is doing the sae thing as the one you posted.
0
 

Author Closing Comment

by:wally_davis
ID: 34099104
KenMcF, Thank you for the solution. It was exactly what I needed and I've learned a little bit more about Powershell. :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question