Snippets ( Windows Users )
Add User Accounts
Author: MHz
_NetUser('Jack', 'pw_abc')
_NetUser('Jill', 'pw_def')
; Add User Accounts
Func _NetUser($sName, $sPassword = '', $sGroupName = 'Administrators', $iAutoLogon = 0)
; Creates user accounts. Only 1 user can have autologon, if set.
If Not FileExists(EnvGet('AllUsersProfile') & '\..\' & $sName) Then
RunWait(@ComSpec & ' /c ' & _
'Net User ' & $sName & ' ' & $sPassword & ' /add &&' & _
'Net LocalGroup ' & $sGroupName & ' ' & $sName & ' /add &' & _
'Net Accounts /MaxPwAge:UnLimited', '', @SW_HIDE)
If $iAutoLogon Then
Local $sRegKey = 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
RegWrite($sRegKey, 'DefaultUserName', 'Reg_sz', $sName)
RegWrite($sRegKey, 'DefaultPassword', 'Reg_sz', $sPassword)
RegWrite($sRegKey, 'AutoAdminLogon', 'Reg_sz', 1)
EndIf
EndIf
EndFunc ;==>_NetUser
_GetLogonTime
Author: guinness
; Get the last date and time the user logged on.
#include <Date.au3>
ConsoleWrite(_GetLogonTime() & @LF)
Func _GetLogonTime($sUserName = @UserName, $sComputerName = ".") ; Idea by trancexx: http://www.autoitscript.com/forum/topic/113611-if-isadmin-not-detected-as-admin/
Local $aRet = DllCall("netapi32.dll", "long", "NetUserGetInfo", "wstr", $sComputerName, "wstr", $sUserName, "dword", 11, "ptr*", 0)
If @error Or $aRet[0] Then Return SetError(1, 0, False)
Local $sHours = DllStructGetData(DllStructCreate("ptr;ptr;ptr;ptr;dword;dword;dword;ptr;ptr;dword;dword;dword;dword;ptr;dword;ptr;dword;dword;byte;dword", $aRet[4]), 18)
DllCall("netapi32.dll", "long", "NetApiBufferFree", "ptr", $aRet[4])
Return _DateAdd("h", "-" & $sHours, _NowCalc())
EndFunc ;==>_GetLogonTime
_GetUserFullName
Author: guinness
ConsoleWrite('Current user''s full name is: ' & _GetUserFullName() & @CRLF)
; Get the full name of the current user. May be blank if you haven't set a full name.
Func _GetUserFullName()
Local $oWMIService = ObjGet('winmgmts:\\localhost\root\CIMV2'), $sReturn = ''
Local $oColItems = $oWMIService.ExecQuery('Select * From Win32_UserAccount', 'WQL')
If IsObj($oColItems) Then
For $oItem In $oColItems
Return $oItem.FullName
Next
EndIf
EndFunc ;==>_GetUserFullName
_GetLocalUsers
Author: spudw2k
ConsoleWrite(_GetLocalUsers() & @CRLF)
;Retrieve Local Machine Users
Func _GetLocalUsers($host = @ComputerName)
Local $Users = ""
Local $colUsers = ObjGet("WinNT://" & $host & "")
If Not IsObj($colUsers) Then Return 0
Dim $filter[1] = ["user"]
$colUsers.Filter = $filter
For $objUser In $colUsers
$Users &= $objUser.name & @CR
Next
$Users = StringTrimRight($Users,1)
$colUsers = 0
$filter = 0
Return $Users
EndFunc ;==>_GetLocalUsers
_GetLocalAdmins
Author: spudw2k
ConsoleWrite(_GetLocalAdmins() & @CRLF)
;Retrieve Members of the Local Administrators Group
Func _GetLocalAdmins($host = @ComputerName)
Local $AdminUsers = ""
Local $colGroups = ObjGet("WinNT://" & $host & "")
If Not IsObj($colGroups) Then Return 0
Dim $filter[1] = ["group"]
$colGroups.Filter = $filter
For $objGroup In $colGroups
If $objGroup.name = "Administrators" Then
For $objUser In $objGroup.Members
$AdminUsers &= $objUser.name & @CR
Next
$AdminUsers = StringTrimRight($AdminUsers,1)
EndIf
Next
$colGroups = 0
$filter = 0
Return $AdminUsers
EndFunc ;==>_GetLocalAdmins
_IsAdminEnabled
Author: guinness
ConsoleWrite(_IsAdminEnabled() & @CRLF)
Func _IsAdminEnabled() ; By Rover and guinness
Local $oWMIService = ObjGet('winmgmts:\\localhost\root\CIMV2')
Local $oColItems = $oWMIService.ExecQuery('SELECT * FROM Win32_UserAccount WHERE Name = "Administrator"', "WQL", 0x30)
If IsObj($oColItems) Then
For $oItem In $oColItems
Return $oItem.Disabled = False
Next
EndIf
Return True
EndFunc ;==>_IsAdminEnabled
_IsAdminEnabledEx
Author: guinness
#include <Constants.au3>
ConsoleWrite(_IsAdminEnabledEx() & @CRLF)
; Check if the Administrator account is enabled. Using 'net user' and reading the Standard Stream Output.
Func _IsAdminEnabledEx()
Local $iPID = Run(@ComSpec & ' /c net user Administrator', @SystemDir, @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD), $sReturn = ''
While 1
$sReturn &= StdoutRead($iPID)
If @error Then
ExitLoop
EndIf
WEnd
Local $aReturn = StringRegExp($sReturn, 'Account active\s*(.*?)\r', 3)
If @error Then
Return SetError(1, 0, False)
EndIf
Return $aReturn[0] = 'Yes'
EndFunc ;==>_IsAdminEnabledEx
_IsGuestEnabled
Author: guinness
ConsoleWrite(_IsGuestEnabled() & @CRLF)
Func _IsGuestEnabled() ; By Rover and guinness
Local $oWMIService = ObjGet('winmgmts:\\localhost\root\CIMV2')
Local $oColItems = $oWMIService.ExecQuery('SELECT * FROM Win32_UserAccount WHERE Name = "Guest"', "WQL", 0x30)
If IsObj($oColItems) Then
For $oItem In $oColItems
Return $oItem.Disabled = False
Next
EndIf
Return True
EndFunc ;==>_IsGuestEnabled
_IsGuestEnabledEx
Author: guinness
#include <Constants.au3>
ConsoleWrite(_IsGuestEnabledEx() & @CRLF)
; Check if the Guest account is enabled. Idea by Chimaera.
Func _IsGuestEnabledEx()
Local $iPID = Run(@ComSpec & ' /c net user Guest', @SystemDir, @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD), $sReturn = ''
While 1
$sReturn &= StdoutRead($iPID)
If @error Then
ExitLoop
EndIf
WEnd
Local $aReturn = StringRegExp($sReturn, 'Account active\s*(.*?)\r', 3)
If @error Then
Return SetError(1, 0, False)
EndIf
Return $aReturn[0] = 'Yes'
EndFunc ;==>_IsGuestEnabledEx
_IsPasswordExpired
Author: guinness
ConsoleWrite(_IsPasswordExpired() & @CRLF)
Func _IsPasswordExpired($sComputerName = @UserName)
Local $oWMIService = ObjGet('winmgmts:\\localhost\root\CIMV2')
Local $oColItems = $oWMIService.ExecQuery('Select * From Win32_UserAccount Where Name = "' & $sComputerName & '"', "WQL", 0x30)
If IsObj($oColItems) Then
For $oItem In $oColItems
Return $oItem.PasswordExpires
Next
EndIf
Return True
EndFunc ;==>_IsPasswordExpired
_SetWorkGroupName
Author: JScript
$sNewWorkgroupName = "HOMEOS"
_SetWorkGroupName($sNewWorkgroupName)
Func _SetWorkGroupName($sGroupName)
Local $aRet = DllCall("Netapi32.dll", "long", "NetJoinDomain", "int", 0, "wstr", $sGroupName, "int", 0, "int", 0, "int", 0, "dword", 0x00000040)
Return $aRet[0]
EndFunc ;==>_SetWorkGroupName
_WhoAmI
Author: guinness
#include <Constants.au3>
ConsoleWrite(_WhoAmI() & @CRLF)
; Displays the username and domain for the currently logged in user.
Func _WhoAmI()
Local $iPID = Run(@ComSpec & ' /c whoami', @SystemDir, @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD), $sReturn = ''
While 1
$sReturn &= StdoutRead($iPID)
If @error Then
ExitLoop
EndIf
WEnd
Return StringRegExpReplace(StringUpper($sReturn), '\n|\r', '')
EndFunc ;==>_WhoAmI