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:
- Save the script below in a folder and create a package containing the script.
- Change the domain name in the script to match your environment
- Add a “Run Command Line” step in the Task Sequence after the “Setup Windows and Configuration Manager” step.
- Add the following command in the “Run Command Line” step cscript.exe checkdomain.vbs
- 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
'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
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
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