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:
- Removing the ", 1, false" optional arguments = same error.
- 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. - 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. - Also changed escape characters around in as many possible combinations I can think of, so I don't think it is that.
- 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!