Jump to content


This is a ready-only archive of the InstallSite Forum. You cannot post any new content here. / Dies ist ein Archiv des InstallSite Forums. Hier können keine neuen Beiträge veröffentlicht werden.
Photo

How to add a user to the Local Admin Group


3 replies to this topic

ghines

ghines
  • Full Members
  • 4 posts

Posted 19 September 2006 - 22:14

Hi all,

I'm using Installshield 12 and have a Basic MSI project that I want to add a CA that will add a user to the local administrators group. I can add the user OK but I don't know how to add it to the administrators group.

Also is it possible to add a description to the user?

Any help would be appreciated.

The code I have so far is this:-

CODE

set oComputer = CoGetObject("WinNT://" + szComputerName + ",computer", "");

if (IsObject(oComputer)) then
 try

  // Create IADsUser object
  set oUser = oComputer.Create("user", szUserName);
  oUser.SetInfo();
           
  // Set password
  oUser.SetPassword(szPassword);
  oUser.SetInfo();
 
  // Set Description
           
                      // Set Group

  // Activate the user
  varBuffer = oUser.Get("UserFlags");
  varBuffer = varBuffer | ADS_UF_DONT_EXPIRE_PASSWD |
  ADS_UF_PASSWD_CANT_CHANGE;
  oUser.Put("UserFlags", varBuffer);
  oUser.SetInfo();

  catch

          MessageBox(Err.Description, INFORMATION);

  endcatch;
 
endif;  
end;


krakos

krakos
  • Full Members
  • 8 posts

Posted 01 September 2009 - 10:14

A little late, but for all others:

I used LaunchAppAndWait with:

CODE

//add user
net user username1 password1 /add /expires:never                                  

// add user to admingroup (german spelling!!!)
net localgroup Administratoren username1 /add


Look for the net.exe documentation, its very powerfull and easy to use.


Maybe this helps others.

Juanchex

Juanchex
  • Full Members
  • 2 posts

Posted 02 September 2009 - 09:38

QUOTE (krakos @ 2009-09-01 10:14)
A little late, but for all others:

I used LaunchAppAndWait with:

CODE

//add user
net user username1 password1 /add /expires:never                                  

// add user to admingroup (german spelling!!!)
net localgroup Administratoren username1 /add


Look for the net.exe documentation, its very powerfull and easy to use.


Maybe this helps others.

For this, I use a VB Script. Here it goes (in parts):

This function creates a user with fullname, description and password

Function CreateUser(s_User,s_FullName,s_Description,s_Password)
Const ADS_UF_DONT_EXPIRE_PASSWD = &H10000
Const ADS_UF_PASSWD_CANT_CHANGE = &H0040

Dim o_WinNT
Dim o_User
Dim o_Flag

Set o_WinNT = GetObject("WinNT://.")

' Benutzer anlegen
Set o_User = o_WinNT.Create("User", s_User)
o_User.SetInfo
o_User.FullName = s_FullName
o_User.Description = s_Description
o_User.SetPassword s_Password
o_User.SetInfo

' Benutzer kann Passwort nicht ändern
If Not o_User.UserFlags AND ADS_UF_PASSWD_CANT_CHANGE Then
o_Flag = o_User.UserFlags XOR ADS_UF_PASSWD_CANT_CHANGE
o_User.Put "UserFlags", o_Flag
o_User.SetInfo
End If

' Passwort läuft nie ab
If Not o_User.UserFlags AND ADS_UF_DONT_EXPIRE_PASSWD Then
o_Flag = o_User.UserFlags XOR ADS_UF_DONT_EXPIRE_PASSWD
o_User.Put "UserFlags", o_Flag
o_User.SetInfo
End If

' Clean up objects
Set o_User = Nothing
Set o_WinNT = Nothing
End Function

For Internationalization purposes, we cannot use the "Administrator" group. Because in localized Windows it could be Administratoren, Administradores, Administrateurs, etc. So here is a function that gets this group:

Function GetAdminGroup
Dim o_Net
Dim o_WMI
Dim o_Accounts
Dim o_Account
Dim s_Computer

Set o_Net = CreateObject("WScript.Network")
s_Computer = o_Net.ComputerName

Set o_WMI = GetObject("WinMgMts:\\" & s_Computer & "\Root\CimV2")

' Hinweis:
' Ab welchem Betriebssystem wird diese festgelegte SID nicht mehr gültig sein?
' Das funktioniert für Windows 2000, Windows XP, Windows 2003 & Windows Vista
Set o_Accounts = o_WMI.ExecQuery("Select * From Win32_Group Where Domain = '" & s_Computer & "' AND SID = 'S-1-5-32-544'")

For Each o_Account in o_Accounts
' Erstes admin account finden und abbrechen
GetAdminGroup = o_Account.Name
Exit For
Next

Set o_Account = Nothing
Set o_Accounts = Nothing
Set o_WMI = Nothing
Set o_Net = Nothing
End Function

And finally:
Function AddUserToGroup(s_User,s_Group)
Dim o_Net
Dim o_WinNT
Dim o_User
Dim o_Group

Set o_Net = CreateObject("WScript.Network")
Set o_WinNT = GetObject("WinNT://" & o_Net.ComputerName)
Set o_User = GetObject("WinNT://" & o_Net.ComputerName & "/" & s_User & ", User")
Set o_Group = GetObject("WinNT://" & o_Net.ComputerName & "/" & s_Group & ", Group")
o_Group.Add o_User.ADsPath
o_Group.SetInfo

' Clean up objects
Set o_Group = Nothing
Set o_User = Nothing
Set o_WinNT = Nothing
Set o_Net = Nothing
End Function

So, all in all:
# Add the user
CreateUser "Lala", "Lala Teletubby", "Lala Teletubby is a yellow figure", "my password"

# Get the admin group
AdminGroup = GetAdminGroup

# Put the user in the group
AddUserToGroup "Lala", AdminGroup



Of course I use some more functions to check for the existence of the user and group before trying to create. And if the user already is in the group, etc. to avoid unnecessary errors during setup.

Hope to have helped.

Juan.

P.S.: Sorry for the German comments.


krakos

krakos
  • Full Members
  • 8 posts

Posted 02 September 2009 - 09:55

Thats much more elegant!
But where and how do you check if the user allready exists?
And how do you implement this vb-code into installshield??
Did you make a VB-Project in VisualStudio? I ask, because VB6 has no commandline projects and i dont know whats the best way to implement VB_Script.

QUOTE
P.S.: Sorry for the German comments.
Das macht doch nichts! wink.gif