4

I'm writing a powershell script which needs to RDP to a few servers and do processes there and then come back.

mstsc /v:<computer> by itself looks great as it's security/credential prompt is the same as if you manually executed it.

However, after some research it appears that's meant to be a command line utility and nothing more because trying things like:

mstsc /v:104.209.198.181 | Invoke-Command -ScriptBlock {"New-Item C:\Users\<me>\Desktop\Success.txt -ItemType file"} 

doesn't work.

So I tried Enter-PSSession <computer> -Credential $env:UserName which people use but it looks like a mess to deal with compared to mstsc because it looks primitive (an article I read yesterday tried to say this type of prompt is ALWAYS a phishing scam which obviously it's not but try telling management), it doesn't auto-populate domains, and I get a WinRM error which I'm sure will be a rabbit hole.

So is it possible to RDP with mstsc and then pipe commands to it so they're executed on that computer?

Ryan
  • 43

2 Answers2

4

No. MSTSC is a terminal client. You should really get enter-pssession working if you want to run scripts on remote machines. It will make your life much easier.

It may be as simple as you just need to allow Powershell Remoting by running a single command on each machine:

Enable-PSRemoting –force
Dre
  • 1,864
3

You can create a scheduled task which will be triggered by logging into the server using a special account within in domain and by the trigger a system will run a prepared script which will be located on each server independently.

Steps: You can create a scheduled task that will run when your computer is unlocked:

Start > Administrative Tools > Task Scheduler In Left top corner select Task Scheduler Library click Create Task in the Right top corner in the Create Task dialog: General tab -- provide a name for your task Triggers tab -- click New... and select On workstation unlock Action tab -- click New... and click Browse... to locate your script Conditions tab -- uncheck Start the task only if the computer is on AC power

Net Runner
  • 6,319