Jump to content

Detect if it the pc belongs to a domain or Workgroup


Recommended Posts

i got this when i searched for the title but the solution didnt make me happy enough. this script give more precise results.

VBScript Code:
Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" )
Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
For Each objItem in colItems
    strComputerDomain = objItem.Domain
    If objItem.PartOfDomain Then
        WScript.Echo "Computer Domain: " & strComputerDomain
    Else
        WScript.Echo "Workgroup: " & strComputerDomain
    End If
Next

thanks to http://www.robvanderwoude.com/vbstech_netw...ames_domain.php as it is just wmi scripting it should be pretty easy to get it in au3.if i did prepare a UDF for this, i will post it here..

Link to post
Share on other sites
  • 11 years later...

I was looking for this code.

I translated it into AutoIt for posterity:

 

Dim $objWMISvc = ObjGet("winmgmts:\\.\root\cimv2")
Dim $colItems = $objWMISvc.ExecQuery("Select * from Win32_ComputerSystem", "WQL", 48)

If IsObj($colItems) Then
    For $objItem In $colItems
        If $objItem.PartOfDomain Then
            Msgbox(0, @ScriptName, "Computer Domain: " & $objItem.Domain)
        Else
            Msgbox(0, @ScriptName, "Workgroup: " & $objItem.Domain)
        EndIf
    Next
EndIf

 

Link to post
Share on other sites
  • 1 year later...

I modified the script from aVen9er by simplifying a little the main section, building it into a function, and gave it a couple more return options:

 

; ================================================================================


; --- Examples

$sDomainType=_detectDomainWorkgroup()         ; --- Return string = "Domain"  or "Workgroup"
    Msgbox(0, "Detect Domain Result 1", $sDomainType)

$sDomainType=_detectDomainWorkgroup(1)         ; --- Return string = "ComputerDomain:{name of the domain}"  or "Workgroup:{name of Workgroup}"
    Msgbox(0, "Detect Domain Result 2", $sDomainType)

$sDomainType=_detectDomainWorkgroup(2)         ; --- Return string = "{Domain name}"  or "{Workgroup name}"
    Msgbox(0, "Detect Domain Result 3", $sDomainType)


Exit
; ================================================================================
; ------------- User Defined Functions -------------------------------------------
; ================================================================================

Func _detectDomainWorkgroup($iRetrunStr=0) ; --- Detects if current computer is domain-joined or on a workgroup
    ; --- Return depends on $iReturnStr:
    ;     $iRetrunStr=0 (default) a string = "Domain"  or "Workgroup"
    ;      $iRetrunStr=1 A string = "ComputerDomain:{name of the domain}"  or "Workgroup:{name of Workgroup}"
    ;     $iRetrunStr=2 A string = "{Domain name}"  or "{Workgroup name}"
    ; -----------------------------------------------------------------------------------------------------

Dim $objWMISvc = ObjGet("winmgmts:\\.\root\cimv2")
Dim $colItems = $objWMISvc.ExecQuery("Select * from Win32_ComputerSystem", "WQL", 48)
Dim $sDomainType="Workgroup"

    If IsObj($colItems) Then
        For $objItem In $colItems
            If $objItem.PartOfDomain Then $sDomainType="Computer Domain"
        Next
    EndIf

; ---- Return Options
    Select
        Case $iRetrunStr=1
            Return $sDomainType & ":" & $objItem.Domain    ; --- Return {Domain or Workgroup tag}:{name of domain or workgroup}
        Case $iRetrunStr=2
            Return $objItem.Domain                            ; --- Name of the domain or workgroup alone without an identifying tag label
        Case Else
            Return $sDomainType                                ; --- (Default choice) Return string: "Domain" or "Workgroup"
    EndSelect


EndFunc
; ================================================================================

Link to post
Share on other sites
Looks like internal @LogonDomain and@LogonServer system macros don't do the job for you. You need @LogonWorkGroup too? Wonder if the functionality behind the extraction of those system macros is faulty, or looking at different things?
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...