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 "& {. 'C:\test.ps1';Test -password '`%25'}"" />
</Target>
</Project>
test.ps1
Function Test {
param (
[string]$password = $(throw "Please specify password")
)
Write-Host $password
}
In batch files, the percent sign may be "escaped" by using a double percent sign ( %% ). So this will do the trick:
<Exec Command="powershell "& {. 'C:\test.ps1';Test -password '%25%25'}"" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With