ServiceNow - 在 Midserver 中更改文件名

ServiceNow - Change a file name in Midserver

我目前正在尝试在计划导出后替换中间服务器中的文件名。

这里的想法是,文件的名称格式为“file_name_datetime”,客户需要“datetime_file_name”才能让另一个系统正确读取文件。

我的主要想法是在导出为正确格式后重命名文件,但如果有办法将文件名更改为所需的文件名,我也可以这样做。

我很想听听你们的意见,因为我不知道该怎么做。

提前致谢。

如果有人对答案感兴趣,请看下面:

脚本包括:

initialize: function() {
    this.filePath = gs.getProperty('directory_path');
    this.midServer = gs.getProperty('midserver');
    this.authMidServerBase64 = gs.getProperty('authmidserver');
},

nameChange: function(exportSetName) {

    var exportGr = new GlideRecord("sys_export_set_run");
    exportGr.addEncodedQuery("set.nameSTARTSWITH" + exportSetName);
    exportGr.orderByDesc("completed");
    exportGr.query();

    if (exportGr.next()) {

        var attachSysID = exportGr.ecc_agent_attachment.sys_id;

    }

    var attachGr = new GlideRecord("sys_attachment");
    attachGr.addEncodedQuery("table_sys_idSTARTSWITH" + attachSysID);
    attachGr.query();
    if (attachGr.next()) {

        var attachName = attachGr.file_name;
        var attachDate = attachName.match((/\d+/));
        var newName = attachDate + '_' + exportSetName + '.csv';
    }

    var jspr = new JavascriptProbe(this.midServer);
    jspr.setName('FileNameChange'); // This can be any name 
    jspr.setJavascript('var ddr = new MidServer_script_include(); res = ddr.execute();');
    jspr.addParameter("verbose", "true");
    jspr.addParameter("skip_sensor", "true"); // prevent Discovery sensors running for the ECC input
    jspr.addParameter("filename", this.filePath + "\" + attachName);
    jspr.addParameter("filePath", this.filePath);
    jspr.addParameter("newName", this.filePath + "\" + newName);
    jspr.addParameter("operation", "rename");
    return jspr.create();

},

中间服务器脚本包括:

initialize: function() {
    /**
     *** Set up the Packages references
     **/
    this.File = Packages.java.io.File;
    this.FileOutputStream = Packages.java.io.FileOutputStream;
    this.FileInputStream = Packages.java.io.FileInputStream;
    this.Path = Packages.java.nio.file.Path;
    this.Paths = Packages.java.nio.file.Paths;
    this.Files = Packages.java.nio.file.Files;
    this.StandardCopyOption = Packages.java.nio.file.StandardCopyOption;

    /**
    /* Set up the parameters
    **/
    this.verbose = probe.getParameter("verbose");
    this.filePath = probe.getParameter("filePath");
    this.filename = probe.getParameter("filename");
    this.operation = probe.getParameter("operation");
    this.newName = probe.getParameter("newName");
    result = "initialize complete";
},

execute: function() {
    if (this.operation == 'rename') {
        this.fileRename(this.filename, this.newName);
    }
    return result;
},

fileRename: function(fileName, newName) {
    result+= "\r\n Renaming file.";
    this._debug(result);
    try {
        var res = this._moveFile(fileName, newName);
    } catch (e) {
        result += "\r\n Erro no renomeamento do ficheiro: " + e;
        this._debug(result);
    }
},

_moveFile: function(initialPath, targetPath) {
    try {
        this._debug("Initiating file move function");
        var inPath = this.Paths.get(initialPath);
        var tgPath = this.Paths.get(targetPath);
        var res = this.Files.move(inPath, tgPath, this.StandardCopyOption.REPLACE_EXISTING);

        result += "File successfully moved from: " + initialPath + " to: " + targetPath + " \r\n Result: " + res;
        this._debug(result);
    } catch (e) {
        this._debug('Error:' + e);
    }
},

_debug: function(m) {
    if (this.verbose == "true") {
        ms.log("::: Mid Server script include logger ::: " + m);
    }
},

https://community.servicenow.com/community?id=community_question&sys_id=a56b38a6db326490fa192183ca961987