; ----------------------------------------------------------------------------------------------------------------------
; 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