从 Azure Automation PowerShell Runbook 中的 InvokeAscmd 读取输出

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

我想读取 AAS 表格的上次刷新时间的输出,然后如果当天刷新成功则发送电子邮件。

下面的 MDX 查询用于获取相同的内容

通过下面的 powershell 脚本运行相同的查询 -

$AzureCred = Get-AutomationPSCredential -Name "AzOps.Azure.Group.PROD.EDAP.SA"

$database = "SCP"
$Query = @"
SELECT RefreshedTime
FROM $SYSTEM.TMSCHEMA_PARTITIONS
where TableID=10
"@

$result=(Invoke-ASCmd ` -ServicePrincipal ` -Credential $Azurecred ` -TenantId "95e66ecc-f2c2-464b-84d9-8fda407bc923" ` -Server "asazure://westeurope.asazure.windows.net/asedapprodscp" ` -Database "$database" ` -Query $Query ` -Confirm:$False)

echo $result

以下是输出 -

<return xmlns="urn:schemas-microsoft-com:xml-analysis"><root xmlns="urn:schemas-microsoft-com:xml-analysis:mddataset" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:schema targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset" xmlns:sql="urn:schemas-microsoft-com:xml-sql" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:element name="root"><xsd:complexType><xsd:sequence minOccurs="0" maxOccurs="unbounded"><xsd:element name="row" type="row" /></xsd:sequence></xsd:complexType></xsd:element><xsd:simpleType name="uuid"><xsd:restriction base="xsd:string"><xsd:pattern value="[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}" /></xsd:restriction></xsd:simpleType><xsd:complexType name="xmlDocument"><xsd:sequence><xsd:any /></xsd:sequence></xsd:complexType><xsd:complexType name="row"><xsd:sequence><xsd:element sql:field="RefreshedTime" name="RefreshedTime" type="xsd:dateTime" minOccurs="0" /></xsd:sequence></xsd:complexType></xsd:schema><row xmlns="urn:schemas-microsoft-com:xml-analysis:rowset"><RefreshedTime>2023-10-12T09:57:32.556667</RefreshedTime></row></root></return>

如何从中获取 RefreshedTime? 尝试将输出转换为 XML 以及下面的内容 - 更新的脚本 -

$AzureCred = Get-AutomationPSCredential -Name "AzOps.Azure.Group.PROD.EDAP.SA"

$database = "SCP"
$Query = @"
SELECT RefreshedTime
FROM $SYSTEM.TMSCHEMA_PARTITIONS
where TableID=10
"@

$result=[xml](Invoke-ASCmd ` -ServicePrincipal ` -Credential $Azurecred ` -TenantId "95e66ecc-f2c2-464b-84d9-8fda407bc923" ` -Server "asazure://westeurope.asazure.windows.net/asedapprodscp" ` -Database "$database" ` -Query $Query ` -Confirm:$False)

echo $result.return.root.row.Name

输出-

azure powershell automation mdx azure-analysis-services
1个回答
0
投票

您的解决方法在您将其转换为

XML
之前一直有效。然而, 当检索刷新时间时,您需要将其捕获为
$result.root.row.RefreshedTime
。它使您可以访问并显示
Refreshed Time
值,如图所示。

$TenantId= "xxxxx"
$Password = ConvertTo-SecureString "xxxx" -AsPlainText -Force
$AplicationId ="xxxxx"
$psCred = New-Object System.Management.Automation.PSCredential($AplicationId , $Password)
$database = "xxxdata"
$Query = @"
SELECT Refreshedtime
FROM $SYSTEM.table_jahn
where TableID=1
"@
$result=[xml](Invoke-ASCmd -ServicePrincipal -Credential $psCred -TenantId $TenantId -Server "jahnserver.database.windows.net" -Database $database -Query $Query -Confirm:$False)
$refreshedTime = $result.root.row.Refreshedtime
Write-Host "$refreshedTime"

enter image description here

在自动化运行手册中尝试相同的脚本来实现您的要求。

© www.soinside.com 2019 - 2024. All rights reserved.