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-0703:56 PM) Edit Reason: added code tags
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...
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...