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
Alin P
  • 13
  • 4
  • 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")` – JosefZ May 05 '15 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. – TheMadTechnician May 06 '15 at 01: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. – Alin P May 06 '15 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`... – JosefZ May 06 '15 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... – JosefZ May 06 '15 at 20:31
  • $env:username is not in double quotes... the rest of it is.. `""c:\users\""` , `""\Documents\Canadian...\FileImportSettings.config""` I thought that's what you suggested I do... When I switch it to `powershell.exe (gc "c:\users\$env:USERNAME\Documents\Canadian..... ` I get: positional parameter cannot be found 'second word after canadian' – Alin P May 07 '15 at 15:55

1 Answers1

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

==>
JosefZ
  • 28,460
  • 5
  • 44
  • 83