Solved

Need to run multiple RDC sessions using Powershell

Posted on 2010-11-09
13
1,129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

738 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