logo

NJP

IntegrationHub: Upload File to Windows Server

Import · Nov 03, 2020 · article

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

image

Cheers!!

Oscar Lopez

@oslovanet

View original source

https://www.servicenow.com/community/itom-articles/integrationhub-upload-file-to-windows-server/ta-p/2319287