1

Background:

I am trying to run C:\Windows\System32\Logoff.exe on a local machine from a ASP.NET web application to log off the local user from their terminal services session.

Here is what I have so far:

C#

protected void btnContinueClick(Object sender,
                       EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("WshShell = new ActiveXObject('WScript.Shell');\n");
        sb.Append(@"WshShell.Run('C:\\Windows\\System32\\Logoff.exe " + intSessionID + " /SERVER:" + strServer + "', 1, false);");
        ScriptManager.RegisterStartupScript(Page, GetType(), "RestartSession", sb.ToString(), true);

Running the above with IE's F12 menu open, shows the script gets added to my HTML successfully:

HTML

<script type="text/javascript">
//<![CDATA[
WshShell = new ActiveXObject('WScript.Shell');
WshShell.Run('C:\\Windows\\System32\\Logoff.exe', 1, false);//]]>
</script>
</form>
</body>

However when the Run(...) line executes I get the following error in IE:

Error: WshShell.Run()   Wrong number of arguments or invalid property assignment

What I have tried:

  1. Removing the ", 1, false" optional arguments = same error.
  2. Moving the ' ' so the line reads (don't know which is correct): sb.Append(@"WshShell.Run('C:\\Windows\\System32\\Logoff.exe " + intSessionID + " /SERVER:" + strServer + ", 1, false);'"); = same error.
  3. Calling a .bat file with the logoff command inside. (The .bat does actually run, however I get a new error: "logoff is not recognized as an internal or external command, operable program or batch file". Very similar to Call logoff in Ant script.
  4. Also changed escape characters around in as many possible combinations I can think of, so I don't think it is that.
  5. Running the command directly from a CMD window does the intended job.

Please help!

Any suitable alternative methods of logging off a user's terminal services session from an internal Intranet web app would also be greatly appreciated!

Community
  • 1
  • 1
Tim Rose
  • 45
  • 5
  • `Logoff` needs some parameters? – Teemu Feb 10 '17 at 17:55
  • My plan was to replace the line with: `sb.Append(@"WshShell.Run('C:\\Windows\\System32\\Logoff.exe " + intSessionID + " /SERVER:" + strServer + ", 1, false');");` however this produces the same error. intSessionID and strServer are correct values for the Terminal Services session ID and server hostname. – Tim Rose Feb 13 '17 at 12:18
  • Edited the question to show these parameters as I think they will be necessary when running from a web server. – Tim Rose Feb 13 '17 at 12:27

1 Answers1

0

After a lot of variations, I have a working solution. Although the issue was most likely just with variable passing and escape characters (so quite trivial), I'm posting the answer anyway as it could point others towards a work-around when passing variables across multiple platforms.

C#

ScriptManager.RegisterStartupScript(Page, GetType(), "Logoff", $"Logoff(\"{intSessionID}\", \"{strServer}\");", true);

JavaScript

<script type="text/javascript">
    function Logoff(intID, strSvr) {
        WshShell = new ActiveXObject("WScript.Shell");
        WshShell.Run("C:\\Users\\User\\Desktop\\Logoff.vbs" + " \"" + intID + "\" " + " \"" + strSvr + "\"", 1, false);
    }
</script>

The VB script then runs the original Logoff.exe command, along with a couple other audit logging lines.

Tim Rose
  • 45
  • 5