Menu
CCMEXEC.COM – Enterprise Mobility
  • Home
  • General
  • Configuration Manager
  • Windows 10
  • Windows 11
  • Intune
  • GitHub
  • About
CCMEXEC.COM – Enterprise Mobility

Verify domain join is successful during OS Deployment

Posted on April 7, 2013 by Jörgen Nilsson

There was a question asked on the forums a couple of days ago about how to verify during the OS Deployment Task Sequence, if the computer failed to join the Active Directory domain or not. I wrote together this little script which will check what domain the computer belongs to and if it isn’t the correct domain name the script will return exit code 1 and the task sequence will fail.

The script must be run after the Setup Windows and Configuration Manager step in the task sequence as the computer will restart after that step and configure the Operating System and join the domain.

To implement it:

  1. Save the script below in a folder and create a package containing the script.
  2. Change the domain name in the script to match your environment
  3. Add a “Run Command Line” step in the Task Sequence after the “Setup Windows and Configuration Manager” step.
  4. Add the following command in the “Run Command Line” step cscript.exe checkdomain.vbs
    Checkdomain
  5. Select the newly created package as then package that the command line should be executed from.

Then you are good to go!

Vbscript Checkdomain.vbs:

'Sample script for checking the domain name during OS Deployment

'https://ccmexec.com
'Kept the wscript.echo lines so it is possible to test it
DomainName = "WORKGROUP"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colSettings = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings
If objComputer.Domain = DomainName then
' Wscript.Echo "Domain: " & objComputer.Domain
else
' Wscript.echo "Domain not correct: " & objComputer.Domain
wscript.quit(1)
end if
Next

'Sample script for checking the domain name during OS Deployment

'https://ccmexec.com

'Kept the wscript.echo lines so it is possible to test it

DomainName = "WORKGROUP"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colSettings = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")

For Each objComputer in colSettings

If objComputer.Domain = DomainName then

' Wscript.Echo "Domain: " & objComputer.Domain

else

' Wscript.echo "Domain not correct: " & objComputer.Domain

wscript.quit(1)

end if

Next

2 thoughts on “Verify domain join is successful during OS Deployment”

  1. Mats says:
    April 16, 2013 at 8:09 pm

    Got an error the first time so this might be double.

    Quick edit to remove the hardcoded domain name. Now accepts the domain name as command line argument instead

    ‘Sample script for checking the domain name during OS Deployment

    ‘https://ccmexec.com

    ‘Kept the wscript.echo lines so it is possible to test it

    ‘ quick edit to remove hardcoded domain name. Now takes the domain name as argument on the commandline
    ‘ example checkdomain.vbs mydomain.com

    Set objArgs = Wscript.Arguments

    If objArgs.Count1 Then
    wscript.quit(1)
    else
    DomainName = objArgs(0)
    Set objWMIService = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2”)
    Set colSettings = objWMIService.ExecQuery(“Select * from Win32_ComputerSystem”)
    For Each objComputer in colSettings
    If objComputer.Domain = DomainName then
    ‘ Wscript.Echo “Domain: ” & objComputer.Domain
    else
    ‘ Wscript.echo “Domain not correct: ” & objComputer.Domain
    wscript.quit(1)
    end if
    Next
    End if

    Reply
  2. Jo says:
    April 29, 2013 at 5:21 pm

    Hi,

    I did a small modification to the edit by Mats. I modified to compare to a string compare to prevent case sensitive.

    ‘https://ccmexec.com

    ‘Kept the wscript.echo lines so it is possible to test it

    ‘ quick edit to remove hardcoded domain name. Now takes the domain name as argument on the commandline
    ‘ example checkdomain.vbs mydomain.com
    ‘ edit : added the compare without case sensitive

    Set objArgs = Wscript.Arguments

    If objArgs.Count=1 Then
    DomainName = objArgs(0)
    Set objWMIService = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2”)
    Set colSettings = objWMIService.ExecQuery(“Select * from Win32_ComputerSystem”)

    For Each objComputer in colSettings

    ‘Compare line to Computer’s Domain to argument without case sensitive
    Compare=strComp(objComputer.Domain,DomainName,1)

    If Compare=0 then
    ‘Wscript.Echo “Domain: ” & objComputer.Domain
    else
    ‘Wscript.echo “Domain not correct: ” & objComputer.Domain
    wscript.quit(1)
    end if
    Next

    else
    wscript.quit(1)
    End If

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

My name is Jörgen Nilsson and I work as a Senior Consultant at Onevinn in Malmö, Sweden. This is my blog where I will share tips and stuff for my own and everyone elses use on Enterprise Mobility and Windows related topics.
All code is provided "AS-IS" with no warranties.

Recent Posts

  • Tip when troubleshooting unexpected reboots during Autopilot – event ID 2800
  • New settings in Intune Security Baseline Windows 11 24H2 -2504
  • Managing extensions in Visual Studio Code
  • Reinstall a required Win32app using remediation on demand
  • Administrator protection in Windows 11 – First look
©2025 CCMEXEC.COM – Enterprise Mobility | WordPress Theme by Superb Themes
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT