Snippets ( Windows Users ): Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
(No difference)

Revision as of 16:02, 21 May 2012

Please always credit an author in your script if you use their code, Its 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

Return To Contents

_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

Return To Contents

_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

Return To Contents

_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

Return To Contents

_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

Return To Contents

_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

Return To Contents

_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

Return To Contents

_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

Return To Contents

_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

Return To Contents

_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

Return To Contents