1

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.

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

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

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
6
  • 1
    You need double all inner ". Debug with Wscript.Echo instead of Objshell.Run attempts. For instance, Wscript.Echo "aaa (gc ""path 1"" ""path 2"")" will result to aaa (gc "path 1" "path 2") Commented May 5, 2015 at 23:47
  • 2
    @JosefZ Make that an answer man, that's the whole issue with his script. He either needs to escape his double quotes by doubling up on the interior ones, or he needs to use single quotes on the outside, and double quotes on the inside. Commented May 6, 2015 at 1:09
  • The doubling of interior quotes didn't work. The code no longer makes the changes to the config files... Further, I'm not even sure how to run it manually as cmd detects | and thinks out-file is a new command. I added to the initial question more details. Commented May 6, 2015 at 18:06
  • Escape the | with ^ as follows: ... blah' ^| out-file .... As a matter of general principle, you need to ensure your command will run from pure CLI before scripting it in VBScript... Commented May 6, 2015 at 18:43
  • And I don't understand why do you enclose $env:USERNAME in double quotes?. IMHO "c:\users\$env:USERNAME\Documents\Canadian...\FileImportSettings.config" should constitute right full path of your file... Commented May 6, 2015 at 20:31

1 Answer 1

0

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'

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'

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.

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

==>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.