0

I run the following command to process a database/and its partitions.

Invoke-ASCmd –InputFile $file -Server $Server >$output

from the output, if there is an error, this is whats displayed:

<return xmlns="urn:schemas-microsoft-com:xml-analysis"><root xmlns="urn:schemas-microsoft-com:xml-analysis:empty"><Excep
tion xmlns="urn:schemas-microsoft-com:xml-analysis:exception" /><Messages xmlns="urn:schemas-microsoft-com:xml-analysis:
exception"><Error ErrorCode="-1055653884" Description="Either the dimension with the ID of &#39;0f585685&#39; does not exist in the database with the ID of &#39;&#39;, or the u
ser does not have permissions to access the object." Source="Microsoft SQL Server 2017 Analysis Services" HelpFile="" />
</Messages></root></return>

can i extract the description part since its the most useful?

i want to extract it to have meaningful email messages with errors that can be understood right away without having to search through the whole other XML mess

$Email_Body = Get-Content -Path $output | Out-String

essentially, the email would have:

Either the dimension with the ID of '0f585685' does not exist in the database with the ID of '', or the u ser does not have permissions to access the object.

also some outputs sometimes contain multiple errors/descriptions.

how would i handle that?

for example:

<return xmlns="urn:schemas-microsoft-com:xml-analysis"><root xmlns="urn:schemas-microsoft-com:xml-analysis:empty"><Exception xmlns="urn:schemas-microsoft-com:xml-analysis:exception" /><Messages xmlns="urn:schemas-microsoft-com:xml-analysis:exception"><Error ErrorCode="-1055784933" Description="[Teradata Database] [8017] The UserId, Password or Account is invalid.. The exception was raised by the IDbConnection interface." Source="Microsoft SQL Server 2017 Analysis Services" HelpFile="" /><Error ErrorCode="-1055784860" Description="A connection could not be made to the data source with the DataSourceID of &#39;&#39;, Name of &#39;Teradata &#39;." Source="Microsoft SQL Server 2017 Analysis Services" HelpFile="" /></Messages></root></return>

can all descriptions be stored as part of $Email_body and sent? so in this case, it would be:

[Teradata Database] [8017] The UserId, Password or Account is invalid.. The exception was raised by the IDbConnection interface.

A connection could not be made to the data source with the DataSourceID of '', Name of 'Teradata '.

AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
Cataster
  • 3,081
  • 5
  • 32
  • 79

1 Answers1

3

You can capture it as XML, then just drill down to the part you want to see:

[xml]$Result = Invoke-ASCmd –InputFile $file -Server $Server
$Result.return.root.Messages.Error.description | Set-Content $output

I'm assuming that $output is a file path the way you used it in your example. Now all that $output will contain is the relevant error descriptions.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • ohhh you can cast as xml! interesting! – Cataster Mar 11 '19 at 20:45
  • what id there is multiple descriptions outputted? – Cataster Mar 11 '19 at 21:01
  • @Cataster: It outputs them all, as an array. Using proper XML parsing is the right solution - there is no need to resort to brittle regex-based string parsing. – mklement0 Mar 11 '19 at 21:38
  • @mklement0 instead of setting [xml]$Result to the command, can i just output it AFTER the command executes? because when you set it its not gonna execute...i dont see how it would. maybe outputting this way Invoke-ASCmd –InputFile $file -Server $Server > [xml]$Result – Cataster Mar 11 '19 at 21:44
  • 1
    No, you'd need a `ForEach-Object` (`%`) call for that: `(Invoke-ASCmd –InputFile $file -Server $Server | % { [xml] $_ }).return.root.Messages.Error.description`; alternatively (faster): `([xml] (Invoke-ASCmd –InputFile $file -Server $Server)).return.root.Messages.Error.description` – mklement0 Mar 11 '19 at 21:53
  • @mklement0 wait how does the faster one loop? – Cataster Mar 11 '19 at 22:01
  • @Cataster: The loop is implicit in the dot-notation-based access to XML documents that PowerShell conveniently provides; in this case, if the `` element has multiple `` child elements, `....Messages.Error` returns them all - see [this answer](https://stackoverflow.com/a/49213568/45375) for background. – mklement0 Mar 11 '19 at 22:09