运行Powershell脚本,在VBS的路径中包含空格

时间:2021-04-02 02:21:29

Can someone help with this script please? Basically, it's being ran by vbs and the options are set for each user running the script.

有人可以帮助这个脚本吗?基本上,它由vbs运行,并为运行脚本的每个用户设置选项。

The script works as is on machines that support the dos 8.3 file system, but we also have quite a few imaged systems and do not have this ability...

该脚本在支持dos 8.3文件系统的机器上工作,但我们也有相当多的成像系统,并且没有这种能力......

So i'm fruitlessly trying to get the machines that do not support 8.3 to run this. Powershell does not like the space in the file path..., and I'm trying to run the script silently.

所以我毫无结果地试图让那些不支持8.3的机器运行它。 Powershell不喜欢文件路径中的空间......而且我正在尝试以静默方式运行脚本。

Objshell.Run "powershell.exe (gc c:\users\$env:USERNAME\Mydocu~1\Canadi~1\FileImportSettings.config) -replace 'temp','server\blahblah' | out-file c:\users\$env:USERNAME\Mydocu~1\Canadi~1\FileImportSettings.config",0

The comment was coming messy so I'm posting it further on to the original post..

评论变得混乱,所以我将其进一步发布到原帖。

What I had posted initially was part ofmy attempt at fixing the problem and it wouldn't run. This edited one does... But when I try changing it to doublequotes, the code no longer changes the username to %username%. I've tried running the command directly and cmd complains that out-file is not a recognised valid command.

我最初发布的是我尝试修复问题的一部分,它不会运行。这个编辑过的...但是当我尝试将其更改为双引号时,代码不再将用户名更改为%username%。我试过直接运行命令,cmd抱怨out-file不是公认的有效命令。

Wscript.echo "powershell.exe (gc ""c:\users\""$env:USERNAME""\Documents\Canadian...\FileImportSettings.config"") -replace 'temp','server\blahblah' | out-file ""c:\users\""$env:USERNAME""\Documents\Canadian....\FileImportSettings.config""",0

1 个解决方案

#1


Filenames with spaces in powershell.

Simple powershell command with no spaces in (8.3 aliased) path:

简单的powershell命令,在(8.3别名)路径中没有空格:

==>powershell (gc .\$env:USERNAME\yyyy.txt) -replace 'efg h','E FGH'
xxx1 abc D
xxx2 E FGH
xxx3 ijk L

Prepare for (long) file names with spaces in path (string concatenation). Single quote marks result in literal values being echoed back; double quote marks result in the actual value of a variable being echoed back):

准备路径中带有空格的(长)文件名(字符串连接)。单引号会导致字面值被回显;双引号会导致回显变量的实际值):

powershell (gc $("'.\'"+$env:USERNAME+"'\yyyy.txt'")) -replace 'efg h','E FGH'

Replace (8.3 aliased) path with (long) file names with spaces in path:

用(长)文件名替换(8.3别名)路径,路径中包含空格:

powershell (gc $("'.\'"+$env:USERNAME+"'\yy yy.txt'")) -replace 'efg h','E FGH'

Pipe: | Out-file.

We must escape the | pipe character in next command to forward it to PoverShell; otherwise, it applies in cmd shell with 'out-file' is not recognized as an internal or external command, operable program or batch file error.

我们必须逃避|下一个命令中的管道字符将其转发给PoverShell;否则,它适用于cmd shell,'out-file'不被识别为内部或外部命令,可操作程序或批处理文件错误。

powershell (gc $("'.\'"+$env:USERNAME+"'\yy yy.txt'")) -replace 'efg h','E FGH' ^| out-file $("'.\'"+$env:USERNAME+"'\yy yy.txt'")

VBScript.

Doubled all inner " double quote. Need to use absolute path for file name as for another working directory...

加倍所有内部“双引号。需要使用文件名的绝对路径作为另一个工作目录...

' 30064463
option explicit
Dim cmdLine, objShell
cmdLine = "powershell (gc $(""'D:\bat\'""+$env:USERNAME+""'\yy yy.txt'"")) -replace 'efg h','E FGH' ^| out-file $(""'D:\bat\'""+$env:USERNAME+""'\yy yy.txt'"")"
Wscript.Echo cmdLine
Set objShell = WScript.CreateObject("WScript.Shell")
Objshell.Run "cmd /C " & cmdLine, 1, true
Wscript.Quit

Output.

==>type  "D:\BAT\%username%\yy yy.txt"
xxx1 abc D
xxx2 efg H
xxx3 ijk L

==>cscript D:\VB_scripts\SO\30064463.vbs
powershell (gc $("'D:\bat\'"+$env:USERNAME+"'\yy yy.txt'")) -replace 'efg h','
E FGH' ^| out-file $("'D:\bat\'"+$env:USERNAME+"'\yy yy.txt'")

==>type  "D:\BAT\%username%\yy yy.txt"
xxx1 abc D
xxx2 E FGH
xxx3 ijk L

==>

#1


Filenames with spaces in powershell.

Simple powershell command with no spaces in (8.3 aliased) path:

简单的powershell命令,在(8.3别名)路径中没有空格:

==>powershell (gc .\$env:USERNAME\yyyy.txt) -replace 'efg h','E FGH'
xxx1 abc D
xxx2 E FGH
xxx3 ijk L

Prepare for (long) file names with spaces in path (string concatenation). Single quote marks result in literal values being echoed back; double quote marks result in the actual value of a variable being echoed back):

准备路径中带有空格的(长)文件名(字符串连接)。单引号会导致字面值被回显;双引号会导致回显变量的实际值):

powershell (gc $("'.\'"+$env:USERNAME+"'\yyyy.txt'")) -replace 'efg h','E FGH'

Replace (8.3 aliased) path with (long) file names with spaces in path:

用(长)文件名替换(8.3别名)路径,路径中包含空格:

powershell (gc $("'.\'"+$env:USERNAME+"'\yy yy.txt'")) -replace 'efg h','E FGH'

Pipe: | Out-file.

We must escape the | pipe character in next command to forward it to PoverShell; otherwise, it applies in cmd shell with 'out-file' is not recognized as an internal or external command, operable program or batch file error.

我们必须逃避|下一个命令中的管道字符将其转发给PoverShell;否则,它适用于cmd shell,'out-file'不被识别为内部或外部命令,可操作程序或批处理文件错误。

powershell (gc $("'.\'"+$env:USERNAME+"'\yy yy.txt'")) -replace 'efg h','E FGH' ^| out-file $("'.\'"+$env:USERNAME+"'\yy yy.txt'")

VBScript.

Doubled all inner " double quote. Need to use absolute path for file name as for another working directory...

加倍所有内部“双引号。需要使用文件名的绝对路径作为另一个工作目录...

' 30064463
option explicit
Dim cmdLine, objShell
cmdLine = "powershell (gc $(""'D:\bat\'""+$env:USERNAME+""'\yy yy.txt'"")) -replace 'efg h','E FGH' ^| out-file $(""'D:\bat\'""+$env:USERNAME+""'\yy yy.txt'"")"
Wscript.Echo cmdLine
Set objShell = WScript.CreateObject("WScript.Shell")
Objshell.Run "cmd /C " & cmdLine, 1, true
Wscript.Quit

Output.

==>type  "D:\BAT\%username%\yy yy.txt"
xxx1 abc D
xxx2 efg H
xxx3 ijk L

==>cscript D:\VB_scripts\SO\30064463.vbs
powershell (gc $("'D:\bat\'"+$env:USERNAME+"'\yy yy.txt'")) -replace 'efg h','
E FGH' ^| out-file $("'D:\bat\'"+$env:USERNAME+"'\yy yy.txt'")

==>type  "D:\BAT\%username%\yy yy.txt"
xxx1 abc D
xxx2 E FGH
xxx3 ijk L

==>