从 JavaScript 运行 WScript.Shell.Run() 时获取 "Wrong number of arguments or invalid property assignment"
Getting "Wrong number of arguments or invalid property assignment" when running WScript.Shell.Run() from JavaScript
背景:
我正在尝试 运行 C:\Windows\System32\Logoff.exe 在本地计算机上从 ASP.NET 网络应用程序中注销本地用户的终端服务会话。
这是我目前的情况:
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);
运行在 IE 的 F12 菜单打开的情况下执行上述操作,显示脚本已成功添加到我的 HTML:
HTML
<script type="text/javascript">
//<![CDATA[
WshShell = new ActiveXObject('WScript.Shell');
WshShell.Run('C:\Windows\System32\Logoff.exe', 1, false);//]]>
</script>
</form>
</body>
然而,当 运行(...) 行执行时,我在 IE 中收到以下错误:
Error: WshShell.Run() Wrong number of arguments or invalid property assignment
我试过的:
- 删除“, 1, false”可选参数 = 同样的错误。
- 移动“ ”使该行显示(不知道哪个是正确的):
sb.Append(@"WshShell.Run('C:\Windows\System32\Logoff.exe " + intSessionID + " /SERVER:" + strServer + ", 1, false);'");
= 同样的错误。
- 正在调用一个包含注销命令的.bat 文件。 (.bat 实际上确实 运行,但是我收到一个新错误:
"logoff is not recognized as an internal or external command, operable program or batch file"
。非常类似于 Call logoff in Ant script。
- 还以我能想到的尽可能多的可能组合更改了转义字符,所以我认为不是那样。
- 运行直接从 CMD window 命令执行预期的工作。
请帮忙!
从内部 Intranet Web 应用程序注销用户终端服务会话的任何合适的替代方法也将不胜感激!
经过多次修改后,我有了一个可行的解决方案。虽然问题很可能只是变量传递和转义字符(非常微不足道),但我还是发布了答案,因为它可能会在跨多个平台传递变量时将其他人指向 work-around。
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>
VB 脚本然后运行原始的 Logoff.exe 命令,以及其他几个审计日志行。
背景:
我正在尝试 运行 C:\Windows\System32\Logoff.exe 在本地计算机上从 ASP.NET 网络应用程序中注销本地用户的终端服务会话。
这是我目前的情况:
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);
运行在 IE 的 F12 菜单打开的情况下执行上述操作,显示脚本已成功添加到我的 HTML:
HTML
<script type="text/javascript">
//<![CDATA[
WshShell = new ActiveXObject('WScript.Shell');
WshShell.Run('C:\Windows\System32\Logoff.exe', 1, false);//]]>
</script>
</form>
</body>
然而,当 运行(...) 行执行时,我在 IE 中收到以下错误:
Error: WshShell.Run() Wrong number of arguments or invalid property assignment
我试过的:
- 删除“, 1, false”可选参数 = 同样的错误。
- 移动“ ”使该行显示(不知道哪个是正确的):
sb.Append(@"WshShell.Run('C:\Windows\System32\Logoff.exe " + intSessionID + " /SERVER:" + strServer + ", 1, false);'");
= 同样的错误。 - 正在调用一个包含注销命令的.bat 文件。 (.bat 实际上确实 运行,但是我收到一个新错误:
"logoff is not recognized as an internal or external command, operable program or batch file"
。非常类似于 Call logoff in Ant script。 - 还以我能想到的尽可能多的可能组合更改了转义字符,所以我认为不是那样。
- 运行直接从 CMD window 命令执行预期的工作。
请帮忙!
从内部 Intranet Web 应用程序注销用户终端服务会话的任何合适的替代方法也将不胜感激!
经过多次修改后,我有了一个可行的解决方案。虽然问题很可能只是变量传递和转义字符(非常微不足道),但我还是发布了答案,因为它可能会在跨多个平台传递变量时将其他人指向 work-around。
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>
VB 脚本然后运行原始的 Logoff.exe 命令,以及其他几个审计日志行。