Generate alert XML

This topic contains 3 replies, has 2 voices, and was last updated by  Joakim 3 years, 6 months ago.

  • Author
    Posts
  • #15121

    André Borgeld
    Participant

    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"?>



    [REFERENCE]
    [TARIFF]

    [SENDER]
    8
    [NUMBER]
    [MESSAGE]
    [NUMBER]


    Kind regards,

    André

  • #15127

    André Borgeld
    Participant

    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.

  • #15122

    Joakim
    Participant

    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!

  • #15146

    Joakim
    Participant

    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"?>
    
      
      
      $Reference
      $Tariff
      
        $Sender
        8
        $To
        $Message
        $ToNumber
      
    
    "@
    
    # 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)

You must be logged in to reply to this topic.