Generate alert XML

Hi Guys,

Im new on generating XML. So i hope somebody can help me.
Here i got different XML elements and attributes (see below). How do i generate something like this in powershell and save it to a file :

‘$xmlDoc.LoadXml(etc…)’
‘$xmlDoc.Save(“c:\Temp\exampe1.xml”)’

This is what i want as XML output:

<?xml version=“1.0”?>
<MESSAGES>
<CUSTOMER ID=[CUSTOMER ID] />
<USER LOGIN=[LOGIN] PASSWORD=[PASSWORD] />
<REFERENCE>[REFERENCE]</REFERENCE>
<TARIFF>[TARIFF]</TARIFF>
<MSG>
<FROM>[SENDER]</FROM>
<DCS>8</DCS>
<TO>[NUMBER]</TO>
<BODY TYPE=“[TYPE]” HEADER=“[HEADER]”>[MESSAGE]</BODY>
<TO OPERATOR=“[OPERATOR]”>[NUMBER]</TO>
</MSG>
</MESSAGES>

Kind regards,

André

Quick question, in what format is your source data? Is it also im XML? Or will you get the data from other sources? I.e. is that you are asking how to generate the XML document above with values filled in?

There are a number of different ways you can generate the document you want and which way is easiest/best depends on where your data is coming from. With PowerShell you can create real objects and the use general cmdlets such as ConverTo-Xml to save the XML. Here’s a very basic example:

$properties = @{'Customer ID'=1;
                'User Login'='login';
                }
$object = New-Object –TypeName PSObject –Prop $properties
Write-Output $object
$tmp = $object | ConvertTo-Xml

let us know a Little bit more about your requirements!

My output is XML and my input is a operations manager message what i must add in the MSG element.
So there is an alert in opsmgr and that starts a powershell script that generates and XML (like above) and sent it’s to a website.
The last part i know but i don’t know how to create an XML (like above) in Powershell.

Ok, so here is one way of doing it. This example first defines the variables you want to put in your XML document. Then it create the XML document (using Here-String) which takes the XML template and the values from the variables. Once that is done a random filenamne is generated and then saved.


# Setup your data in variables / or objects
$CustomerId ="customerid"
$Login = "login"
$Password = "mypassword"
$Reference = "reference"
$Tariff = "tariff"
$Sender = "Sender"
$To = "number"
$BodyType = "type"
$BodyHeader = "header"
$Message = "message"
$ToOperator="operator"
$ToNumber="number"

# Create a XML document by using a Here-String, place the variables within doubl-quotes where you want them.

[xml]$messages = @"
<?xml version="1.0"?>
<MESSAGES>
  <CUSTOMER ID="$CustomerId" />
  <USER LOGIN="$Login" PASSWORD="$Password" />
  <REFERENCE>$Reference</REFERENCE>
  <TARIFF>$Tariff</TARIFF>
  <MSG>
    <FROM>$Sender</FROM>
    <DCS>8</DCS>
    <TO>$To</TO>
    <BODY TYPE="$BodyType" HEADER="$BodyHeader">$Message</BODY>
    <TO OPERATOR="$ToOperator">$ToNumber</TO>
  </MSG>
</MESSAGES>
"@

# Save the file where you want it (here I generate a random filename)
$outfile = Join-Path -Path $env:TEMP  -ChildPath ([System.IO.Path]::GetRandomFileName())

$messages.Save($outfile)