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.

How to copy file to mapped network drive (Isilon SMB) on mid server using domain user (COMPANY\username)

Igor Virgilio
Kilo Contributor

Hi guys,

I have a demand to send an attachment (picture) from a sc_task to a mapped network drive folder like "\\isilon\photos_folder" mapped on MID server. But, as expected the company controls the access to this folder by users that are on their domain.

I'm already sending the file correctly to a local folder on MID Server, and also to a \\isilon\photos_folder when it has the "allow everyone" rule applied to this folder. So, the MID server is using a local user (probably the Administrator) to write on the shared folder, how can I assure this?

Then, can I specify on Script Include and script step on my Flow action, to use users that belong to the Company's Domain? 

Down below there are the used scripts:

I tried to use the commented lines about credentials, username, and password, but this also didn't work.

Script Include:

var PhotoAttachment = Class.create();
PhotoAttachment.prototype = {
    initialize: function() {
        /* Set up the Packages references */
        this.debug("PhotoAttachment script started.");
        this.File = Packages.java.io.File;
        this.FileOutputStream = Packages.java.io.FileOutputStream;
        this.HttpClient = Packages.org.apache.commons.httpclient.HttpClient;
        this.UsernamePasswordCredentials = Packages.org.apache.commons.httpclient.UsernamePasswordCredentials;
        this.AuthScope = Packages.org.apache.commons.httpclient.auth.AuthScope;
        this.GetMethod = Packages.org.apache.commons.httpclient.methods.GetMethod;

        /* Set up the parameters */
        this.verbose = probe.getParameter("verbose");
        this.filepath = probe.getParameter("filepath");
        this.filename = probe.getParameter("filename");
        this.encodedData = probe.getParameter('encodedData');

        //this.username = probe.getParameter("username");  // ADD
        //this.password = probe.getParameter("password");

        this.debug("verbose -- filepath -- filename : " + this.verbose + ' -- ' + this.filepath + ' -- ' + this.filename);
    },

    /* Function to create new file and write data in target path*/
    saveToFile: function(targetPath) {
        this.debug("Initiating file save function");
        var f = new this.File(targetPath); // create new file
        var inputStream = this.encodedData;

        // var credentials = new this.UsernamePasswordCredentials(this.username,this.password);	

        var fout = new this.FileOutputStream(f);
        this.StringUtil = Packages.com.glide.util.StringUtil;
        var data = this.StringUtil.base64DecodeAsBytes(inputStream); // convert base64 to original format
        fout.write(data); // write data to newly created file
        fout.close();
        inputStream.close();
        result = "File successfully created : " + this.filepath + this.filename;
        this.debug(result);
    },

    /* Function to debug in mid-server log*/
    debug: function(m) {
        if (this.verbose == "true") {
            ms.log("Photo Attachment: " + m);
        }
    },

    /* Execute the Probe*/
    execute: function() {
        var saveRes = this.saveToFile(this.filepath + this.filename);
        return result;
    },
    type: "PhotoAttachment"
};

 

Script on my Flow Action:

(function execute(inputs, outputs) {
  var gr = GlideRecord('sys_attachment');
  gr.addQuery('sys_id', inputs.attachment);
  gr.query();
  if (gr.next()) {
    var name = '';
    name = inputs.file_name.replaceAll('.', '_') + '.jpg';
    name = name.replaceAll(' ', '');
    
    makeSortString = (function() {
    var translate_re = /[¹²³áàâãäåaaaÀÁÂÃÄÅAAAÆccç©CCÇÐÐèéê?ëeeeeeÈÊË?EEEEE€gGiìíîïìiiiÌÍÎÏ?ÌIIIlLnnñNNÑòóôõöoooøÒÓÔÕÖOOOØŒr®Ršs?ߊS?ùúûüuuuuÙÚÛÜUUUUýÿÝŸžzzŽZZ]/g;
    var translate = {
"¹":"1","²":"2","³":"3","á":"a","à":"a","â":"a","ã":"a","ä":"a","å":"a","a":"a","a":"a","a":"a","À":"a","Á":"a","Â":"a","Ã":"a","Ä":"a","Å":"a","A":"a","A":"a",
"A":"a","Æ":"a","c":"c","c":"c","ç":"c","©":"c","C":"c","C":"c","Ç":"c","Ð":"d","Ð":"d","è":"e","é":"e","ê":"e","?":"e","ë":"e","e":"e","e":"e","e":"e","e":"e",
"e":"e","È":"e","Ê":"e","Ë":"e","?":"e","E":"e","E":"e","E":"e","E":"e","E":"e","€":"e","g":"g","G":"g","i":"i","ì":"i","í":"i","î":"i","ï":"i","ì":"i","i":"i",
"i":"i","i":"i","Ì":"i","Í":"i","Î":"i","Ï":"i","?":"i","Ì":"i","I":"i","I":"i","I":"i","l":"l","L":"l","n":"n","n":"n","ñ":"n","N":"n","N":"n","Ñ":"n","ò":"o",
"ó":"o","ô":"o","õ":"o","ö":"o","o":"o","o":"o","o":"o","ø":"o","Ò":"o","Ó":"o","Ô":"o","Õ":"o","Ö":"o","O":"o","O":"o","O":"o","Ø":"o","Œ":"o","r":"r","®":"r",
"R":"r","š":"s","s":"s","?":"s","ß":"s","Š":"s","S":"s","?":"s","ù":"u","ú":"u","û":"u","ü":"u","u":"u","u":"u","u":"u","u":"u","Ù":"u","Ú":"u","Û":"u","Ü":"u",
"U":"u","U":"u","U":"u","U":"u","ý":"y","ÿ":"y","Ý":"y","Ÿ":"y","ž":"z","z":"z","z":"z","Ž":"z","Z":"z","Z":"z"
    };
    return function(s) {
        return(s.replace(translate_re, function(match){return translate[match];}) );
    }
})();
    name = makeSortString(name);
    var serverPath = '\\\\isilon\\photos_folder\\';   // this syntax is correct
    //var username = 'company\username';
    //var password = '********';
    
    var sa = new GlideSysAttachment();
    var binData = sa.getBytes(gr);
    var encData = GlideStringUtil.base64Encode(binData);
    var jspr = new JavascriptProbe('MidServer-DEV');
    jspr.setName('PhotoProbe'); // This can be any name 
    jspr.setJavascript("var ddr = new PhotoAttachment(); res= ddr.execute();");
    jspr.addParameter("verbose","true");
    //jspr.addParameter("username", username);
    //jspr.addParameter("password", password);
    jspr.addParameter("filepath", serverPath);
    jspr.addParameter("filename", name);
    jspr.addParameter("encodedData",encData);
    jspr.create();
  }
})(inputs, outputs);

 

Thank you guys, in advance.

 

Best regards

1 ACCEPTED SOLUTION

ServiceNow Tec2
Mega Sage
This has been resolved by ServiceNow Technical Support. Please refer to KB1024134 for more information.

View solution in original post

1 REPLY 1

ServiceNow Tec2
Mega Sage
This has been resolved by ServiceNow Technical Support. Please refer to KB1024134 for more information.