首页 > 解决方案 > 调用 PowerShell 脚本时出现 Azure 扩展错误

问题描述

使用 PowerShell,我正在尝试将文本文件编码为字符串,并提供编码字符串作为输入以获取食谱名称和食谱详细信息。当我检查输出说明书名称和 Json 文件时,它们是字符串和有效的 json 文件。但是当我通过 Azure 扩展使用此代码时,我收到错误消息:

ConvertFrom-Json :传入的未终止字符串。
FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
#Encode the Input file
[string]$sStringToEncode=Get-Content "c:\temp\json.txt"
$sEncodedString=[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($sStringToEncode))
write-host -ForegroundColor Green "Encoded String:`n" $sEncodedString

#Decode text
$EncodedText = $sEncodedString.replace("\n","")
write-host "String after replacing \n:" $EncodedText
$DecodedText = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($EncodedText))

#Convert to Json file
$DecodedText | ConvertFrom-Json | ConvertTo-Json | Out-File c:\temp\DNAFiledata.json

#Taking out cookbook Name
$CookbookName = (Get-Content c:\temp\DNAFiledata.json | Out-String | ConvertFrom-Json).cookbook_name
$CookbookName | Out-File "c:\temp\3_CookBookName.txt"

#Taking out recipe details
$Datainput = (Get-Content -Raw -Path c:\temp\DNAFiledata.json | ConvertFrom-Json).chef_attributes | ConvertTo-Json
$Datainput | Out-File "c:\temp\4_Datainput.json"

$Datainput | Set-Content "c:\temp\$CookbookName.json"

请对此提出任何建议。

标签: jsonazurepowershell

解决方案


推荐阅读