Solved

Need to run multiple RDC sessions using Powershell

Posted on 2010-11-09
13
1,124 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 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now