Client Automation DSM Script - How to check if a computer belongs to AD Domain or Workgroup ?

Document ID : KB000107445
Last Modified Date : 29/08/2018
Show Technical Document Details
Introduction:

Using DSM Script how to check if a computer belongs to an AD DOMAIN or a Workgroup ?
Instructions:

Here is an example of DMS script in order to do this :

 
#if Win32
  '32-Bit declaration of the function
 Function LsaOpenPolicy Lib "Advapi32.dll" Alias "LsaOpenPolicy" (SystemName as STRING, ByRef ObjectAttributes[512] AS Byte, DesiredAccess as INTEGER, ByRef PolicyHandle as integer) as integer
 Function LsaClose Lib "Advapi32.dll" Alias "LsaClose" (PolicyHandle as integer) as integer
 Function LsaQueryInformationPolicy Lib "Advapi32.dll" Alias "LsaQueryInformationPolicy" (PolicyHandle as integer, InformationClass as integer, ByRef Buffer[4] AS Byte) as integer
 Function memcpy Lib "Ntdll.dll" Alias "memcpy" (ByRef Destination[512] as Byte, ByVal Source as Integer, length AS integer) as string
#EndIf



'**************************************************************************************
'*                               IsDomainMember
'**************************************************************************************

FUNCTION IsDomainMember(ByRef Name AS STRING) AS INTEGER

DIM iPolicyHandle as INTEGER
DIM ObjectAttributes[512] AS Byte
DIM GENERIC_READ AS INTEGER
DIM POLICY_VIEW_LOCAL_INFORMATION AS INTEGER
DIM PolicyPrimaryDomainInformation AS INTEGER
DIM POLICY_PRIMARY_DOMAIN_INFO[512] AS Byte
DIM Buffer[4] AS Byte
DIM Buffer2[512] AS Byte
DIM Addr AS INTEGER
DIM j AS INTEGER
DIM NameLength AS INTEGER

' ------- Declare some Constants ------
   GENERIC_READ=0x80000000
   POLICY_VIEW_LOCAL_INFORMATION=0x1
   PolicyPrimaryDomainInformation=3


IsDomainMember=-1

' ------- Execute Lsa Functions ------

   j=LsaOpenPolicy("",  ObjectAttributes, GENERIC_READ+POLICY_VIEW_LOCAL_INFORMATION, iPolicyHandle)
   IF j<>0 THEN
      Exit FUNCTION
   END IF

   j=LsaQueryInformationPolicy(iPolicyHandle, PolicyPrimaryDomainInformation, Buffer)
   IF j<>0 THEN
      LsaClose(iPolicyHandle)
      Exit FUNCTION
   END IF

' ------- Retrieve Name ------
   Addr=Buffer[3]*16777216+Buffer[2]*65536+Buffer[1]*256+Buffer[0]
   memcpy(POLICY_PRIMARY_DOMAIN_INFO,Addr,20)

   NameLength=POLICY_PRIMARY_DOMAIN_INFO[0]
   Addr=POLICY_PRIMARY_DOMAIN_INFO[7]*16777216+POLICY_PRIMARY_DOMAIN_INFO[6]*65536+POLICY_PRIMARY_DOMAIN_INFO[5]*256+POLICY_PRIMARY_DOMAIN_INFO[4]
   memcpy(Buffer2,Addr,NameLength)

   FOR j=0 TO NameLength STEP 2
      Name=Name+CHR(Buffer2[j]))
   NEXT j


' ------- Sid ------
   Addr=POLICY_PRIMARY_DOMAIN_INFO[11]*16777216+POLICY_PRIMARY_DOMAIN_INFO[10]*65536+POLICY_PRIMARY_DOMAIN_INFO[9]*256+POLICY_PRIMARY_DOMAIN_INFO[8]

   IF Addr<>0 THEN
      IsDomainMember=1
   ELSE
      IF COMPUTERNAME=Name THEN
         IsDomainMember=3
      ELSE
         IsDomainMember=2
      END IF
   END IF
   
   LsaClose(iPolicyHandle)

END FUNCTION





'**************************************************************************************
'*                               Main
'**************************************************************************************
DIM Name AS STRING
DIM i AS Integer

i=IsDomainMember(Name)

IF i=-1 THEN
   PRINT("Function IsDomainMember failed")
ELSE
   IF i=1 THEN
      PRINT("Machine is member of Domain : "+Name)
   ELSE 
      IF i=2 THEN
         PRINT("Machine is member of Workstation : "+Name)
      ELSE
         PRINT("Machine is a standalone computer")
      END IF
   END IF
END IF