d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Write A Cmd Script And Write Out Output? > May Be A Little Removed From Programming
Add Reply New Topic New Poll
Member
Posts: 10,365
Joined: Aug 5 2007
Gold: 0.00
Jan 2 2017 04:09pm
So I have a list of usernames and I would like to automate a method of getting their full names to check for removal.

I have a command string that pulls their name in cmd with the following output.

Code

net user "username" /domain | find /i "Full Name"
Full Name Last, First *sometimes middle initial*

and I have a list (notepad) of usernames. Is there a way to write a script that will go line by line in a .txt file of usernames, run the above command and write out the full names to another .txt file?

e:\ added code brackets

This post was edited by ass666 on Jan 2 2017 04:15pm
Member
Posts: 8,112
Joined: Sep 23 2006
Gold: 3,558.23
Jan 2 2017 07:33pm
Is this being used in a domain environment? If so, I'd powershell it instead of cmd it.
Assuming your list of users reads like this:

Code

user1
user2
user3
etc.


In powershell, get the contents of the list, pass every through to get the fullname, and send it out to another text file:

Code

$userlist = get-content "c:\users.txt" # full path to your list of usernames
foreach ($user in $userlist)
{
$usersfullname = get-aduser $user -properties * | select displayname
add-content "c:\fullnames.txt" "`n$user $usersfullname"
}



This post was edited by Qord on Jan 2 2017 08:03pm
Member
Posts: 10,365
Joined: Aug 5 2007
Gold: 0.00
Jan 3 2017 02:29pm
Thanks a lot. Saved it as .ps1 and ran nicely.

Sent you a tip of gratitude.
Member
Posts: 8,112
Joined: Sep 23 2006
Gold: 3,558.23
Jan 3 2017 02:37pm
Hey thanks!! On a side note, you can run all old CMD commands inside powershell so if you wanted you could still use your net use command instead of the get-aduser part of the line.
Member
Posts: 10,365
Joined: Aug 5 2007
Gold: 0.00
Jan 3 2017 03:10pm
So I've already accomplished what I wanted with your help so the following doesn't bear too much weight.

But do you think we could try and get the CMD variant to work? If so I could apply the new knowledge with some other cmd commands I commonly use. Or I can start learning some basic powershell :P

I replaced the line that assigns $usersfullname a value with

Code
$usersfullname = net user $user /domain find /i "Full Name"


and I presume it's not getting the assignment because the output file only has a list of $user.

Could it have to do with the output type of the ner user /find command?

get-aduser outputs the following

Code
displayname
-----------
Last, First MI


Where net user outputs

Code
Full Name Last, First MI


e:\ minor edit

So just testing my above theory, I did the following and got the following output.

Code
> $testvar = net user MyUserName/domain | find /i "Full Name"
> $testvar
Full Name MyLast, MyFirst MyMI


but within the PS script outputs blanks interestingly enough.

e2:\ added some observations

Figured it out. Typo. I was missing an | as you might notice in the first code box above.

This post was edited by ass666 on Jan 3 2017 03:24pm
Member
Posts: 8,112
Joined: Sep 23 2006
Gold: 3,558.23
Jan 3 2017 03:25pm
I'm not very good with CMD and bat scripting, so I can't say for sure if it's possible doing it with just CMD. I can tell you if it is possible, it's a pain in the ass to get right.

For this one though, try putting "$user" instead of $user. Having it wrapped in quotes might do the trick.

E: ah, I love typos in code!! I do it a lot.

This post was edited by Qord on Jan 3 2017 03:26pm
Member
Posts: 10,365
Joined: Aug 5 2007
Gold: 0.00
Jan 3 2017 03:27pm
I figured it out, was just a typo on my part. Was missing a | between /domain and find in my implementation.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll