Can I RDP through mstsc and run a script on that remote desktop?

6,917

Solution 1

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

Solution 2

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

Share:
6,917

Related videos on Youtube

Ryan
Author by

Ryan

Updated on September 18, 2022

Comments

  • Ryan
    Ryan over 1 year

    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?

    • Matthew Wetmore
      Matthew Wetmore almost 6 years
      There's a good chance you don't even need to prompt for a credential if the current user is an administrator on the remote machine. Get-Credential (whoami) will pre-populate the prompt with both username and domain. You can also use -Message to provide additional info.
  • Colyn1337
    Colyn1337 almost 6 years
    I just want to clarify an aspect to this question. Enter-PSSession is good for interactive session commands but it is not for use IN scripts. If you need to remote inside a script, use invoke-command.