Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Oscar Lopez
Mega Guru

Allow the end users to upload files to a Windows Server throughout Service Request using Integration Hub and PowerShell.

 

Script Step - Base64 Encoding (Global Scope)

/* Get file attachement */
var att = new GlideRecord("sys_attachment");
att.addQuery("sys_id", inputs.attachment_sys_id);
att.query();

if(att.next()) {
  /* create input stream */
  var attIS = new GlideSysAttachmentInputStream(att.sys_id);
  /* create Byte Array holder */
  var byteArrayOS = new Packages.java.io.ByteArrayOutputStream();
  /* write Byte Array */
  attIS.writeTo(byteArrayOS);
  /* get Base64 Encoding */
  outputs.file_base64_encoded = GlideBase64.encode(byteArrayOS.toByteArray());
  /* concatenate Folder Path with File Name */
  outputs.file_path = inputs.folder_path + "\\" + att.file_name;
}

 

PowerShell Script

function Convert-StringToBinary {
	[CmdletBinding()]
	param (
		[string] $EncodedString,
		[string] $FilePath = ('{0}\{1}' -f $env:TEMP, [System.Guid]::NewGuid().ToString())
	)

	try {
		if ($EncodedString.Length -ge 1) {
		     # decodes the base64 string
		    $ByteArray = [System.Convert]::FromBase64String($EncodedString);
		    [System.IO.File]::WriteAllBytes($FilePath, $ByteArray);
		}
	}
	catch {
	}
}

Convert-StringToBinary -EncodedString $Base64String -FilePath $TargetFileName

 

find_real_file.png

Cheers!!

Oscar Lopez

@oslovanet

Comments
Gerrity
Tera Expert

Hi Oscar,

Thanks for taking the time to create this.

Question, everything executes but no files are being uploaded to the sys_attachment table.  The Script Step seems to get the attachment_sys_id and the target_folder_path but the step output data of Base64 Encoded and File Name are empty.

Any possible ideas?

Cheers

Version history
Last update:
‎11-02-2020 06:37 PM
Updated by: