Jump to content
SOF-TECH

Get local Admin and power users in same function

Recommended Posts

SOF-TECH

Hi guys, 

I am new to autoit an find amazing.

I have script that collects Group Admin members and I display it. I want it  to display also Power user Groups in the same time.

the script looks for the Administrator Group based on SID = "S-1-5-32-544"     As for Power User SID = "S-1-5-32-547"

Below is the original script. I appreciate an y suggestion ; 

 

MsgBox(0, "Administrator & Power users Manager", WMI_GetLocalAdminMembership())

Func WMI_GetLocalAdminMembership($sHost = @ComputerName) 
    If $sHost = "Localhost" Then $sHost = @ComputerName
    Local $LM_members, $x, $LM_LocalGroup_Name, $type
    $LM_LocalGroup_Name = "Power Users"
    $objWMIService = ObjGet("winmgmts:\\" & $sHost & "\root\cimv2")
    $colItems = $objWMIService.ExecQuery("Select Name, SID from Win32_Group WHERE Domain='" & $sHost & "'", "WQL", 0x30)
    If IsObj($colItems) Then
        For $objItem In $colItems
            If $objItem.SID = "S-1-5-32-544" Then $LM_LocalGroup_Name = $objItem.Name
            
        Next
    Else
        Return SetError (1, 0, 0) ;No WMI objects found for class Win32_Group
    EndIf

    $colItems = $objWMIService.ExecQuery("Select * from Win32_GroupUser Where GroupComponent=""Win32_Group.Domain='" & $sHost & "',Name='" & $LM_LocalGroup_Name & "'""", "WQL", 0x30)
    If IsObj($colItems) Then
        For $objItem In $colItems
            If $objItem.PartComponent <> "" Then
                $x = StringSplit($objItem.PartComponent, """")
                $type = StringMid($x[1], StringInStr($x[1], ":Win32_") + 7, (StringInStr($x[1], ".") - (StringInStr($x[1], ":Win32_") + 7)))
                $LM_members &= $sHost & ";" & $LM_LocalGroup_Name & ";" & $type & ";" & $x[2] & "\" & $x[4] & @CRLF
            EndIf
        Next
        Return $LM_members
    EndIf
    Return SetError (2, 0, 0) ;No WMI objects found for class Win32_GroupUser
EndFunc   ;==>WMI_GetLocalAdminMembership

    $colItems = $objWMIService.ExecQuery("Select * from Win32_GroupUser Where GroupComponent=""Win32_Group.Domain='" & $sHost & "',Name='" & $LM_LocalGroup_Name & "'""", "WQL", 0x30)
    If IsObj($colItems) Then
        For $objItem In $colItems
            If $objItem.PartComponent <> "" Then
                $x = StringSplit($objItem.PartComponent, """")
                $type = StringMid($x[1], StringInStr($x[1], ":Win32_") + 7, (StringInStr($x[1], ".") - (StringInStr($x[1], ":Win32_") + 7)))
                $LM_members &= $sHost & ";" & $LM_LocalGroup_Name & ";" & $type & ";" & $x[2] & "\" & $x[4] & @CRLF
            EndIf
        Next
        Return $LM_members
    EndIf
    Return SetError (2, 0, 0) ;No WMI objects found for class Win32_GroupUser
EndFunc   ;==>WMI_GetLocalAdminMembership

 

Thank you in Advance 

Share this post


Link to post
Share on other sites
Juvigy

So just mind the SID and the group name - first do it for admin , then for power users.

$LM_LocalGroup_Name

  for the second run change this line and update the SID :

  If $objItem.SID = "S-1-5-32-544" Then $LM_LocalGroup_Name = $objItem.Name
Edited by Juvigy
update

Share this post


Link to post
Share on other sites
Subz

Another method:

#include <Array.au3>
Local $aGroupMembers[1][3]
_LocalGroupMembers(@ComputerName, "Administrators")
_LocalGroupMembers(@ComputerName, "Power Users")
_ArrayDisplay($aGroupMembers, "Local Group Members", "", 0, Default, "Username|Domain|Group Name")
Func _LocalGroupMembers($_sComputer = @ComputerName, $_sLocalGroupName = "Administrators")
    Local $aMember
    Local $oComputer = ObjGet("WinNT://" & $_sComputer & "/" & $_sLocalGroupName)
    For $oGroups In $oComputer.Members
        $aMember = StringSplit(StringReplace($oGroups.AdsPath, "WinNT://", ""), "/")
        _ArrayAdd($aGroupMembers, $aMember[$aMember[0]] & "|" & $aMember[$aMember[0] - 1] & "|" & $_sLocalGroupName)
    Next
    $aGroupMembers[0][0] = UBound($aGroupMembers) - 1
EndFunc

 

Share this post


Link to post
Share on other sites
SOF-TECH
21 hours ago, Juvigy said:

So just mind the SID and the group name - first do it for admin , then for power users.

$LM_LocalGroup_Name

  for the second run change this line and update the SID :

  If $objItem.SID = "S-1-5-32-544" Then $LM_LocalGroup_Name = $objItem.Name

Hi Juvigy,

thank you, second run inspired me to add new function as below and run it for Power Users check.  I just made the display of the result of both Function  on one  MsgBox

WMI_GetLocalPowerUser

Share this post


Link to post
Share on other sites
SOF-TECH
17 hours ago, Subz said:

Another method:

#include <Array.au3>
Local $aGroupMembers[1][3]
_LocalGroupMembers(@ComputerName, "Administrators")
_LocalGroupMembers(@ComputerName, "Power Users")
_ArrayDisplay($aGroupMembers, "Local Group Members", "", 0, Default, "Username|Domain|Group Name")
Func _LocalGroupMembers($_sComputer = @ComputerName, $_sLocalGroupName = "Administrators")
    Local $aMember
    Local $oComputer = ObjGet("WinNT://" & $_sComputer & "/" & $_sLocalGroupName)
    For $oGroups In $oComputer.Members
        $aMember = StringSplit(StringReplace($oGroups.AdsPath, "WinNT://", ""), "/")
        _ArrayAdd($aGroupMembers, $aMember[$aMember[0]] & "|" & $aMember[$aMember[0] - 1] & "|" & $_sLocalGroupName)
    Next
    $aGroupMembers[0][0] = UBound($aGroupMembers) - 1
EndFunc

 

Hi Subz,

Thank you also for the array table suggestion.

This script is part long  bunch of  functions and prefer instant pop up message for every step instead of array table. 

 

In short, I got efficient support. this forum members are really awesome. thank you guys 

Share this post


Link to post
Share on other sites
Subz

You could use the single function and just add parameters like I did for the Power Users group/sid

Share this post


Link to post
Share on other sites
AdamUL

If you need to work more with local users and groups, have a look at the Local Account UDF.  

 

Adam

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×