windows admins, bat...
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] windows admins, batch files "for" command

10 Posts
8 Users
0 Reactions
86 Views
 MSP
Posts: 15473
Free Member
Topic starter
 

I have a need to create lots of local accounts on some systems (forced upon us by a software vendor). To semi automate the process I want to have a file with the following information.

username-1 password-1 email-1 fullname-1 group-a
username-2 password-2 email-2 fullname-2 group-b

and use it to populate the commands in a batch file.

NET USER username-1 password-1 /ADD /COMMENT:"email-1" /FULLNAME:"fullname-1" /logonpasswordchg:yes
NET LOCALGROUP group-1 username-1 /ADD
NET LOCALGROUP "Remote Desktop Users" username-1 /ADD

Now, I can populate 1 piece of information using the for command, but I cant get my head around multiple different fields.

Any ideas?


 
Posted : 24/10/2018 2:30 pm
Posts: 10315
Full Member
 

you could use one batch file with the NET user type commands - call it STW.bat

then change your password file to start with STW in each line ie

stw username-1 password-1 email-1 fullname-1 group-a
stw username-2 password-2 email-2 fullname-2 group-b

then you use %1 in the stw.bat file to replace username

%2 for password

%3 for email

%4 for fullname

etc, so stw.bat looks like

NET USER %1 %2 /ADD /COMMENT:%3 /FULLNAME:%4 /logonpasswordchg:yes
NET LOCALGROUP %5 %1 /ADD
NET LOCALGROUP “Remote Desktop Users” %1 /ADD

It's probably easier using powershell but I think that that would work.  I haven't written batch files in years so that above is likely to be largely pants


 
Posted : 24/10/2018 2:41 pm
Posts: 822
Free Member
Posts: 77347
Free Member
 

Go back to the vendor and tell them to provide something that's fit for purpose?  That's the sort of insanity I'd have expected to see 20 years ago.

You can do something like FOR /F "tokens=* delims=, %i in (foo.csv) DO {whatever with %i, %j etc} on a CSV, is that the sort of thing you mean?  for /? at the commandline is your friend here.

TBH, I'd be tempted to break out PowerShell for this.


 
Posted : 24/10/2018 2:52 pm
Posts: 4954
Free Member
 

Another hacky approach I have used when I can get a bat file (or any other scripting ISH lang I know a limited amount of) to do something once but not follow the  some greater more complicated variation is to write a python script to write a bat file or edit a bat file and repeatedly call said bat file. Yeeehaaawww cow boy!!!


 
Posted : 24/10/2018 3:19 pm
 MSP
Posts: 15473
Free Member
Topic starter
 

Go back to the vendor and tell them to provide something that’s fit for purpose? That’s the sort of insanity I’d have expected to see 20 years ago.

My time in comparison to the software's functionality is considered nothing but a triviality. It is a 20 year old bespoke software package that has been updated for it's operational functionality and to run on newer os's, but not modernised for the modern IT landscape. All I can do is keep raising it with the account manager.


 
Posted : 24/10/2018 3:29 pm
Posts: 77347
Free Member
 

Ugh.  You have my sympathies then.


 
Posted : 24/10/2018 3:43 pm
Posts: 887
Full Member
 

MS have a powershell script for this that may do the trick now that you can't use Group Policy for this sort of thing anymore:

Scroll down to the workarounds part of this article for the 'Invoke-PasswordRoll' script.

https://support.microsoft.com/en-us/help/2962486/ms14-025-vulnerability-in-group-policy-preferences-could-allow-elevati


 
Posted : 24/10/2018 4:00 pm
Posts: 3991
Full Member
 

Ultra hacky quick and dirty way. Write your batch file command once and ensure it works. Transpose it into Excel. Split it up into various columns, then have other columns for the variables (usernames, passwords etc) interspersed. Then have a final column that concatenates it all. Copy this down populating the usernames/passwords as far as you need to. Then copy and paste the rows from your final column into a batch file. So something like the below

Command A |Username 1 | Command B | Password1 | Command C | Email 1 | Concatenated string
Command A |Username 2 | Command B | Password2 | Command C | Email 2 | Concatenated string
Command A |Username 3 | Command B | Password3 | Command C | Email 3 | Concatenated string

Or yeah just use powershell.


 
Posted : 24/10/2018 6:24 pm
Posts: 822
Free Member
 

You've probably already written it, but if not, Powershell script:

Import-CSV ".\users.txt" -delimiter " " -Header uname,pwd,email,fullname,group | ForEach-Object {
Write-Host $_.uname
$pwd = ConvertTo-SecureString $_.pwd -AsPlainText -Force
# Remove-LocalUser -Name $_.uname
$user = New-LocalUser -Name $_.uname -Password $pwd -Description $_.email -FullName $_.fullname
$group = Get-LocalGroup -Name $_.group
Add-LocalGroupMember -Group $group -Member $user
}

Before you can run this you may need to

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

to let you run scripts.


 
Posted : 24/10/2018 7:09 pm
Posts: 727
Free Member
 

Yep, powershell is the key. Is it multiple pcs? You could edit the script to remote into the pcs as long as you have set them to allow it.


 
Posted : 24/10/2018 8:52 pm

6 DAYS LEFT
We are currently at 95% of our target!