-1

So this is what I have in a batch file. What I need it to do is

  • Show the current RDP sessions.
  • Prompt for the session to shadow
  • User input the session ID.

Then the mstsc /shadow: /control /noconsentprompt is executed with the session ID indicated at prompt where /shadow:X is

This is what I have but how to I insert the session ID chosen by user input into the /shadow: field ?

Thanks for your help.

    @echo off
    qwinsta
    set /p id=Enter Session ID:
    echo %id%

    mstsc /shadow: /control /noconsentprompt
Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
SDK
  • 1
  • 1
  • What is stopping you from putting the `%id%` variable after `/shadow:`? – Squashman Feb 25 '21 at 19:13
  • Variables are the core of any programming. If you couldn't use them with a command you need to execute you would never get anything done with a program. You could have just browsed a couple of the batch file questions just to see examples of how variables are used. – Squashman Feb 25 '21 at 19:26
  • 1
    I strongly recommend that you set a bookmark for https://ss64.com/cmd - this is a _very_ good reference site for using the Windows command prompt CMD.EXE. – Jeff Zeitlin Feb 25 '21 at 19:28
  • When taking user input via `Set /P` it is strongly recommended that the input be validated as suitable for the intended use to avoid unhandled failure, or worse, code injection. Search out examples of such on this site. – T3RR0R Feb 26 '21 at 06:01

1 Answers1

0

You will probably need to enable delayed expansion of variables, at which point you would write your mstsc command as

mstsc /shadow:!id! /control /noconsentprompt
Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
  • Based on the code example they have given, I see no reason to use delayed expansion. – Squashman Feb 25 '21 at 19:15
  • @Squashman - Perhaps not, but I've had ... irregular ... results when I don't. – Jeff Zeitlin Feb 25 '21 at 19:16
  • Does this look correct ? `@echo off SETLOCAL EnableDelayedExpansion qwinsta set /p id=Enter Session ID: echo %id% mstsc /shadow:!id! /control /noconsentprompt` – SDK Feb 25 '21 at 19:17
  • @SDK - If the `echo %id%` prints what you expect, then you can use `/shadow:%id%` instead of `/shadow:!id!` - but otherwise, yes, that's what you'd do. – Jeff Zeitlin Feb 25 '21 at 19:18