2009-03-04

VBScript: Is computer in DOMAIN?

I had to make a check for domain membership in a VBScript today. Some users tried to run it on private computers, that did not belong to our domain. Source

My solution is based on ADSystemInfo and is quite simple:

' Function that checks for membership in domain
'
' Copyright (c) 2009 Rune Nordbøe Skillingstad
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; version 2 dated June, 1991.
'
' This program is distributed in the hope that it will be useful, but
' WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
' General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
' USA.
'
Option Explicit

On Error Resume Next

If isInDomain("DOMAIN") Then
WScript.Echo "YEAH!"
Else
WScript.Echo "NOPE!"
WScript.Quit 1
End If

Function isInDomain(ByVal strDomain)
On Error Resume Next
isInDomain = False

Dim objSysInfo

Set objSysInfo = CreateObject("ADSystemInfo")
If objSysInfo.DomainShortName = strDomain Then
isInDomain = True
End If

Set objSysInfo = Nothing
End Function

1 comment: