Need Help: Copy ServiceNow Attachments to Local Windows Folder using MID Server

Rohitp484
Tera Contributor

 

Hi Community,

I'm working on a ServiceNow integration use case and need some guidance.

Requirement:

Whenever a user uploads a file in the ServiceNow Attachment section, the same file should automatically be copied to a local Windows folder.

Example scenario:

  • Windows Machine has a folder:

 

 
C:\ServiceNowLearning\
 

 

 
  • User uploads a file in ServiceNow attachment section, for example:

 

 
employees.csv
 

 

 
  • After upload, the same file should automatically appear in another local Windows folder such as:

 

 
C:\SN_Attachments\
 
 

Environment details:

  • ServiceNow instance(PDI Zurich)
  • Windows OS
  • MID Server configured and running (Status = Up)
  • Looking for a solution using MID Server / Business Rule / Flow Designer / PowerShell (any recommended approach).

Questions:

  1. What is the recommended approach for this scenario?
  2. Should this be implemented using Business Rule + ECC Queue + MID Server SystemCommand?
  3. Is Flow Designer + MID Server PowerShell capability a better/cleaner solution?
  4. How can the attachment binary/content be safely transferred from ServiceNow to the local Windows filesystem?

Note: When I'm running through the powersheel command manually, running then its showing in the local folder i created a poll_configure.ps1 file in the destination folder and command which i'm using is

powershell -ExecutionPolicy Bypass -File C:\ServiceNowAttachments\poll_attachments.ps1 

If anyone has implemented a similar use case or has sample steps/scripts, it would be very helpful.

Thanks!

1 ACCEPTED SOLUTION

Tanushree Maiti
Giga Patron

Hi @Rohitp484 

 

To automatically copy an attachment uploaded in ServiceNow to a local Windows folder on a MID Server, you need to use a Business Rule that triggers an ECC Queue probe. This probe executes a PowerShell script on the MID Server to save the file.

 

Step 1: Create a MID Server Script Include

  • Navigate to MID Server > Script Files in ServiceNow.
  • Click New and set the Name (e.g., SaveAttachmentToFolder.ps1) and Parent to PowerShell.
  • Add a PowerShell script that receives the file stream, converts the base64 content to bytes, and writes it to your local Windows directory. 

 

Step 2: Create a Business Rule

  • Navigate to System Definition > Business Rules and click New.
  • Table: sys_attachment
  • When: After
  • Insert: Check the box
  • Condition: table_name is your target table (e.g., incident, sc_req_item). 

Sample script:

(function executeRule(current, previous /*null when async*/) {

     if (current.table_name == 'your_custom_table' || current.table_name == 'incident') {

         var targetFolderPath = 'C:\\ServiceNowLearning\\';

         var agent = 'mid.server.YOUR_MID_SERVER_NAME';

        var topic = 'Powershell';

        var name = 'SaveAttachmentToFolder.ps1';

         var payload = '<parameters>' +

                      '<parameter name="attachment_sys_id" value="' + current.sys_id + '"/>' +

                      '<parameter name="target_path" value="' + targetFolderPath + '"/>' +

                      '</parameters>';                     

        var ecc = new GlideRecord('ecc_queue');

        ecc.initialize();

        ecc.agent = agent;

        ecc.topic = topic;

        ecc.name = name;

        ecc.queue = 'output';

        ecc.state = 'ready';

        ecc.payload = payload;

        ecc.insert();

    }

})(current, previous);

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron

@Rohitp484 

But I didn't get the business use-case for this requirement.

what purpose it serves?

User is uploading file from his/her local machine to ServiceNow, again why to copy that file to some other folder of that user?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Tanushree Maiti
Giga Patron

Hi @Rohitp484 

 

To automatically copy an attachment uploaded in ServiceNow to a local Windows folder on a MID Server, you need to use a Business Rule that triggers an ECC Queue probe. This probe executes a PowerShell script on the MID Server to save the file.

 

Step 1: Create a MID Server Script Include

  • Navigate to MID Server > Script Files in ServiceNow.
  • Click New and set the Name (e.g., SaveAttachmentToFolder.ps1) and Parent to PowerShell.
  • Add a PowerShell script that receives the file stream, converts the base64 content to bytes, and writes it to your local Windows directory. 

 

Step 2: Create a Business Rule

  • Navigate to System Definition > Business Rules and click New.
  • Table: sys_attachment
  • When: After
  • Insert: Check the box
  • Condition: table_name is your target table (e.g., incident, sc_req_item). 

Sample script:

(function executeRule(current, previous /*null when async*/) {

     if (current.table_name == 'your_custom_table' || current.table_name == 'incident') {

         var targetFolderPath = 'C:\\ServiceNowLearning\\';

         var agent = 'mid.server.YOUR_MID_SERVER_NAME';

        var topic = 'Powershell';

        var name = 'SaveAttachmentToFolder.ps1';

         var payload = '<parameters>' +

                      '<parameter name="attachment_sys_id" value="' + current.sys_id + '"/>' +

                      '<parameter name="target_path" value="' + targetFolderPath + '"/>' +

                      '</parameters>';                     

        var ecc = new GlideRecord('ecc_queue');

        ecc.initialize();

        ecc.agent = agent;

        ecc.topic = topic;

        ecc.name = name;

        ecc.queue = 'output';

        ecc.state = 'ready';

        ecc.payload = payload;

        ecc.insert();

    }

})(current, previous);

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

@Tanushree Maiti 

Could you please help where the mid server needs to be installed?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader