我有一个字符串。有时看起来像这样:
9xABp'} H9 $ G(@
虽然有时看起来像这样:
9xABp“} H9 $ G(@
我对用于生成字符串的字符集没有任何控制,但是我需要让Powershell停止抱怨无法解析该字符串并将所有字符都交给我。
$string = '9xABp'}H9$G(@'
$secure = ConvertTo-SecureString -String $string -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
这不起作用,因此我尝试将字符串用双引号而不是单引号引起来。
$string = "9xABp'}H9$G(@"
$secure = ConvertTo-SecureString -String $string -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
很好,但是不包括$ G(用反斜杠代替),并且当我的字符串中包含双引号时该怎么办?
我尝试使用[Regex] :: Escape()。
$string = "9xABp'}H9$G(@"
$secure = ConvertTo-SecureString -String ([Regex]::Escape($string)) -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
但是$ G仍然丢失。另一种尝试,这次在外面用双引号和单引号引起来。
$string = "'9xABp'}H9$G(@'"
$secure = ConvertTo-SecureString -String ([Regex]::Escape($string)) -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
我在这里可以做什么?
PowerShell此处字符串仅在这种情况下存在。