Jump to content

Need help escaping % character in string


Recommended Posts

Hi,

I have an au3 script that executes a powershell script with arguments. The code works fine except when I need to pass a string parameter to powershell script that has the format "%%TOKEN%%" (1 double quote, 2 % characters, 1 double quote). To escape the double quotes, I passed the string """%%TOKEN%%""" to the function. The double quotes are passed to the called script. However, only 1 % sign gets passed to the script. What is the escape character I need to use to pass 2 % characters in my string?

Func A()

        B("""%%TOKEN""")

EndFunc

 

FUNC B($searchstring)

        Local $PowerShellArguments = StringFormat('-Path "%s" -SearchValue "%s" -ReplaceValue "%s"', $File, $searchstring, $ReplaceValue)

        ExecutePowershell($PowerShellArguments)

EndFunc

 

Func ExecutePowershell($Arguments)

         Dim $_ResultsArray = _RunReadStd($Arguments, 0, @TempDir, @SW_SHOW, -1, @CRLF)

EndFunc

 

Can someone please provide pointers/suggestions/advice?

Link to comment
Share on other sites

wrap your doublequoted string in single quotes

$sEcho = '"%%TOKEN%%"'

run('cmd /k powershell echo ' & $sEcho)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Thank you iamtheky for responding.

I tried with single quotes. Now the double quotes are gone too. :(

Only 1 % character in the string parameter is getting passed to FUNC B from Func A. 

 

Link to comment
Share on other sites

hmm, we are going to be doing some guessing without a proper script to reproduce this behavior.  But you may also get different behavior by wrapping the variable in the command, rather than trying to pre-wrap the variable

 Powershell seems to be the one sanitizing your input, as these quotes disappear in the echo too.

run('cmd /k powershell echo ' & '"' & $sEcho & '"')

 

the funcs seem to all play nicely and treat the string as just a string (no matter which fantastical way you decide to organize the quotes).

 

$sEcho = '"' & '%%TOKEN%%' & '"'

_FuncA($sEcho)

Func _FuncA($sShow)
    _show($sShow)
EndFunc

Func _show($string)
    msgbox(0, '' , $string)
EndFunc

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I stripped away code from the au3 file that I had and the skeleton of the code looks like this:


FindAndReplace('"""%%TOKEN%%"""', "abc")

Func FindAndReplace($SearchValue, $ReplaceValue)

    ConsoleWrite("SearchValue:" & $SearchValue & @CRLF)
    Local $PowerShellArguments = StringFormat('-Path "%s" -SearchValue %s -ReplaceValue "%s"', 'C:\temp\test.js', '"%%TOKEN%%",', $ReplaceValue)
    Local $result = ExecutePowerShellScript($PowerShellArguments)

EndFunc

Func ExecutePowerShellScript($ScriptParameters)
    Dim $PS_TokenConfigScript = "FindAndReplace.ps1"

    Dim $PowerShellExe = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"

    Dim $CommandArguments = StringFormat('%s -NoLogo -NoProfile -NonInteractive -ExecutionPolicy ByPass -File "%s" %s', $PowerShellExe, $PS_TokenConfigScript, $ScriptParameters)
    ConsoleWrite("CommandArguments:" & $CommandArguments & @CRLF)
    Dim $_ResultsArray = _RunReadStd($CommandArguments, 0, @TempDir, @SW_HIDE, -1, @CRLF)
EndFunc

Func _RunReadStd($sDoscmd, $nTimeoutSeconds = 0, $sWorkingdir = @ScriptDir, $nFlag = @SW_HIDE, $nRetVal = -1, $sDelim = @TAB)

    Local $aReturn, $i_Pid, $h_Process, $i_ExitCode, $sStdOut, $sStdErr, $runTimer
    Dim $aReturn[3]

    $i_Pid = Run($sDoscmd, $sWorkingdir, $nFlag, 6); 6 = $STDERR_CHILD+$STDOUT_CHILD

    ; Get process handle
    Sleep(100); or DllCall may fail - experimental
    $h_Process = DllCall('kernel32.dll', 'ptr', 'OpenProcess', 'int', 0x400, 'int', 0, 'int', $i_Pid)

    ; wait for process to exit, or kill it if it exceeds $timeoutSeconds
    While ProcessExists($i_Pid)
        Sleep(100)
        If $nTimeoutSeconds > 0 Then
            If TimerDiff($runTimer) / 1000 > $nTimeoutSeconds Then
                ProcessClose($i_Pid)
            EndIf
        EndIf
    WEnd


    ; fetch exit code and close process handle
    If IsArray($h_Process) Then
        Sleep(100); or DllCall may fail - experimental
        Local $e = DllStructCreate("struct;dword dw;endstruct")
        $i_ExitCode = DllCall('kernel32.dll', 'ptr', 'GetExitCodeProcess', 'hwnd', $h_Process[0], 'int_ptr', DllStructGetPtr($e))
        $aReturn[0] = DllStructGetData($e, "dw")
        Sleep(100); or DllCall may fail - experimental
        DllCall('kernel32.dll', 'ptr', 'CloseHandle', 'ptr', $h_Process[0])
    Else
        $aReturn[0] = -2
    EndIf

    Return $aReturn
EndFunc   ;==>_RunReadStd

Link to comment
Share on other sites

first, use code tags when you post.  its the little icon in the bar next to the smiley that looks like <>

quotes are indeed kicking my ass too now, but i cant reproduce the percent sign being stripped...is the ps1 doing anything spectacular?  Do you see only one percent sign in this small rep?

$PowerShellExe = "testexe"
$PS_TokenConfigScript = "testtoken"
$ReplaceValue = "testreplace"


$ScriptParameters = StringFormat('-Path "%s" -SearchValue %s -ReplaceValue "%s"', 'C:\temp\test.js', '"%%TOKEN%%",', $ReplaceValue)


$sOut = StringFormat('%s -NoLogo -NoProfile -NonInteractive -ExecutionPolicy ByPass -File "%s" %s', $PowerShellExe, $PS_TokenConfigScript, $ScriptParameters)


run("cmd /k powershell echo " & $sOut)

 

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...