Snippets ( Windows Users )

From AutoIt Wiki
Jump to navigation Jump to search

Please always credit an author in your script if you use their code. It is only polite.


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

ReturnToContents

_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

ReturnToContents

_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

ReturnToContents

_GetLocalUsers

Author: spudw2k








ConsoleWrite(_GetLocalUsers() & @CRLF)

;Retrieve Local Machine Users
Func _GetLocalUsers($sHost = @ComputerName)
   Local $sUsers = ""
   Local $oColUsers = ObjGet("WinNT://" & $sHost & "")
   If Not IsObj($oColUsers) Then Return 0
   Dim $aFilter[1] = ["user"]
   $oColUsers.Filter = $aFilter
   For $oUser In $oColUsers
	  $sUsers &= $oUser.name & @CR
   Next
   $sUsers = StringTrimRight($sUsers,1)
   $oColUsers = 0
   $aFilter = 0
   Return $sUsers
EndFunc   ;==>_GetLocalUsers

ReturnToContents

_GetLocalAdmins

Author: spudw2k








ConsoleWrite(_GetLocalAdmins() & @CRLF)

;Retrieve Members of the Local Administrators Group
Func _GetLocalAdmins($sHost = @ComputerName)
   Local $sAdminUsers = ""
   Local $oColGroups = ObjGet("WinNT://" & $sHost & "")
   If Not IsObj($oColGroups) Then Return 0
   Local Const $bAdminGroupSID = "0x01020000000000052000000020020000"
   Local $aFilter[1] = ["group"]
   $oColGroups.Filter = $aFilter
   For $oGroup In $oColGroups
	  If $oGroup.objectSID = $bAdminGroupSID Then
		 For $oUser In $oGroup.Members
			$sAdminUsers &= $oUser.Name & @CR
		 Next
		 $sAdminUsers = StringTrimRight($sAdminUsers,1)
	  EndIf
   Next
   $oColGroups = 0
   $aFlter = 0
   Return $sAdminUsers
EndFunc   ;==>_GetLocalAdmins

ReturnToContents

_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

ReturnToContents

_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

ReturnToContents

_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

ReturnToContents

_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

ReturnToContents

_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

ReturnToContents

_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

ReturnToContents

_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

ReturnToContents