Escape% in parameter passed to Powershell

I have an MSBuild script that runs PowerShell to do some work on a remote computer, so you need to pass a password to create a remote session, but there is a problem - if the password contains the% character, it is parsed incorrectly -% is missing (ex: pass% word → password, '%' → '' (nothing)).

If I create a variable in PowerShell script $ password = "pass% word", it works fine. I know that% foreach is in PowerShell, so I tried to escape from it with `- but that didn't help. I can also change the password, but this is not an option (at the moment).

So how can I solve this problem?

MSBuild Part

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Test"> <Target Name="Test"> <!-- %25 - it 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 } 
+4
source share
1 answer

In batch files, the percent sign can be "escaped" with a double percent sign (%%). So this will be the trick:

 <Exec Command="powershell &quot;&amp; {. 'C:\test.ps1';Test -password '%25%25'}&quot;" /> 
+4
source

Source: https://habr.com/ru/post/1414356/


All Articles