使用 VBscript 隐藏 Powershell 窗口:-file 文件路径中的空格 [英] Hiding a Powershell window using VBscript: whitespace in the -file filepath

查看:30
本文介绍了使用 VBscript 隐藏 Powershell 窗口:-file 文件路径中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户 JPBlanc 和其他人针对这样的问题给出了很好的解决方案:

User JPBlanc and others have given a nice solution to such a problem with the solution:

Set Args = Wscript.Arguments
'MsgBox  "Chemin LDAP: " & Args(0)
'MsgBox  "Classe: " & Args(1)

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file c:\SlxRH\RhModif.ps1 " & chr(34) & Args(0) & chr(34) , 0

我已经测试过这样的解决方案,它运行良好.但是,我的问题涉及标识为-file"选项参数的文件路径中包含空格的情况;例如,如果上面的代码示例的文件路径为:

I have tested such a solution out and it works fine. However, my problem concerns the situation when the file path identified as the argument for the "-file" option has white space in it; as example, if the code example above had a file path of:

c:\Slx RH\RhModif.PS1

c:\Slx RH\RhModif.PS1

注意文件路径中的空格.问题是:如何正确转义文件路径.我还没有找到一种方法来做到这一点(使其发挥作用).

Observe the white space in the file path. The question is this: how to escape the file path correctly. I haven't found a way to do this (to make it work).

我在这个站点和一般的interweb"上进行了搜索,并且了解到(我认为)有两个级别的脚本解释正在进行:VBscript 和 Wscript.另一个非常好的例子出现在这里(链接到带有此标题的完整页面):

I have searched on this site and the general "interweb" and have learned (I think) that there are two levels of script interpretation going on: VBscript and the Wscript. Another very good example appears here (link to the full page with this title):

运行计划任务PowerShell 中的 WinForm GUI

Dim objShell
Set objShell=CreateObject("WScript.Shell")
strExpression="get-date | add-content c:\temp\test.txt"
strCMD="powershell -sta -noProfile -NonInteractive  -nologo -command " & Chr(34) & "&{" & strExpression &"}" & Chr(34) 
objShell.Run strCMD,0

我已经测试过了,它对我有用.虽然不使用-file"选项,但此示例给出(您会认为)将是为 strCMD 变量放置-command"或-file"输入的通用方法.但是,当我使用以下代码时,它不起作用:

I have tested this and it works for me. While not using the "-file" option, this example gives (what you would think) would be a generic method of putting a "-command" or "-file" input for the strCMD variable. However, when I use the following code, it does not work:

Dim objShell
Set objShell=CreateObject("WScript.Shell")

strExpression="C:\aP_RDB\Set Up\SimpleInsert.PS1"
strCMD="powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file " & Chr(34) & "&{" & strExpression &"}" & Chr(34) 

objShell.Run strCMD,0

在我上面的例子中,我基本上复制了上面的代码参考(和列出的链接),用我的文件路径(包括空格)替换了变量 strExpression 的 PS 命令,并通过替换了 strCMD 定义来更新-command"参数与-file"参数.但是,错误消息指向一个无法识别的参数c:\aP_RDB\Set",也就是说,它没有看到整个路径C:\aP_RDB\Set Up\SimpleInsert.PS1".似乎引号(或其他东西)的转义不起作用......更好地说,我没有正确做.

In my example above, I essentially copied the code reference above (and from the link listed), replaced the PS command with my file path (which includes white space) for the variable strExpression, and updated the strCMD definition by replacing the "-command" argument with the "-file" argument. However, the error message points to an unrecognized argument "c:\aP_RDB\Set" that is, it does not see the whole path "C:\aP_RDB\Set Up\SimpleInsert.PS1". It appears that the escaping of the quotes (or something) is not working... better said, I am not doing it correctly.

任何深思熟虑的建议将不胜感激.仅供参考:我在 Powershell 中尝试了 -WindowStyle Hidden 选项;我喜欢很多人发现这不是跨 Windows 平台的一致方法(更具体地说,我无法让它工作).

Any thoughtful advice would be greatly appreciated. FYI: I have tried the -WindowStyle Hidden option in Powershell; I like many have found that not to be a consistent approach across Windows platforms (more specifically for me: I could not get it to work).

推荐答案

感谢参考,这里有两种方法可以启动名称中带有空格的脚本:

Thanks for the reference, here are two ways to start scripts with white space in the name :

1) 使用双 "("")

1) using double " ("")

set Args = Wscript.Arguments
MsgBox "Chemin LDAP: " & Args(0)
'MsgBox "Classe: " & Args(1)

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file ""c:\temp\test arg.ps1"" " & chr(34) & Args(0) & chr(34) , 0

2) 在没有控制台的情况下编写自己的 PowerShell.exe,正如我在本文末尾所解释的那样 ""PowerShell.exe" ne sait pas démarrer sans afficher une fenêtre"(法语抱歉)

2) Consist in writing your own PowerShell.exe with no console, as I explain at the end of this article ""PowerShell.exe" ne sait pas démarrer sans afficher une fenêtre"(sorry in french)

这篇关于使用 VBscript 隐藏 Powershell 窗口:-file 文件路径中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆