PowerShell return values messing up XML in payload

StephenHey
Mega Guru

We are pulling some data through a powershell script using orchestration.   The data that's coming back contains some special characters, specifically an ampersand (&).   When an ampersand is present in the returned data, our payload has two extra sets of characters before and after our data:

 

<powershell>

<output>

 

However, when we exclude the data with the ampersand, these characters no longer show up.   (Which is odd.   I would think they would show up as <powershell><output>).   This is also affected our ability to use service-now's XML-to-Object system where we could reference 'document.result.output.<my xml tags>' for easy gathering of info.

 

In any case, we have also tried to sanitize the data coming in from PowerShell by doing a replace on the string before piping it in.   However, that doesn't seem to help either.

 

Any suggestions for getting properly formatted payloads with data with special characters??

2 REPLIES 2

StephenHey
Mega Guru

So, apparently this line of code exists in the PSSCript.ps1 on the MID Server in agent/lib.



if (!$isDiscovery) {


      Write-Host "<powershell>"


      Write-Host "<output>"


}



Since the XML is being written to the shell here, I've found that if you have any characters that need to be escaped in the data, the <powershell><output> get included too since it's being written directly to the shell instead of being appended before and after the shell data.



I noticed the Write-Host doesn't get written unless it's not discovery.   So, I took the easy way out and just moved my data gathering from a Powershell activity to a discovery probe/sensor and ran the probe from a workflow instead.   This still properly escapes the data, but doesn't include those extra tags that it was also escaping.


britwill
ServiceNow Employee
ServiceNow Employee

Hi Stephen-



Brit here.



Moving your data to a discovery probe/sensor and running the probe via a workflow is indeed a way to go.


However since you are using Orchestration you are undoubtedly sending your payload through a mid server.


If not configured accordingly the MID server has no knowledge you are passing it special characters such as ampersands "&".



Work around/ Fix :
One way forward is via the wrapper.conf file.
 
Background:


wrapper.java.additional.<n>
 
Additional Java parameters to pass to Java when it is launched. These are not parameters for your application, but rather parameters for the Java virtual machine. Each element has a property name which starts withwrapper.java.additional. and ends with an integer number counting up from 1. There can be no missing numbers.
 
For each entry, you can also have a corresponding strip quotes flag which will remove unescaped quotes from the element when run on Linux or UNIX machines. This was necessary to allow for file references which contain spaces. For parameters like the one below it would not otherwise be possible to decide when to remove quotes and when to leave them alone. '\\' will become '\', '\"' will become '"' and '"' will become ''. All other characters will remain unchanged.
 
Steps:
 
1 - Modify the file [wrapper.conf] on MID Server (configuration on client side):
2 - Edit the file C:\<MID Agent Dir>\conf\wrapper.conf
3 - Find a line starting with wrapper.java.additional.xx (xx could be 1, 2, or 3...depending on your configuration)
4 - Note this number xx
5 - Add a line wrapper.java.additional.xx+1=-Dfile.encoding=UTF-8
 
So if the last line was wrapper.java.additional.1, the new line to add should be: wrapper.java.additional.2=-Dfile.encoding=UTF-8



Regards,


Brit