[英]Running Powershell script with spaces in path from VBS
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. 因此,我无济于事地尝试让不支持8.3的计算机运行此程序。 Powershell does not like the space in the file path..., and I'm trying to run the script silently.
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%.
这个编辑过的确实可以...但是当我尝试将其更改为双引号时,代码不再将用户名更改为%username%。 I've tried running the command directly and cmd complains that out-file is not a recognised valid command.
我尝试直接运行命令,而cmd抱怨出文件不是公认的有效命令。
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
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'
We must escape the |
我们必须逃离
|
pipe character in next command to forward it to PoverShell
; 在下一个命令
PoverShell
竖线字符将其转发给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. 否则,它将在
cmd
shell中应用,并且'out-file' is not recognized as an internal or external command, operable program or batch file
错误。
powershell (gc $("'.\'"+$env:USERNAME+"'\yy yy.txt'")) -replace 'efg h','E FGH' ^| out-file $("'.\'"+$env:USERNAME+"'\yy yy.txt'")
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
==>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
==>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.