Snippets ( Windows Users ): Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
m (+Snippets Category (+ normalize top))
(Edited snippets to conform to template Snippet Header.)
Line 4: Line 4:
{{Snippet Credit Header}}
{{Snippet Credit Header}}


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _Add User Accounts ~ Author - [http://www.autoitscript.com/forum/user/2709-mhz/ MHz] '''</blockquote> =====
== Add User Accounts ==
 
{{Snippet Header
| AuthorURL = 2709-mhz
| AuthorName = MHz
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 27: Line 32:
EndFunc  ;==>_NetUser
EndFunc  ;==>_NetUser
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _GetLogonTime() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _GetLogonTime ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 46: Line 57:
EndFunc  ;==>_GetLogonTime
EndFunc  ;==>_GetLogonTime
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _GetUserFullName() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _GetUserFullName ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}
 
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
ConsoleWrite('Current user''s full name is: ' & _GetUserFullName() & @CRLF)
ConsoleWrite('Current user''s full name is: ' & _GetUserFullName() & @CRLF)
Line 63: Line 81:
EndFunc  ;==>_GetUserFullName
EndFunc  ;==>_GetUserFullName
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">'''  _IsAdminEnabled() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _IsAdminEnabled ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 81: Line 105:
EndFunc  ;==>_IsAdminEnabled
EndFunc  ;==>_IsAdminEnabled
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">'''  _IsAdminEnabledEx() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _IsAdminEnabledEx ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 107: Line 137:
EndFunc  ;==>_IsAdminEnabledEx
EndFunc  ;==>_IsAdminEnabledEx
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">'''  _IsGuestEnabled() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _IsGuestEnabled ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 124: Line 160:
EndFunc  ;==>_IsGuestEnabled
EndFunc  ;==>_IsGuestEnabled
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">'''  _IsGuestEnabledEx() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _IsGuestEnabledEx ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 150: Line 192:
EndFunc  ;==>_IsGuestEnabledEx
EndFunc  ;==>_IsGuestEnabledEx
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">'''  _IsPasswordExpired() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _IsPasswordExpired ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 168: Line 216:
EndFunc  ;==>_IsPasswordExpired
EndFunc  ;==>_IsPasswordExpired
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _SetWorkGroupName() ~ Author - [http://www.autoitscript.com/forum/user/44525-jscript/ JScript] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _SetWorkGroupName ==
 
{{Snippet Header
| AuthorURL = 44525-jscript
| AuthorName = JScript
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 181: Line 235:
EndFunc  ;==>_SetWorkGroupName
EndFunc  ;==>_SetWorkGroupName
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">'''  _WhoAmI() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _WhoAmI ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 202: Line 262:
EndFunc  ;==>_WhoAmI
EndFunc  ;==>_WhoAmI
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]
 
[[#top | ReturnToContents]]

Revision as of 18:27, 18 November 2012


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

_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