When I was building my vRops Custom Rest outbound plugin I wanted to capture the output from vRops to an XML file… this way I could review the output easily!

I came up with the following Powershell solution (based off an example from someone else which I can no longer credit cause I can’t find it) it works for both HTTP and HTTPS, for HTTPS to work you will need to run the following netsh command once in Windows as an Administrator

You can customize the appid and the certhash if you wish… I just added the example below to make it easy.

netsh http add sslcert ipport=0.0.0.0:6666 appid=`{00112233-4455-6677-8899-AABBCCDDEEFF`} certhash=‎952e09627ea02b1b3abbe73a41cdb7308e0640e9

Below is the script, If you want to change the listening ports just change 6666 / 6667, make sure you create a folder called requests in the same location as the script as this is where the XML’s will be output to.

How to use it??

POST to /customrest with ContentType ‘text/xml’

POST to /rest with ContentType ‘application/xml’

One the script is listening if you want to stop it… just do a GET to /stop

# PowerShell RESTful server
#v1.0 vMan.ch, 31.05.2017 - Initial Version

$ScriptPath = (Get-Item -Path ".\" -Verbose).FullName

# Start of script
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add('http://+:6667/') # Must exactly match the netsh command above
$listener.Prefixes.Add('https://+:6666/')

$RunCollection = 1 
 
$listener.Start()
'Listening ...'
while ($true) {
    $context = $listener.GetContext() # blocks until request is received
    $request = $context.Request
    $response = $context.Response


    if ($request.Url -match '/customrest

vMan
) {
        $response.ContentType = 'text/xml'
        $hour = [System.DateTime]::Now.Hour
        $minute = [System.DateTime]::Now.Minute
        $message = "<?xml version=""1.0""?><Time><Hour>$hour</Hour><Minute>$minute</Minute></Time>"
        $request.HttpMethod
        $StreamReader = New-Object System.IO.StreamReader $request.InputStream
        $StreamData = $StreamReader.ReadToEnd()
        $RunDateTime = (Get-date)
        $RunDateTime = $RunDateTime.tostring("yyyyMMddHHmmss")
        $StreamData | out-file -FilePath "$ScriptPath\requests\$RunDateTime-$RunCollection.xml"
        $RunCollection += 1 
    }

    if ($request.Url -match '/rest

vMan
) {
        $response.ContentType = 'application/xml'
        $hour = [System.DateTime]::Now.Hour
        $minute = [System.DateTime]::Now.Minute
        $message = "<?xml version=""1.0""?><Time><Hour>$hour</Hour><Minute>$minute</Minute></Time>"
        $request.HttpMethod
        $StreamReader = New-Object System.IO.StreamReader $request.InputStream
        $StreamData = $StreamReader.ReadToEnd()
        $RunDateTime = (Get-date)
        $RunDateTime = $RunDateTime.tostring("yyyyMMddHHmmss") 
        $StreamData | out-file -FilePath "$ScriptPath\requests\$RunDateTime-$RunCollection.xml"
        $RunCollection += 1 
    }	
	
    if ($request.Url -match '/stop

vMan
) {break}
 
    [byte[]] $buffer = [System.Text.Encoding]::UTF8.GetBytes($message)
    $response.ContentLength64 = $buffer.length
    $output = $response.OutputStream
    $output.Write($buffer, 0, $buffer.length)
    $output.Close()

}
 
$listener.Stop()

vMan