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 '0f585685' does not exist in the database with the ID of '', 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 '', Name of 'Teradata '." Source="Microsoft SQL Server 2017 Analysis Services" HelpFile="" />
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 '.
Assuming you save the error as $msg, you need to convert it to an XML type:
[xml]$xml = $msg
then you can use . (dot) notation to get the details when it’s converted to XML:
$xml.return.root.messages.error.Description
Select-String really should not be used for XML parsing. You can, just as you could using RegEx matching.
$XmlData = @'
< 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 '0f585685' does not exist in the database with the ID of '', or the u
ser does not have permissions to access the object." Source="Microsoft SQL Server 2017 Analysis Services" HelpFile="" />
Messages>root>return>
'@
[RegEx]::Matches($XmlData,'(?<=Description=)(?s)(.*)(\.")').Value
# Results
"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."
However, this why the xml cmdlets exists as well as additional modules on the MS PowerShellGallery.
Well, as long as the XML is properly formatted.
Get-Command -Name '*xml*' | Format-Table -AutoSize
CommandType Name Version Source
----------- ---- ------- ------
Function ConvertFrom-PSFClixml 0.10.28.144 PSFramework
Function ConvertFrom-PSFClixml 0.10.27.135 PSFramework
Function ConvertTo-PSFClixml 0.10.28.144 PSFramework
Function ConvertTo-PSFClixml 0.10.27.135 PSFramework
Function Export-PSFClixml 0.10.28.144 PSFramework
Function Export-PSFClixml 0.10.27.135 PSFramework
Function Export-PSFClixml 0.9.25.113 PSFramework
Function Get-ClusterFaultDomainXML 2.0.0.0 FailoverClusters
Function Import-PSFClixml 0.10.28.144 PSFramework
Function Import-PSFClixml 0.10.27.135 PSFramework
Function Import-PSFClixml 0.9.25.113 PSFramework
Function Set-ClusterFaultDomainXML 2.0.0.0 FailoverClusters
Cmdlet ConvertTo-Xml 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Convert-XMLtoJSON 5.0.0.1 Sorlov.PowerShell
Cmdlet Export-Clixml 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Import-Clixml 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Merge-XMLFile 5.0.0.1 Sorlov.PowerShell
Cmdlet New-XSDfromXML 5.0.0.1 Sorlov.PowerShell
Cmdlet Select-Xml 3.1.0.0 Microsoft.PowerShell.Utility
Just read in the XML and select the value you are after.
Select-Xml
Module:Microsoft.PowerShell.Utility
Finds text in an XML string or document
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-xml?view=powershell-6
Search XML files with PowerShell using Select-XML
As far as the email part. This is what Send-MailMessage is for. Capture what you need in a variable or variables, and pass that to it.
# get function / cmdlet details
(Get-Command -Name Send-MailMessage).Parameters
Get-help -Name Send-MailMessage -Full
Get-help -Name Send-MailMessage -Online
Get-help -Name Send-MailMessage -Examples
Excellent, thank you both!