Thursday, December 29, 2011

Find a Machine on which Domain(WorkGroup)

Dim sName
Dim logFile

'
' Logger
'
Sub Logger
Set fso = CreateObject("Scripting.FileSystemObject")

Set logFile = fso.OpenTextFile( "C:\ComputerList.txt", 8, True )
End Sub


'
' Get all the NetBIOS namespaces
'
Sub PullAllDomains

Dim objNameSpace
Dim AllNameSpaces
Dim Domain
Dim c

c = 0
Set objNameSpace = GetObject("WinNT:")
For Each Domain in objNamespace
AllNameSpaces = AllNameSpaces & VBCRLF & Domain.Name
c = c + 1
if Instr(sName, Domain.Name) <> 0 then
logFile.writeline Domain.Name & " matches"
else
GetComputerList Domain.Name
End if
Next
logFile.writeline "Namespaces :" & c
logFile.writeline AllNameSpaces

End sub

'
' When passed a namespace returns all computers present in it.
'
sub GetComputerList( ContainerName )

Dim Container
Dim Computer
Dim sTemp
Dim filter
Dim counter
filter = "Computer"
counter = 0
Set Container = GetObject("WinNT://" & ContainerName)
Container.Filter = Array(filter)

For Each Computer In Container
sTemp = sTemp & Computer.Name & VBCRLF
counter = counter + 1
Next

logFile.writeline VBCRLF
logFile.writeline "Machines in " & ContainerName & " : " & counter
logFile.writeline sTemp

End Sub

'
' Finds the current workgroup/domain the local machine is present using WMI.
'
Sub GetLocalDomain

sNode = "."
Set oWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& sNode & "\root\cimv2")
Set colComputer = oWMI.ExecQuery _
("Select DomainRole, Domain from Win32_ComputerSystem")
For Each oComputer in colComputer
iDR = oComputer.DomainRole
sName = oComputer.Domain
Next

If iDR = 0 Or iDR = 2 Then
logFile.writeline "Computer is in workgroup " & sName
Else
logFile.writeline "Computer is in domain " & sName
End If

GetComputerList sName

End Sub

Logger
logFile.writeline Date()&":"&Time()&":" & " Starting discovery ..."
GetLocalDomain
PullAllDomains
logFile.writeline Date()&":"&Time()&":" & " End of discovery ..."

' Close the log file.

logFile.Close

No comments:

Post a Comment

Disqus for yogi's talk

comments powered by Disqus