English 中文(简体)
在参数中逃出% 传输给 Powershell
原标题:Escape % in parameter passed to Powershell

我有一个 MSBulild 脚本, 启动 PowerShell 可以在远程计算机上做一些工作, 所以您必须通过密码才能创建远程会话, 但有一个问题 - 如果密码包含% 字符, 那么解析错误 - % 缺失了 (例如: 密码% 字 - & gt; 密码,% - & gt; (没有) ) 。

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).

那么,我怎样才能解决这个问题呢?

MSBulid 部分

<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:	est.ps1 ;Test -password  `%25 }&quot;" />
    </Target>
</Project>

选项卡.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 &quot;&amp; {.  C:	est.ps1 ;Test -password  %25%25 }&quot;" />
问题回答

暂无回答




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签