从 VBS 的路径中运行带有空格的 Powershell 脚本 [英] Running Powershell script with spaces in path from VBS

查看:36
本文介绍了从 VBS 的路径中运行带有空格的 Powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮忙处理这个脚本吗?基本上,它由 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.

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

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...

所以我徒劳地试图让不支持 8.3 的机器运行它.Powershell 不喜欢文件路径中的空格...,我正在尝试静默运行脚本.

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.

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..

我最初发布的内容是我尝试解决问题的一部分,但它无法运行.这个编辑过的确实......但是当我尝试将其更改为双引号时,代码不再将用户名更改为 %username%.我试过直接运行该命令,但 cmd 抱怨 out-file 不是可识别的有效命令.

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.

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

推荐答案

powershell 中带空格的文件名.

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

Filenames with spaces in powershell.

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

==>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'

用路径中带空格的(长)文件名替换(8.3别名)路径:

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

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

管道: |外档.

我们必须在下一个命令中转义 | 管道字符以将其转发到 PoverShell;否则,它适用于 cmd shell,'out-file' is not识别为内部或外部命令,可运行的程序或批处理文件错误.

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.

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

VBScript.

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

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

输出.

==>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

==>

这篇关于从 VBS 的路径中运行带有空格的 Powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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