- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 02:53 PM
I am running a script (background script currently, but it will be in an ETL later). My goal is to accept a username in any of the formats:
- username@domain.com
- domain\username
- username
I will convert the second two, to "username@domain.com" and do a query against sys_user, for that value. My problem is that I sometimes the username is something like "domain\rsmith". When I run the script, after adjusting the username value, I am left with "smith@domain.com" because SN is interpreting "\r" as a carriage return character. I have worked around the issue for \r but I could have the same problem with other regular expression characters (e.g. \n) and I don't really want to try to code for every possibility.
Is there an OOB way to make SN ignore "\r" and the like?
Here is the script I have been testing:
var input = 'domain\nsmith';
if (input.indexOf('\\') == -1) {
gs.info('input contains a backslash');
if (input.contains('\r')) {
input = input.replace('domain\r', 'r');
} else {
input = input.replace('domain', '');
}
gs.info('Replaced domain: {0}', input);
var backslashIndex = input.indexOf('\\');
if (backslashIndex !== -1) {
input = input.substring(backslashIndex + 1);
gs.info('removed domain: {0}', input);
} else {
gs.info('domain not in username');
}
if (!input.includes('@domain.com')) {
input += '@domain.com';
gs.info('added mail domain. input is now: {0}', input);
}
var user = new GlideRecord('sys_user');
user.addQuery('user_name', input);
user.addQuery('active', 'true');
user.query();
var count = 0;
var firstSysId;
while (user.next()) {
if (count === 0) {
firstSysId = user.sys_id;
}
count++;
}
gs.info('returning user: {0}', firstSysId);
} else {
gs.info('no slash');
}
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2025 11:58 AM
I ended-up using the following script.
(function (batch, output) {
for (var i = 0; i < batch.length; i++) {
var input = batch[i].input;
//var log_source = 'SccmUserLookup';
var downLevelDomain = 'domain';
var fullDomain = 'domain.com';
var domainRegex = new RegExp('^(' + downLevelDomain + '|' + fullDomain + ')(\\\\)?', 'i');
/*Logger = {
message: '',
appendLine: function (s) {
this.message += new Date().toISOString() + ': ' + s + '\n';
},
flush: function (error) {
var f = error ? gs.logError : gs.log;
f(this.message, log_source);
}
};*/
try {
//Logger.appendLine('Beginning SCCM user lookup script.');
if (input.includes(',')) {
var inputArray = input.split(',');
input = inputArray[0];
}
input = '.\administrator'
switch (true) {
case input.includes('\n'):
input = input.replace(/^.*(?=\n)/, '').replace('\n', 'n');
break;
case input.includes('\r'):
input = input.replace(/^.*(?=\r)/, '').replace('\r', 'r');
break;
case input.includes('\t'):
input = input.replace(/^.*(?=\t)/, '').replace('\t', 't');
break;
case input.includes('\T'):
input = input.replace(/\\T/g, 'T');
break;
case input.includes('\f'):
input = input.replace(/^.*(?=\f)/, '').replace('\f', 'f');
break;
case input.includes('\v'):
input = input.replace(/^.*(?=\v)/, '').replace('\v', 'v');
break;
case input.includes('\x08'): // represents \b
input = input.replace(/\x08/g, 'b');
break;
case input.includes('\B'):
input = input.replace(/\\B/g, 'B');
break;
case input.includes('\d'):
input = input.replace(/\\d/g, 'd');
break;
case input.includes('\D'):
input = input.replace(/\\D/g, 'D');
break;
case input.includes('\w'):
input = input.replace(/\\w/g, 'w');
break;
case input.includes('\W'):
input = input.replace(/\\W/g, 'W');
break;
case input.includes('\S'): // Skipped \s because no special handling is needed
input = input.replace(/\\S/g, 'S');
break;
default:
break;
}
input = input.replace(domainRegex, '');
//Logger.appendLine('Current username value: ' + input);
if (!input.includes('@domain.com')) {
input += '@domain.com';
//Logger.appendLine('Appended domain to username. Modified username value: ' + input);
}
var user = new GlideRecord('sys_user');
user.addQuery('user_name', input);
user.addQuery('active', 'true');
user.query();
var count = 0;
var firstSysId = '';
while (user.next()) {
if (count === 0) {
firstSysId = user.sys_id;
}
count++;
}
//Logger.appendLine('Found ' + count + ' user(s), returning user sys_id: ' + firstSysId);
//Logger.flush();
// Set each output element below
output[i] = firstSysId;
} catch (e) {
//Logger.appendLine('Error: ' + e.message);
//Logger.flush(true);
}
}
})(batch, output);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2025 11:18 AM
In my case, I am receiving the string from another source (SGC for SCCM). Using GlideStringUtil.escapeNonPrintable() breaks the rest of the script. Is there another encoding method you can suggest?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2025 12:08 PM
I'm thinking, Let's see if the return from SGC for SCCM really reflects the string you want to parse, or if it's loaded with a [CR] already.
FWIW: your test code should be made to work with a string literal that already has that double-backslash. I can't think of much that'd resolve your issue if SGC really does return a username with a [CR] or [LF] in it, except to correct the data where it came from.
I guess the important thing to diagnose would be to get a very clear idea what your call is returning to you. It's unfortunate that .escapeNonPrintable() can't log the result. It may be just that it's a different object and some value in it needs to be set into a String object with .toString(). That's a little beyond what we need, and it carries a bunch of worries with it. So far we don't need it absolutely -- but I'm worried in a couple of steps we might.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2025 12:24 PM
I shoulda just said, "what's gs.info() return when you print out the username?" Silly me. That's the next step.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2025 12:35 PM
The user's real name is "Robert Smith" (for example) and his username is "rsmith". Because SCCM is sending it as a down-level logon name, I am getting a string with the domain, followed by a backslash, then his username.
I am not getting an actual [CR] character, SN is just treating it like that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2025 12:48 PM
yes. I want to know, when it's returned from SCCM, what does that string look like.
When you put in the string literal with two backslashes, Javascript will load only one. The "other" backslash is an escape operator.
By "string literal", this is simply the text string you type in for Javascript to interpret as a string. Javascript doesn't copy into the Javascript string exactly what you type in. It interprets these strings to find escape operators (\) and interpret what follows, differently. Which is why the "double-backslash" to add a single backslash.