Snippets ( Windows Users ): Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
 
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<div class="center" style="width:auto; margin-left:auto; margin-right:auto;">'''Please always credit an author in your script if you use their code, Its only polite.'''</div>
__TOC__
===== <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> =====
[[category:Snippets]]
 
{{Snippet Credit Header}}
 
== Add User Accounts ==
 
{{Snippet Header
| AuthorURL = 2709-mhz
| AuthorName = MHz
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 23: 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 42: 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 59: 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]]
 
== _GetLocalUsers ==
 
{{Snippet Header
| AuthorURL = 26215-spudw2k
| AuthorName = spudw2k
}}
 
<syntaxhighlight lang="autoit">
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</syntaxhighlight>
 
[[#top | ReturnToContents]]
 
== _GetLocalAdmins ==
 
{{Snippet Header
| AuthorURL = 26215-spudw2k
| AuthorName = spudw2k
}}
 
<syntaxhighlight lang="autoit">
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</syntaxhighlight>
 
[[#top | ReturnToContents]]
 
== _IsAdminEnabled ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 77: Line 166:
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 103: Line 198:
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 120: Line 221:
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 146: Line 253:
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 164: Line 277:
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 177: Line 296:
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 198: Line 323:
EndFunc  ;==>_WhoAmI
EndFunc  ;==>_WhoAmI
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]
 
[[#top | ReturnToContents]]

Latest revision as of 17:19, 1 April 2015


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