Arrays Powershell convertfrom-转换为base64后出现Json错误
我试图在powershell中执行以下操作,但出现了一个无法找出原因的错误 下面的工作很好Arrays Powershell convertfrom-转换为base64后出现Json错误,arrays,powershell,base64,Arrays,Powershell,Base64,我试图在powershell中执行以下操作,但出现了一个无法找出原因的错误 下面的工作很好 $config = @" { "Common.BinDir": "G:\result", "Infrastructure.WebRoot": "G:\result20171120" } "@ $abc = ConvertFrom-Json $testconfig 但当我传入上面的base64时(我使用的脚本期望base64) 在运行convert命令时,我得到以下错误 ConvertF
$config = @"
{
"Common.BinDir": "G:\result",
"Infrastructure.WebRoot": "G:\result20171120"
}
"@
$abc = ConvertFrom-Json $testconfig
但当我传入上面的base64时(我使用的脚本期望base64)
在运行convert命令时,我得到以下错误
ConvertFrom-Json : Invalid JSON primitive: .
At line:1 char:8
+ $abc = ConvertFrom-Json $decodedConfig
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json],
ArgumentException
+ FullyQualifiedErrorId :
System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
对于给定的
$config
,以下代码段应按预期工作:
$config = "QCINCnsNCgkiQ29tbW9uLkJpbkRpciI6ICJHOlxyZXN1bHQiLA0KCSJJbmZyYXN0cnVjdHVyZS5XZWJSb290IjogIkc6XHJlc3VsdDIwMTcxMTIwIg0KfQ0KIkA="
$decodedConfig = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($config))
$abc = ConvertFrom-Json -InputObject $(
Invoke-Expression -Command $decodedConfig.Replace('\', '\\')
)
我相信有两件事
1) 从base64解码的字符串包含@“和”@-作为字符串的一部分,而不是限定符-所以
$decodedString
是:
@"
{
"Common.BinDir": "G:\result",
"Infrastructure.WebRoot": "G:\result20171120"
}
"@
{
"Common.BinDir": "G:\result",
"Infrastructure.WebRoot": "G:\result20171120"
}
而$config
是:
@"
{
"Common.BinDir": "G:\result",
"Infrastructure.WebRoot": "G:\result20171120"
}
"@
{
"Common.BinDir": "G:\result",
"Infrastructure.WebRoot": "G:\result20171120"
}
以下内容适用于您的情况(尽管必须有更好的方法)
2) 您需要在.Json文件中屏蔽\
,因此需要有效地使用\
,因此实际上,您的Json应该如下所示:
@"
{
"Common.BinDir": "G:\\result",
"Infrastructure.WebRoot": "G:\\result20171120"
}
"@
有什么方法可以改变“{”Common.BinDir:“G:\result”,“Infrastructure.WebRoot:“G:\result20171120”}”@,而不是更改解码的配置或abc代码,因为这些代码是脚本的一部分,我无法更改。谢谢您指出问题,解码下面的{”Common.BinDir:“G:\\result”,“Infrastructure.WebRoot:“G:\\result20171120”}帮助
$testconfig
(在前一个示例中)和$config
(在后一个示例中)来自何处?请回答与规则匹配的问题。