Solved

Need to run multiple RDC sessions using Powershell

Posted on 2010-11-09
13
1,128 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DFS issue 3 26
Need a powershell script to add multiple users to multiple groups 6 62
Comparing two variable properties 3 26
Powershell and robocopy 2 13
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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