3

User JPBlanc and others have given a nice solution to such a problem with the solution:

Set Args = Wscript.Arguments
'MsgBox  "Chemin LDAP: " & Args(0)
'MsgBox  "Classe: " & Args(1)

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file c:\SlxRH\RhModif.ps1 " & chr(34) & Args(0) & chr(34) , 0

I have tested such a solution out and it works fine. However, my problem concerns the situation when the file path identified as the argument for the "-file" option has white space in it; as example, if the code example above had a file path of:

c:\Slx RH\RhModif.PS1

Observe the white space in the file path. The question is this: how to escape the file path correctly. I haven't found a way to do this (to make it work).

I have searched on this site and the general "interweb" and have learned (I think) that there are two levels of script interpretation going on: VBscript and the Wscript. Another very good example appears here (link to the full page with this title):

Run scheduled tasks with WinForm GUI in PowerShell

Dim objShell
Set objShell=CreateObject("WScript.Shell")
strExpression="get-date | add-content c:\temp\test.txt"
strCMD="powershell -sta -noProfile -NonInteractive  -nologo -command " & Chr(34) & "&{" & strExpression &"}" & Chr(34) 
objShell.Run strCMD,0

I have tested this and it works for me. While not using the "-file" option, this example gives (what you would think) would be a generic method of putting a "-command" or "-file" input for the strCMD variable. However, when I use the following code, it does not work:

Dim objShell
Set objShell=CreateObject("WScript.Shell")

strExpression="C:\aP_RDB\Set Up\SimpleInsert.PS1"
strCMD="powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file " & Chr(34) & "&{" & strExpression &"}" & Chr(34) 

objShell.Run strCMD,0

In my example above, I essentially copied the code reference above (and from the link listed), replaced the PS command with my file path (which includes white space) for the variable strExpression, and updated the strCMD definition by replacing the "-command" argument with the "-file" argument. However, the error message points to an unrecognized argument "c:\aP_RDB\Set" that is, it does not see the whole path "C:\aP_RDB\Set Up\SimpleInsert.PS1". It appears that the escaping of the quotes (or something) is not working... better said, I am not doing it correctly.

Any thoughtful advice would be greatly appreciated. FYI: I have tried the -WindowStyle Hidden option in Powershell; I like many have found that not to be a consistent approach across Windows platforms (more specifically for me: I could not get it to work).

Dave Ef
  • 71
  • 2
  • 5
  • Untested, because I only have PS v1. but try this: `strCMD="powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file " & Chr(34) & "&{'" & strExpression &"'}" & Chr(34)` – Daniel Dec 24 '12 at 03:39
  • Thanks Daniel - that did not appear to work, but I looked at it further and tried the following and it worked: `strCMD="powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file " & CHR(34) & strExpression & CHR(34)`@DanielCook – Dave Ef Dec 24 '12 at 04:00

1 Answers1

1

Thanks for the reference, here are two ways to start scripts with white space in the name :

1) using double " ("")

set Args = Wscript.Arguments
MsgBox "Chemin LDAP: " & Args(0)
'MsgBox "Classe: " & Args(1)

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file ""c:\temp\test arg.ps1"" " & chr(34) & Args(0) & chr(34) , 0

2) Consist in writing your own PowerShell.exe with no console, as I explain at the end of this article ""PowerShell.exe" ne sait pas démarrer sans afficher une fenêtre"(sorry in french)

JPBlanc
  • 70,406
  • 17
  • 130
  • 175