简体   繁体   English

在VBS路径中使用空格运行Powershell脚本

[英]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

Filenames with spaces in powershell. 在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 ; 在下一个命令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'")

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

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

==>

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

相关问题 从路径中有空格的快捷方式运行 PowerShell 脚本 - Running PowerShell Script from shortcut with spaces in path 脚本路径中的Powershell空间 - Powershell spaces in script path 在不使用反引号的情况下运行具有包含Jenkins Pipeline中空格的路径的PowerShell脚本文件 - Running a PowerShell script file with path containing spaces from Jenkins Pipeline without using backtick 从 Excel 运行 powershell 脚本,路径中有空格 - Running a powershell script from Excel with space in path 使用命令作为参数从 vbs 运行 Powershell - Running Powershell from vbs with command as parameter 如何在带有空格的路径中运行 PowerShell 脚本? - How to run a PowerShell script in a path with spaces? 如何从VBS登录脚本调用Powershell复制脚本 - How to Call Powershell Copy-Script from VBS Logon Script 使用快捷方式的管理员权限在 UNC 路径上运行 PowerShell 脚本 - Running A PowerShell Script At A UNC Path With Admin Privilege From Shortcut 当脚本所在的文件夹的名称带有空格时,从任务计划程序运行Powershell脚本 - Running Powershell script from task scheduler when the name of the folder where script is kept has spaces 当ps脚本路径包含空格并且路径从%〜dp0%派生时,如何从批处理文件运行powershell脚本 - How to run powershell script from batch file while the ps script path has spaces and path is derived from %~dp0%
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM