使用 Azure Powershell Runbook 将输出变量结果写入 Azure 存储帐户中的新 .parquet 或 .csv 文件

问题描述 投票:0回答:2

美好的一天,

我有以下 MDXQuery 返回 2 列

SELECT[TableID], [Name] FROM $SYSTEM.TMSCHEMA_PARTITIONS

想法是使用 Azure Powershell runbook 创建 csv 或 parque 文件并将其保存到特定的容器和文件夹中。 powershell 代码必须执行以下步骤:

1-执行DMX查询获取数据

2-将结果作为 XML 解析到名为 $Results

的变量

3-Write-Output 来自变量 $Results 的结果并创建一个 csv 或 .parquet 文件

4-确保文件进入特定的 Azure 存储帐户

我无法完成最后两个步骤,因为我不知道怎么做,我正在考虑使用 Set-AzStorageBlobContent 但基于 Microsoftdetails 这更多是将本地文件上传到 Azure 存储 blob 而不是使用 XML 的结果从头开始创建文件的变量。

这是我在 Azure runbook 中的脚本

Param
(
  [Parameter (Mandatory= $true)]
  [String] $Query,
  [Parameter (Mandatory= $true)]
  [String] $OperationType,
  [Parameter (Mandatory= $true)]
  [String] $ServerName,
  [Parameter (Mandatory= $true)]
  [String] $DatabaseName,
  [Parameter (Mandatory= $true)]
  [String] $SecretName,
  [Parameter (Mandatory= $true)]
  [String] $ResourceGroupName,
  [Parameter (Mandatory= $true)]
  [String] $StorageAccountName

)

$cred = Get-AutomationPSCredential -Name $SecretName

$Results = [XML] (Invoke-ASCmd -Server $ServerName -Database $DatabaseName -Credential $cred -Query $Query)

Write-Output  $Results.return.root.row

# Connect using a Managed Service Identity
try {
        $AzureContext = (Connect-AzAccount -Identity).context
    }
catch{
        Write-Output "There is no system-assigned user identity. Aborting."; 
        exit
    }

Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName

输出结果是 3 列,除非 MDX 查询添加更多但我需要使用它来创建具有相同数量的列和数据的文件。需要在 powershell 代码指定的 Azure 存储帐户中创建文件

azure-powershell azure-runbook
2个回答
0
投票

我无法完成最后两步,因为我不知道怎么做

我在我的环境中重现并得到如下预期结果,我遵循了Microsoft-Document1Microsoft-Documnet2

这里 $r 包含一个表。

$s = "rithwik"
$c = "rithwik"
$k = "rCQR+OahV3cWz8sPK5GLS0hT/C+AStNYbM6Q=="
$Context = New-AzStorageContext -StorageAccountName $s -StorageAccountKey $k

$r=Get-Module
$r | Export-csv -Path emo.csv -NoTypeInformation
Set-AzStorageBlobContent -File "emo.csv" -Container $c -Blob "emo.csv" -Context $Context

enter image description here

这里$s是存储账户名

$c 是容器名称

$k 是存储账户密钥

输出:

enter image description here

文件已作为 csv 文件导出到存储帐户。

enter image description here

内容:

enter image description here

通过上面的代码/流程,您可以将内容设置为csv,然后将其导出到特定的存储帐户。


0
投票

出于安全考虑,我不能使用存储密钥。我必须使用管理身份。我删除了一些不需要的变量并添加了额外的变量以创建具有不同数据的第二个文件。

    Param
(
  [Parameter (Mandatory= $true)]
  [String] $ServerName,
  [Parameter (Mandatory= $true)]
  [String] $DatabaseName,
  [Parameter (Mandatory= $true)]
  [String] $SecretName,
  [Parameter (Mandatory= $true)]
  [String] $StorageAccountName,
  [Parameter (Mandatory= $true)]
  [String] $Query,
  [Parameter (Mandatory= $true)]
  [String] $Query2

)

$cred = Get-AutomationPSCredential -Name $SecretName

$Results = [XML] (Invoke-ASCmd -Server $ServerName -Database $DatabaseName -Credential $cred -Query $Query)
$report = Write-Output  $Results.return.root.row
$report | Export-CSV -Encoding ASCII ($env:TEMP+"AS_PartitionsData.csv") -Notype -Delimiter "`t" | % {$_ -replace '"', ""} 

$Results2 = [XML] (Invoke-ASCmd -Server $ServerName -Database $DatabaseName -Credential $cred -Query $Query2)
$report2 = Write-Output  $Results2.return.root.row
$report2 | Export-CSV -Encoding ASCII ($env:TEMP2+"AS_TableNamesData.csv") -Notype -Delimiter "`t" | % {$_ -replace '"', ""} 


# Connect using a Managed Service Identity
try {
        $AzureContext = (Connect-AzAccount -Identity).context
    }
catch{
        Write-Output "There is no system-assigned user identity. Aborting."; 
        exit
    }

$Context = New-AzStorageContext -StorageAccountName $StorageAccountName

Set-AzStorageBlobContent -Context $Context -Container "mapping" -File ($env:TEMP+"AS_PartitionsData.csv") -Blob "AS_PartitionsData.csv" -Force 
Set-AzStorageBlobContent -Context $Context -Container "mapping" -File ($env:TEMP2+"AS_TableNamesData.csv") -Blob "AS_TableNamesData.csv" -Force
© www.soinside.com 2019 - 2024. All rights reserved.