#190536 - 2008-11-07 03:28 PM
Error in UDF to add a computer to a group via ADSI
|
Sam_B
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
; ----------------------------------------------------------------------------------------------------------------------
; 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
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
|
|
|
|
#190546 - 2008-11-10 10:04 AM
Re: Error in UDF to add a computer to a group via ADSI
[Re: Allen]
|
Sam_B
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
|
|
|
|
#190550 - 2008-11-11 09:37 AM
Re: Error in UDF to add a computer to a group via ADSI
[Re: kholm]
|
Sam_B
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...
$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
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?
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
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
0 registered
and 685 anonymous users online.
|
|
|