azure - 如何在Powershell Runbook中将JSON文件内容转换为Powershell对象?

标签 azure powershell azure-runbook

我正在尝试将存储帐户数据中存在的 JSON 文件转换为 Powershell 对象。但我没有得到正确的输出。输出不包含正确的值。

我的代码:

$storageAccountKey = "xxxx"
$Context = New-AzStorageContext -StorageAccountName 'xxxx' -StorageAccountKey $storageAccountKey
$b='C:\Temp\Scheduled.json'
Get-AzureStorageFileContent -Path $b -ShareName "file-share-name" 
$newScheduledRules =  Get-Content -Raw   $b | ConvertFrom-Json
Write-Output ("########newScheduledRules::###########" + $newScheduledRules)

输出:

Could not get the storage context.  Please pass in a storage context or set the current storage context.
########newScheduledRules::###########@{Scheduled=System.Object[]; Fusion=System.Object[]; MLBehaviorAnalytics=System.Object[]; MicrosoftSecurityIncidentCreation=System.Object[]}

最佳答案

我已经在我的环境中重现并得到了如下预期结果,并遵循以下过程并遵循 Microsoft-Document :

Scheduled.json:

{
"Rithwik":"Hello",
"Chotu":"Bojja"
}

enter image description here

首先,我创建了存储帐户,然后在文件共享中添加了 Scheduled.json,如下所示:

enter image description here

现在我已经创建了一个 Runbook 并在 Runbook 中执行了以下脚本,如下所示:

$storageAccountKey = "PFHxFbVmAEvwBM6/9kW4nORJYA+AStA2QQ1A=="
$Context = New-AzStorageContext -StorageAccountName 'rithwik' -StorageAccountKey $storageAccountKey
$out = "$($env:TEMP)\$([guid]::NewGuid())"
New-Item -Path $out -ItemType Directory -Force
Get-AzStorageFileContent -ShareName "rithwik" -Context $Context -Path 'Scheduled.json' -Destination $out -Force
$newScheduledRules = Get-Content -Path "$out\Scheduled.json" -Raw | ConvertFrom-Json
Write-Output ("########newScheduledRules::###########" + $newScheduledRules)

enter image description here

输出:

enter image description here

这里$out是目标变量。

-Path 应该仅是文件名 Scheduled.json Get-AzStorageFileContent 命令。

关于azure - 如何在Powershell Runbook中将JSON文件内容转换为Powershell对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75027703/

相关文章:

azure - 在 Azure 云服务上启用 OPTIONS 方法(以启用 CORS)

python - 如何使用 Python 检查虚拟机是否正在运行

c# - 如何在 Azure 中创建 SOAP WebService?

Azure Web应用程序克隆 powershell 脚本不起作用

PowerShell IComparable 与子类

excel - 以编程方式将最小值/最大值添加到 Excel 右下角的状态栏

azure - 命令 'Get-AzFunctionApp ' 在 Azure 自动化 Runbook 中不起作用

用于连接到 Azure VM 上运行的 SQL 服务器的 Azure Runbook

c# - 使用 Azure AD Graph 客户端获取 Active Directory 管理员

Azure 策略仅允许创建特定标签