Jump to content

Trying to learn to use COM objects...


Recommended Posts

  • Developers

That worked, and so did this:

; Add the user to the group
; $objGroup.add ($objUser)
    $objGroup.add ("WinNT://" & @ComputerName & "/" & $sNewGrpMbr)

This raises a new question... what if some user and group names happen to match? The call to "WinNT://" & @ComputerName & "/" & $sNewGrpMbr depends on unique naming. If there is a user called Test and a group called Test, how do I distinguish in the COM objects, since they both would be: "WinNT://" & @ComputerName & "/Test"?

Did you notice in my code the ",group" in $objGroup = ObjGet("WinNT://" & @ComputerName & "/" & $sGrpName & ",group") ?

Think that should do it... just give it a try and see if that works .....

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Did you notice in my code the ",group" in $objGroup = ObjGet("WinNT://" & @ComputerName & "/" & $sGrpName & ",group") ?

Think that should do it... just give it a try and see if that works .....

:D Oops... umm... err... the sun was in my eyes and I missed that! Yeah, that's it... the sun... in my eyes... :">

When I tested it, I only pasted in your changed line at $objGroup.Add($objUser.ADsPath) and totaly missed that part. Thanks!

:D

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Did you notice in my code the ",group" in $objGroup = ObjGet("WinNT://" & @ComputerName & "/" & $sGrpName & ",group") ?

Think that should do it... just give it a try and see if that works .....

Seems not to have mattered much anyway. I tried to create the Testing user and the Testing group, then put the user in the group of the same name to verify that was working, and found it failed. So I tried it manualy and found you can't have a local user and group of the same name (at least on XP Pro SP2).

Thinking it would work in AD, I tried it in a Win2003 domain and you still can't have a user and group with the same name... learn something new every day... :D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks to everyone who helped, especialy JdeB! It works completely now:

2006-06-19 14:42:11 : Start Test......................................
2006-06-19 14:42:16 : Successfully added new user: TestUser
2006-06-19 14:42:16 : Successfully added new local group: Test Group
2006-06-19 14:42:25 : Successfully added user TestUser to group: Test Group

This is the code, if anyone besides me is interested:

#include<file.au3>
$LogFile = @ScriptDir & "\TestLog.log"
_FileWriteLog($LogFile, "Start Test......................................")

; Declare COM Object error handler:
Global $oComError = ObjEvent("AutoIt.Error", "_ComErrFunc")

; Test the functions
_Add_LocalUser("TestUser", "C0mplex!ty", "Mrs. Test T. User", "My test user.")

_Add_LocalGroup("Test Group", "My test group.")

_Add_Usr2Grp("TestUser", "Test Group")

;--------------------------------------
;   Local Functions
;--------------------------------------
;--------------------------------------
; Function _ComErrFunc()
;   Custom COM object error handler
;--------------------------------------
Func _ComErrFunc()
    Local $HexNumber = Hex($oComError.number, 8)
    _FileWriteLog($LogFile, "AutoIT COM Error Occured!" & @CRLF & _
            @TAB & "Error Number: " & $HexNumber & @CRLF & _
            @TAB & "Line Number: " & $oComError.scriptline & @CRLF & _
            @TAB & "Description: " & $oComError.description & @CRLF & _
            @TAB & "WinDescription: " & $oComError.windescription)
    SetError(1); something to check for when this function returns
EndFunc  ;==>_ComErrFunc


;--------------------------------------
;  Function _Add_Usr2Grp()
;   Call with:  _Add_Usr2Grp($sNewGrpMbr, $sGrpName) where:
;       $sNewGrpMbr = New member to add to the group
;       $sGrpName = local group name to add members to
;   Returns 1 for good, 0 for bad
;--------------------------------------
Func _Add_Usr2Grp($sNewGrpMbr, $sGrpName)
    Local $objGroup, $objUser
    
; Init user COM object
    $objUser = ObjGet("WinNT://" & @ComputerName & "/" & $sNewGrpMbr & ",user")
    
; Init group COM object
    $objGroup = ObjGet("WinNT://" & @ComputerName & "/" & $sGrpName & ",group")
    
; Add the user to the group
    $objGroup.Add ($objUser.ADsPath)
    $objGroup.SetInfo
    
; Return Results
    If @error = 0 Then
        _FileWriteLog($LogFile, "Successfully added user " & $sNewGrpMbr & " to group: " & $sGrpName)
        Return 1
    Else
        _FileWriteLog($LogFile, "Error adding user " & $sNewGrpMbr & " to group: " & $sGrpName & "; @Error = " & @error)
        Return 0
    EndIf
EndFunc  ;==>_Add_Usr2Grp


;--------------------------------------
; Function _Add_LocalGroup()
;   Call with:  _Add_LocalGroup($sNewGrpName, $sNewGrpDesc) where:
;       $sNewGrpName = New local group name to add
;       $sNewGrpDesc = (optional) Group's description
;   Returns 1 for success, 0 for fail
;--------------------------------------
Func _Add_LocalGroup($sNewGrpName, $sNewGrpDesc = "")
    Local $colLocalComputer, $objGroup
    
; Init COM object
    $colLocalComputer = ObjGet("WinNT://" & @ComputerName)
    
; Create user
    $objGroup = $colLocalComputer.Create ("group", $sNewGrpName)
    
; Apply properties to user
    $objGroup.Put ("Description", $sNewGrpDesc)
    $objGroup.SetInfo
    
; Return results
    If @error = 0 Then
        _FileWriteLog($LogFile, "Successfully added new local group: " & $sNewGrpName)
        Return 1
    Else
        _FileWriteLog($LogFile, "Error adding group: " & $sNewGrpName & "; @Error = " & @error)
        Return 0
    EndIf
EndFunc  ;==>_Add_LocalGroup


;--------------------------------------
; Function _Add_LocalUser
;   Call with:  _Add_LocalUser($sNewUsrName, $sNewUsrPass [, $sNewUsrFull] [, $sNewUsrDesc]) where:
;       $sNewUsrName = UserName to add
;       $sNewUsrPass = New user's password
;       $sNewUsrFull = (optional) User's full name
;       $sNewUsrDesc = (optional) User's description (comment)
;   Returns 1 for success, 0 for fail
;--------------------------------------
Func _Add_LocalUser($sNewUsrName, $sNewUsrPass, $sNewUsrFull = "", $sNewUsrDesc = "")
    Local $colLocalComputer, $objUser
    
; Init COM object
    $colLocalComputer = ObjGet("WinNT://" & @ComputerName)
    
; Create user
    $objUser = $colLocalComputer.Create ("user", $sNewUsrName)
    
; Apply properties to user
    $objUser.SetPassword ($sNewUsrPass)
    $objUser.Put ("Fullname", $sNewUsrFull)
    $objUser.Put ("Description", $sNewUsrDesc)
    $objUser.SetInfo
    
; Return results
    If @error = 0 Then
        _FileWriteLog($LogFile, "Successfully added new user: " & $sNewUsrName)
        Return 1
    Else
        _FileWriteLog($LogFile, "Error adding user: " & $sNewUsrName & "; @Error = " & @error)
        Return 0
    EndIf
EndFunc  ;==>_Add_LocalUser

Cheers! :D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...