Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to run multiple RDC sessions using Powershell

Posted on 2010-11-09
13
Medium Priority
?
1,137 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 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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