Page 1 of 1 1
Topic Options
#190536 - 2008-11-07 03:28 PM Error in UDF to add a computer to a group via ADSI
Sam_B Offline
Getting the hang of it

Registered: 2005-10-25
Posts: 68
Loc: Bern, Switzerland
Hello experts, I'm lost once again (as usual): I need to add a specific computer to a group in ADS. To to that, I've written the following script: (to be honest, I have modified a similar UDF that does the same thing for a user). The script crashes at command $Group.Add($Computer.ADsPath) and @serror says "The storage control block address is invalid" Can anybody of you show me what I'm doing wrong?

many thanks in advance!

Sam
 Code:
; ----------------------------------------------------------------------------------------------------------------------
; Adds given machine to Group passed to function
; ----------------------------------------------------------------------------------------------------------------------
Function AddComputerToGroup($DomainName, $ComputerName, $GroupName)	
  ;Creating machine and Group objects
  $Computer = GetObject("WinNT://" + $DomainName + "/" + $ComputerName + ",computer")
  If $Computer = 0 ; User not found
    ConvertCOMError(@ERROR)
    $AddComputerToGroup = "Error " + @SERROR + " occured during GetObject(Computer)"
    Return
  EndIf
  $Group = GetObject("WinNT://" + $DomainName + "/" + $GroupName + ",group")
  If $Group = 0 ; Group not found
    ConvertCOMError(@ERROR)
    $AddComputerGroup = "Error " + @SERROR + " occured during GetObject(Group)"
    Return
  EndIf	
  ;Adding user to group
  $Group.Add($Computer.ADsPath)
  If @ERROR <> 0
    ConvertCOMError(@ERROR)
    $AddComputerToGroup = "Error " + @SERROR + " occured during Group.Add"
    Return
  EndIf	
  ;Writes the data to the SAM database from memory.
  $Group.SetInfo
  If @ERROR <> 0
    $AddComputerToGroup = "Error " + @SERROR + " occured during Group.SetInfo"
  Else
    $AddComputerToGroup = @SERROR + " adding Computer " + $ComputerName + " to group " + $GroupName
  EndIf	
  ;Object cleanup.
  $Computer = 0
  $Group = 0
EndFunction


Edited by Allen (2008-11-07 03:56 PM)
Edit Reason: added code tags

Top
#190537 - 2008-11-07 04:15 PM Re: Error in UDF to add a computer to a group via ADSI [Re: Sam_B]
Sam_B Offline
Getting the hang of it

Registered: 2005-10-25
Posts: 68
Loc: Bern, Switzerland
thanks to Allan for the code formatting. Seems that I'm too stupid to do it myself...
Top
#190540 - 2008-11-07 11:46 PM Re: Error in UDF to add a computer to a group via ADSI [Re: Sam_B]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Loosely based on the first example found here:
http://www.microsoft.com/communities/new...a0c7968fcb7&p=1

Untested. Doesn't seem all that much different from yours either... but it give it a try.
If AddcomputertoGroup("YourDomain","YourComputer","YourGroup")=0
? "Success"
else
? "Error"
? @serror
endif


function AddComputerToGroup($Domain,$Computer,$group)
dim $objGroup,$objComputer,$RC
$AddComputerToGroup=1
$objGroup = GetObject("WinNT://" + $Domain + "/" + $Group + ",group")
if @error=0
$objComputer = GetObject("WinNT://" + $Domain + "/" + $Computer + ",computer")
if @error=0
;Check if computer already a member of the group.
if $objGroup.IsMember($objComputer.ADsPath) = Not 1 ;false
$RC=$objGroup.Add($objComputer.ADsPath)
if @error=0
$AddComputertoGroup=0
endif
exit @error
endif
else
exit @error
endif
else
exit @error
endif
endfunction

Top
#190546 - 2008-11-10 10:04 AM Re: Error in UDF to add a computer to a group via ADSI [Re: Allen]
Sam_B Offline
Getting the hang of it

Registered: 2005-10-25
Posts: 68
Loc: Bern, Switzerland
Thanks very much for the script, Allan, but...neither the VB scripts in the link nor your example works in my environment. I always get a exception during objGrou.Add() I can manually add the computer account to the group, therefore, I think it is not a permission problem of the user account. Any other ideas what causes this problem? Using a similar code segment that adds a user to group works perfect...
Top
#190549 - 2008-11-11 12:38 AM Re: Error in UDF to add a computer to a group via ADSI [Re: Sam_B]
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
When You are using the WinNT object the name of a computer has an added $-sign in its name (WinNT name).

So Your line in the starting post:
$Computer = GetObject("WinNT://" + $DomainName + "/" + $ComputerName + ",computer")

Should be changed to:
$Computer = GetObject("WinNT://" + $DomainName + "/" + $ComputerName + "$$",computer")

-Erik


Edited by kholm (2008-11-11 12:45 AM)

Top
#190550 - 2008-11-11 09:37 AM Re: Error in UDF to add a computer to a group via ADSI [Re: kholm]
Sam_B Offline
Getting the hang of it

Registered: 2005-10-25
Posts: 68
Loc: Bern, Switzerland
Not sure your're right... Adding a $ sign causes getObject to fail with "the network path could not be found"... Omitting the $ sign seems to work fine, I can even retrieve the computer name after GetObject, but still fail adding the computer object to the group. Strange...
 Code:
$strDomain = "EMEA"
$strComputer = "SBLAETTL-7"
$objComputer = GetObject("WinNT://" + $strDomain + "/" + $strComputer + ",computer")
If @Error = 0
  ? "Name " + $objComputer.name
Else
  ? "Error " + @SERROR + " occured"
EndIf


Edited by Sam_B (2008-11-11 09:38 AM)

Top
#190602 - 2008-11-15 11:51 PM Re: Error in UDF to add a computer to a group via ADSI [Re: Sam_B]
Sam_B Offline
Getting the hang of it

Registered: 2005-10-25
Posts: 68
Loc: Bern, Switzerland
I have now a solution that works, using dsmod / NameTranslate, but I'm still very interested why my first solution doesn't work. Any hints?

 Code:
Function TranslateName ($InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType)
    
    Dim $InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType
    Dim $NameTranslate, $ReturnName, $Error, $ErrorText
    
    $Error = 0
    $ErrorText = ""
    $ReturnName = ""
    $NameTranslate = CreateObject ("NameTranslate")
    $Error = @error
    $ErrorText = @serror
    If $Error = 0
        $NameTranslate.Init ($InitType, $BindName)
        $Error = @error
        $ErrorText = @serror
        If $Error = 0
            $NameTranslate.Set ($LookupNameType, $LookupName)
            $Error = @error
            $ErrorText = @serror
            If $Error = 0
                $ReturnName = $NameTranslate.Get($ReturnNameType)
                $Error = @error
                $ErrorText = @serror
            EndIf
        EndIf
    EndIf
    $TranslateName = $ReturnName, $Error, $ErrorText
EndFunction
Function AddComputerToGroup($ComputerName, $GroupName)
  $objGroup = GetObject("WinNT://" + @Domain + "/" + $GroupName + ",group")
  If @error
    $AddComputerToGroup = 1
    Return
  EndIf
  If $ObjGroup.IsMember("WinNT://" + @Domain + "/" + $ComputerName + "$$")
    $AddComputerToGroup = 1
    Return
  EndIf  
  $GroupDN = TranslateName(3, "", 3, "@Domain\" + $GroupName, 1)
  If $GroupDN[1] = 0 ; ErrorCode
    $ComputerDN = TranslateName(3, "", 3, "@Domain\" + $ComputerName + "$", 1)
    If $ComputerDN[1] = 0
      $Cmd = 'cmd /c dsmod group "' + $GroupDN[0] + '" -addmbr "' + $ComputerDN[0] + '" >nul'
      Shell $Cmd
      $AddComputerToGroup = @ERROR
    Else
      $AddComputerToGroup = $ComputerDN[1]
    EndIf
  Else
    $AddComputerToGroup = $GroupDN[1]
  EndIf
EndFunction
$Ret = AddComputerToGroup("ComputerName", "GroupName")

Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 405 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.061 seconds in which 0.03 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org