Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape % in parameter passed to Powershell

I have a MSBuild script that starts PowerShell to perform some work on remote computer so you have to pass password to create remote session, but there is issue - if password contains % character it is parsed wrong - % is missing (ex: pass%word -> password, '%' -> ''(nothing)).

If I'm creating variable in PowerShell script $password = "pass%word" it works fine. I know % is foreach in PowerShell so I tried to escape it using ` - but it didn't helped. Also I can change password, but thats not an option (for now).

So how can I solve this issue?

MSBuild part

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Test">
<Target Name="Test">
        <!-- %25 - it's code for %-->
        <Exec Command="powershell &quot;&amp; {. 'C:\test.ps1';Test -password '`%25'}&quot;" />
    </Target>
</Project>

test.ps1

Function Test {
    param (
        [string]$password = $(throw "Please specify password")
    )

    Write-Host $password
}
like image 771
Ash Avatar asked Oct 07 '22 17:10

Ash


1 Answers

In batch files, the percent sign may be "escaped" by using a double percent sign ( %% ). So this will do the trick:

<Exec Command="powershell &quot;&amp; {. 'C:\test.ps1';Test -password '%25%25'}&quot;" />
like image 157
jon Z Avatar answered Oct 13 '22 10:10

jon Z