简体   繁体   English

批处理文件调用VBS,参数中带有“带引号的空格”

[英]Batch file call VBS with “quoted spaces” in argument

I am trying to create a batch file to restart multiple computers from a TXT file. 我正在尝试创建一个批处理文件,以从TXT文件重新启动多台计算机。 Everything works fine as long as the /c "comment here" parameter has no spaces. 只要/ c“在此处注释”参数没有空格,一切都可以正常工作。 If I pass "Testing" as the comment, I get "Testing" in the pop-up for the restart, as expected. 如果我通过“测试”作为注释,则按预期在弹出窗口中看到“测试”。 If I pass "Testing spaces" as the comment, I still only get "Testing" in the pop-up. 如果我通过“测试空间”作为注释,则在弹出窗口中仍只会得到“测试”。 With @echo off, I have verified the comment retains the spaces when it is passed to VBS, so I think the problem is that I am running through an "invisible.vbs" script to prevent another CMD window from opening and hanging the original BAT script. 禁用@echo时,我已验证注释在传递给VBS时保留了空格,因此我认为问题是我正在运行“ invisible.vbs”脚本,以防止另一个CMD窗口打开和挂起原始BAT。脚本。

I would like to be able to have a final command run similar to: 我希望能够运行类似于以下命令的最终命令:

shutdown /r /m \\\\127.0.0.1 /t 120 /c "Your computer will shut down for maintenance tasks in two minutes" shutdown / r / m \\\\ 127.0.0.1 / t 120 / c“您的计算机将在两分钟内关闭以执行维护任务”

Any help with this would be greatly appreciated! 任何帮助,将不胜感激!

The essential part of restart.bat: restart.bat的基本部分:

for /f "tokens=1-3" %%c in (%FilePath%) do WScript /nologo "%windir%\myscripts\invisible.vbs" "shutdown /r /m \\%%c /t %delay% /c %message%"

I have also tried adding extra quotes around %message% with no success: 我也尝试过在%message%周围添加额外的引号,但没有成功:

for /f "tokens=1-3" %%c in (%FilePath%) do WScript /nologo "%windir%\myscripts\invisible.vbs" "shutdown /r /m \\%%c /t %delay% /c "%message%""

The invisible.vbs script (found on StackExchange): invisible.vbs脚本(在StackExchange上找到):

CreateObject("Wscript.Shell").Run "" & WScript.Arguments(0) & "", 0, False

Any help would be greatly appreciated! 任何帮助将不胜感激!

EDIT: Using your third method suggested, I am getting a script error: 编辑:建议使用您的第三个方法,我收到脚本错误:

Script:    C:\Windows\myscripts\invisible.vbs
Line:      1
Char:      46
Error:     The system cannot find the file specified
Code:      80070002
Source:    (null)

EDIT 2: I just tried the first method also, and I now get a vbscript echo pop-up with the correct command that I wish to be sent, but the command is not sent. 编辑2:我也尝试了第一种方法,现在我收到了vbscript echo弹出窗口,其中包含我希望发送的正确命令,但是命令没有发送。 I am far from a programmer, and the only VBS I have used is stuff I have found online. 我离程序员还很远,我使用的唯一VBS是我在网上找到的东西。 I do appreciate the help you have offered so far, but I still can't get this to work. 感谢您到目前为止提供的帮助,但仍然无法解决。

You can not do it. 你做不到。 The logic behind the Arguments object in WScript seems to remove quotes. WScript中Arguments对象背后的逻辑似乎删除了引号。

So, i can think in at least three alternatives 所以,我至少可以考虑三种选择

1) The most simple: use another character as an indicator of a quote and replace it with quote in the vbs script 1)最简单:使用另一个字符作为引号的指示符,并在vbs脚本中将其替换为引号

cmd code : cscript myscript.vbs "shutdown /r /m \\%%c /t %delay% /c '%message%'"
vbs code : WScript.CreateObject("WScript.Shell").Run replace(WScript.Arguments(0),"'",""""), 0, False

The only problem with it is at some point, probably, you will need to use the placeholder character as a real character. 唯一的问题是在某个时候,可能需要将占位符用作真实字符。

2) The most complex: use wmi to retrieve the current process id and from here retrieve the original command line of the script. 2)最复杂:使用wmi检索当前进程ID,并从此处检索脚本的原始命令行。 A lot of code, if interested, here at StackOverflow there are some nice samples. 如果有兴趣的话,很多代码在StackOverflow上有一些不错的示例。

3) The easy, fast, and unusual. 3)简单,快速且与众不同。 Use environment variables. 使用环境变量。 Save the command into a variable and pass the name of the variable to the script. 将命令保存到变量中,然后将变量名传递给脚本。 From it, retrieve the variable contents and use it 从中检索变量内容并使用它

cmd code : set "runVar=shutdown /r /m \\%%c /t %delay% /c "%message%""
           wscript //nologo "%windir%\myscripts\runInvisible.vbs" runVar

vbs code : With WScript.CreateObject("WScript.Shell") : .Run .ExpandEnvironmentStrings("%" & WScript.Arguments(0) & "%"), 0, False : End With
cmd code or bat code:
wscript //nologo invisible.vbs "hello world" 0

somehow the zero on the end, causes the part between quotes being seen as one argument still 不知何故,最后的零会导致引号之间的部分仍被视为一个参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM