Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The word "encode" in a Wscript.Shell Exec does prevent powershell script from running. What can I do?

after a day close to insanity as of why my script wouldn't work on the data provided by the customer, I figured out, that it is the word "encode" somewhere enclosed in double and single quotes in my command, that prevents Wscript.Shell from opening powershell (in which the command works as expected). The german languages' feature to append words like "Marke" and "Code" by adding a "n", hence "Markencode" allows for a lot of words containing "encode". :/

I've created some minimal vba example to show the issue. Does anyone know a way around it?

Option Explicit
Const HK = """"

Sub ScanDrive()
    Dim s As String, command As String
    Dim oExec As Object
    command = "echo 'encode'"
    Debug.Print (command)
    Set oExec = CreateObject("Wscript.Shell").Exec("powershell.exe -command " & HK & command & HK)
    
    Do While oExec.Status = 0
       Application.Wait (Now + TimeValue("0:00:01"))
    Loop
    Debug.Print (oExec.ExitCode)
    s = oExec.StdOut.ReadAll
    Debug.Print (s)
End Sub

The output is (VBA Version 7.1.1108 in Excel):

echo 'encode'
-536870873 


Unfortunately I couldn't find anything for that exit code. Btw...putting in 'decode' instead of 'encode' works fine.

like image 426
Daniel Kaupp Avatar asked Nov 07 '22 00:11

Daniel Kaupp


1 Answers

As this seems to be no general problem, I worked around it by splitting the string between "en" and "code" and then let powershell put it back togheter.


'...
pos = InStr(fileName, "encode")
            If pos > 0 Then
                Dim l, r As String
                l = Left(fileName, pos + 1) & "'"
                r = "'" & Right(fileName, Len(fileName) - (pos + 1))
                'Split filename after "en" and before "code"
                fileName = "-join(" & l & " , " & r & " )"
                Debug.Print "Had to split the filename: " & fileName
            End If
'...

Perhaps this helps someone :)

like image 187
Daniel Kaupp Avatar answered Dec 19 '22 01:12

Daniel Kaupp