#191051 - 2008-12-12 06:41 AM
bunch of Local User Managment functions
|
lukeod
Getting the hang of it
Registered: 2008-01-11
Posts: 70
Loc: Australia
|
Hey All,
I was hard pressed to find how to perform local user managment via Kix. There are examples on how to do some of it remotley as well as for Active Directory but tracking down ones that can be run against the local machine's users was harder to find. I'm suprised there isnt already UDF's for much of the below.
These scripts can be used during shutdown or startup, thereby negating the need for administrative priviliges as well as issues around the scripts containing plain text passwords.
Most of the below code is recycled code from many sources. Appologies for lack of acknowlegment to the original coders.
They could use work along the lines of error handling.
Function ModifyPassword($strUserName, $strPassword)
;DESCRIPTION: Modify the password of a user account
;Declate Variables
Dim $objUser
;Create Object
$objUser = GetObject("WinNT://" + @WKSTA + "/" + $strUserName + "")
;Set the Password
$objUser.SetPassword($strPassword)
;If it failed, return an error
$ModifyPassword = IIf(@ERROR, 'Failed ' + @SERROR , 'Successful') + ' '
EndFunction
Function GetLocalUsers()
;DESCRIPTION : Returns an array containing all local user accounts
;Declare Variables
Dim $strHost, $String
;Initilise Variables
$strHost = @WKSTA
$String = ""
;Get a ';' delimited string containing all the local user accounts
For Each $Object in GetObject("WinNT://" + $strHost)
If $Object.Class = "User"
$String = $String + $Object.name + ";"
EndIf
Next
;Check if the string is empty, if not, create an array
If Len($String) = 0
;No Members in the Group
$GetLocalUsers = "ERROR!!! " + @SERROR
Exit 2
Else
;Create an array from the string
$GetLocalUsers = Split($String, ";")
EndIf
EndFunction
Function GetLocalGroupMembers($strGroupName)
;DESCRIPTION : Returns an array containing the members of the group passed to the Function
;Declare Variables
Dim $strHost, $String, $Object
;Initilise Variables
$strHost = @WKSTA
$String = ""
For Each $Object in GetObject("WinNT://" + $strHost)
If $Object.Class = "Group"
If $Object.Name = $strGroupName
For Each $Member in $Object.members
$String = $String + $Member.name + ";"
Next
EndIf
EndIf
Next
If Len($String) = 0
;No Members in the Group
@SERROR = "No members in the Group Specified (" + $strGroupName + ")"
$GetLocalGroupMembers = "No members in the Group Specified (" + $GroupName + ")"
Exit 2
Else
$GetLocalGroupMembers = Split($String, ";")
EndIf
EndFunction
Function AddLocalAdmin($strUserAccount)
;DESCRIPTION: Attempts to add the account to the local administrators group
Dim $strComputer, $objComputer, $objUser, $objGroup
$strComputer = @WKSTA
;Create Objects
$objGroup = GetObject("WinNT://" + $strComputer + "/Administrators,group")
$objUser = GetObject("WinNT://" + $strComputer + "/" + $strUserAccount + ",user")
;Create the User Account
$objGroup.Add($objUser.ADsPath)
EndFunction
Function CreateLocalUser($strName, $strPassword, $Description)
;DESCRIPTION : Attempts to create a local user account
Dim $strComputer, $objComputer, $objUser
;Set Variables
$strComputer = @WKSTA
;Create Object
$objComputer = GetObject("WinNT://" + $strComputer)
;Create object of class user on the computer.
$objUser = $objComputer.Create("user", $strName)
$objUser.Put("description", $Description)
;Save changes.
$objUser.SetInfo
;set password.
$objUser.SetPassword($strPassword)
$objUser.SetInfo
EndFunction
Function SetNeverExpire($strUserAccount)
;DESCRIPTION: Sets the account's password to never expire
;Original code by Chris S, just function'ised it (that even a word? :P)
;Variables
Dim $ADS_UF_DONT_EXPIRE_PASSWD, $strComputer, $objUser
$ADS_UF_DONT_EXPIRE_PASSWD = &10000
$strComputer = @WKSTA
;Create Objects
$objUser = GetObject("WinNT://" + $strComputer + "/" + $strUserAccount + ",user")
$objUserFlags = $objUser.Get("UserFlags")
If ($objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD) = 0
$objPasswordExpirationFlag = $objUserFlags + $ADS_UF_DONT_EXPIRE_PASSWD
$objUser.Put("userFlags", $objPasswordExpirationFlag)
$objUser.SetInfo
@SERROR ?
Else
"Account already has the Password Never Expires Flag." ?
EndIf
EndFunction
Edited by lukeod (2008-12-12 06:50 AM)
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(StuTheCoder)
and 798 anonymous users online.
|
|
|