Sign in to follow this  
Followers 0
PsaltyDS

_GetWorkgroup and _GetWorkgroupList

9 posts in this topic

#1 ·  Posted (edited)

For the sake of automated management for classroom computers in a workgroup (not a Windows domain), I created a couple of functions to get the workgroup name, list workgroups, and list computers in a workgroup.

_GetWorkgroup() takes no arguments and returns a string of the workgroup name. Failure returns "" and sets @Error = 1.

_GetWorkgroupList() takes one optional string argument and returns a 1D array containing a list of computer names in the local workgroup, or a list of computers in a named workgroup, or a list of workgroups.

This is the test/demo script:

; Test _GetWorkgroup() and _GetWorkgroupList() UDFs
#include <_GetWorkgroup.au3>
#include <_GetWorkgroupList.au3>

$MyWkGrp = _GetWorkgroup ()
$WkGrpLst = _GetWorkgroupList ()
_ArrayDisplay($WkGrpLst, "Listing of " & $MyWkGrp)

$TstGrp = "Workgroup"
$WkGrpLst = _GetWorkgroupList ($TstGrp)
_ArrayDisplay($WkGrpLst, "Listing of " & $TstGrp)

$WkGrpLst = _GetWorkgroupList ("*")
_ArrayDisplay($WkGrpLst, "Listing of all workgroups")

This is _GetWorkgroup():

; ================================================================
; User Defined Function (UDF) _GetWorkgroup
; By PsaltyDS  --  at the AutoIT3 forums www.autoitscript.com/forum/
; Version 1.0 - 02Feb06
; ================================================================
; Syntax:  _GetWorkgroup()
;
; Parameters: None
;
; Retruns:
;    On Success, returns the string value of the computer's current NetBIOS Workgroup
;    On Failure, returns empty string ("") and @Error = 1
; ================================================================
#include-once
#include <array.au3>
#include <file.au3>

Func _GetWorkgroup()
    
    Dim $GetWorkgroupArray[1]
    
    $ExtCommand = " /c nbtstat -n > " & @TempDir & "\NetBiosStat.txt 2>&1"
    FileDelete(@TempDir & "\NetBiosStat.txt")
    
    $ExitCode = RunWait(@ComSpec & $ExtCommand, @TempDir, @SW_HIDE)
    If $ExitCode <> 0 Then
        SetError(1)
        Return ""
    EndIf
    
    If Not _FileReadToArray(@TempDir & "\NetBiosStat.txt", $GetWorkgroupArray) Then
        SetError(1)
        Return ""
    EndIf
    
    If $GetWorkgroupArray[0] >= 6 Then
        If Not StringInStr($GetWorkgroupArray[5], "NetBIOS Local Name Table") Then
            SetError(1)
            Return ""
        EndIf
    Else
        SetError(1)
        Return ""
    EndIf
    
    For $r = 1 To $GetWorkgroupArray[0]
        If StringInStr($GetWorkgroupArray[$r], "<00>  GROUP") Then
            $SplitStringArray = StringSplit(StringStripWS($GetWorkgroupArray[$r], 4 + 1), " ")
            $Workgroup = $SplitStringArray[1]
            ExitLoop
        EndIf
    Next
    
    Return $Workgroup
    
EndFunc;==>_GetWorkgroup

This is _GetWorkgroupList():

; ================================================================
; User Defined Function (UDF) _GetWorkgroupList()
; By PsaltyDS  --  at the AutoIT3 forums www.autoitscript.com/forum/
; Version 1.0 - 02Feb06
; Written for AutoIT 3.1.1 Beta 106 or later
; ================================================================
; Syntax:  _GetWorkgroupList([$sWorkgroup])
;
; Parameters:  [Optional] $sWorkgroup = String name of workgroup from which to list computers
;                                      If $sWorkgroup = "" then list computers in workgroup of local computer
;                                      If $sWorkgroup = "*" then list the names of workgroups
;
; Retruns:
;    On Success:
;         If $sWorkgroup = "", returns a 1D array listing the member computers of the local NetBIOS workgroup with [0] = Count
;         If $sWorkgroup = a workgroup name, returns a 1D array listing the member computers in the named workgroup
;         If $sWorkgroup = "*", returns a 1D array of workgroup names in the NetBIOS address space of the local computer
;
;    On Failure, returns an array with a single element [0] = 0, and @Error = 1
; ================================================================
#include-once
#include <array.au3>
#include <file.au3>

Func _GetWorkgroupList($Workgroup = "")
    
    Dim $WorkgroupArray[1]
    Dim $ComputerList[1]
    $ComputerList[0] = 0
    
    If $Workgroup = "" Then
        $ExtCommand = " /c net view > " & @TempDir & "\WorkgroupList.txt 2>&1"
    Else
        If IsString($Workgroup) Then
            If $Workgroup = "*" Then
                $ExtCommand = " /c net view /domain > " & @TempDir & "\WorkgroupList.txt 2>&1"
            Else
                $ExtCommand = " /c net view /domain:" & $Workgroup & " > " & @TempDir & "\WorkgroupList.txt 2>&1"
            EndIf
        Else
            SetError(1)
            Return $ComputerList
        EndIf
    EndIf
    
    FileDelete(@TempDir & "\WorkgroupList.txt")
    
    $ExitCode = RunWait(@ComSpec & $ExtCommand, @TempDir, @SW_HIDE)
    If $ExitCode <> 0 Then
        SetError(1)
        Return $ComputerList
    EndIf
    
    If Not _FileReadToArray(@TempDir & "\WorkgroupList.txt", $WorkgroupArray) Then
        SetError(1)
        Return $ComputerList
    EndIf
    
    If $Workgroup = "*" Then
        If Not StringLeft($WorkgroupArray[1], 6) = "Domain" Then
            SetError(1)
            Return $ComputerList
        EndIf
    Else
        If Not StringLeft($WorkgroupArray[1], 11) = "Server Name" Then
            SetError(1)
            Return $ComputerList
        EndIf
    EndIf
    
    If $Workgroup = "*" Then
        For $r = 4 To $WorkgroupArray[0]
            If $WorkgroupArray[$r] = "" Or StringLeft($WorkgroupArray[$r], 34) = "The command completed successfully" Then
                ContinueLoop
            Else
                _ArrayAdd($ComputerList, $WorkgroupArray[$r])
            EndIf
        Next
    Else
        For $r = 4 To $WorkgroupArray[0]
            If StringLeft($WorkgroupArray[$r], 2) = "\\" Then
                $WorkgroupArray[$r] = StringReplace($WorkgroupArray[$r], "\\", "")
                $SplitStringArray = StringSplit(StringStripWS($WorkgroupArray[$r], 4 + 1), " ")
                _ArrayAdd($ComputerList, $SplitStringArray[1])
            EndIf
        Next
    EndIf
    
    $ComputerList[0] = UBound($ComputerList) - 1
    If $ComputerList[0] = 0 Then SetError(1)
    Return $ComputerList
    
EndFunc;==>_GetWorkgroupList

Have fun! ;)

Criticism (constructive) always welcome... :lmao:

Edit: Tweak to keep _GetWorkgroupList() from listing one trash line at the bottom of the list of workgroups.

Edit: Tweak to remove literal reference to C:\Temp\

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites



one thing, in my script i had used net view /domain to retrieve the workgroup name, whats would be the comparison between nbtstat and net view domain , any opinions?

Wow... haven't thought about this post in ages!

NET VIEW /DOMAIN will list ALL the workgroups (and domains) visible to NetBIOS. So it gets _YOUR_ workgroup only if that's the only one.

The nbtstat method checks the actual NetBIOS name record for the computer.

Hope that helps.

^_^


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I use this:

$ComputerDomain = _DomainComputerBelongs()

Func _DomainComputerBelongs($strComputer = "localhost")

$Domain = ''

$wbemFlagReturnImmediately = 0x10

$wbemFlagForwardOnly = 0x20

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

If Not IsObj($objWMIService) Then Return SetError(1, 0, '')

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) Then

For $objItem In $colItems

$Domain = $objItem.Domain

Next

EndIf

Return $Domain

EndFunc ;==>_DomainComputerBelongs

It returns my workgroup fine everytime...

Edited by AoRaToS

s!mpL3 LAN Messenger

Current version 2.9.8.8 [30/09/2015]

Download Here

s!mpL3

Share this post


Link to post
Share on other sites

I use this:

$ComputerDomain = _DomainComputerBelongs()

Func _DomainComputerBelongs($strComputer = "localhost")
     $Domain = ''
     $wbemFlagReturnImmediately = 0x10
     $wbemFlagForwardOnly = 0x20
     $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
     If Not IsObj($objWMIService) Then Return SetError(1, 0, '')
     $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
     If IsObj($colItems) Then
          For $objItem In $colItems
               $Domain = $objItem.Domain
          Next
     EndIf
     Return $Domain
EndFunc  ;==>_DomainComputerBelongs

It returns my workgroup fine everytime...

My AutoIt-Fu wasn't strong enough for WMI calls three years ago, but this kind of thing would be my preferred method now.

P.S. If you put code tags around your script the indentation will be preserved, making it easier to read.

^_^


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

hmm, one thing is i have a complete code of listing computers in the neighbourhood / listing current workgroup etc. but , in case u dont have WMI running (of course, a rare case though) it wont work, so i am trying to have handy all three -

  • WMI based {requires WMI service to be running}
  • CMD (net view / nbtstat ) based {This should not write files to local disk which might create admin rights issues...}
  • dll calls based {1. if the list is too big for buffer, it would be an issue, and DLL errors when failed, are not very comprehendable at times}

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

one big update, NET VIEW FAILS IF FILE & PRINTER SHARING IS BLOCKED BY WINDOWS FIREWALL.

i got this error message:

System error 6118 has occurred. The list of servers for this workgroup is not currently available

refer KB 298804 nbtstat would still work.

PS: <Edit> Net View /domain:<WorkgroupName> still works, as long as the domain / workgroup contains PCs that have file & printer sharing enabled!!!!!

Edited by rajeshontheweb

Share this post


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
Sign in to follow this  
Followers 0