none
need script to assign hostname and join windows XP and windows 7 machine to domain via VBS

    Question

  • Dear all,

    i m new for scripting and I need  your expert help for assign hosname form server and join machine in domain via vbs.

    I have  database of hostaname which is mapped with machine serial no and i m looking for vbs script which will run as per below steps..

    1) get serial no from system bios

    2) check database whwre serial no is maped with hostname

    3)if serial no is same the get hostaname from database

    4) assign hostname to machine 

    5) restart the system

    6) after restarting script will add machine in to the domain.

    7) popup machine added in domain and restart.

    Wednesday, October 10, 2012 10:26 AM

Answers

  • Here is a VBScript I have used to join a computer to a domain using the JoinDomainOrWorkGroup method:

    ' JoinDomain.vbs
    ' VBScript program to join a computer to a domain.
    ' The computer account is created in Active Directory.
    ' The computer must have XP or above.
    ' The AD must be W2k3 or above.
    ' See c:\Windows\debug\NetSetup.log for details.

    Option Explicit

    Dim strDomain, strUser, strPassword, strOU
    Dim objNetwork, strComputer, objComputer, lngReturnValue

    Const JOIN_DOMAIN = 1
    Const ACCT_CREATE = 2
    Const ACCT_DELETE = 4
    Const WIN9X_UPGRADE = 16
    Const DOMAIN_JOIN_IF_JOINED = 32
    Const JOIN_UNSECURE = 64
    Const MACHINE_PASSWORD_PASSED = 128
    Const DEFERRED_SPN_SET = 256
    Const INSTALL_INVOCATION = 262144

    ' Prompt for credentials.
    strDomain = InputBox("Enter name of domain")
    strUser = InputBox("Enter account name to use")
    strPassword = InputBox("Enter the account password")

    ' Specify the OU where the computer object will be created.
    strOU = "ou=Computers,ou=West,dc=mydomain,dc=com"

    ' Retrieve NetBIOS name of local computer.
    Set objNetwork = CreateObject("WScript.Network")
    strComputer = objNetwork.ComputerName
     
    Set objComputer = GetObject("winmgmts:" _
        & "{impersonationLevel=Impersonate,authenticationLevel=Pkt}!\\" & _
        strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
            strComputer & "'")
     
    lngReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
        strPassword, strDomain & "\" & strUser, strOU, _
            JOIN_DOMAIN + ACCT_CREATE)

    Wscript.Echo "ReturnValue = " & CStr(lngReturnValue)

    Select Case lngReturnValue
        Case 0
            Wscript.Echo "Success joining computer to the domain!"
        Case 5
            Wscript.Echo "Access is denied"
        Case 87
            Wscript.Echo "The parameter is incorrect"
        Case 110
            Wscript.Echo "The system cannot open the specified object"
        Case 1323
            Wscript.Echo "Unable to update the password"
        Case 1326
            Wscript.Echo "Logon failure: unknown username or bad password"
        Case 1355
            Wscript.Echo "The specified domain either does not exist or could not be contacted"
        Case 2224
            Wscript.Echo "The account already exists"
        Case 2691
            Wscript.Echo "The machine is already joined to the domain"
        Case 2692
            Wscript.Echo "The machine is not currently joined to a domain"
        Case Else
            Wscript.Echo "Unknown error"
    End Select

    -----

    This must be run on the computer that you are joining, and you need to supply domain credentials with sufficient permissions.


    Richard Mueller - MVP Directory Services

    Wednesday, October 10, 2012 7:11 PM
    Moderator
  • This is a lot of work for a beginning scripter. :) Here are a couple of things to help you get started.

    - try checking out the TechNet script repository for some of the specific items you're interested in. You'll need to do some work combining scripts, but people are always willing to help solve particular problems you encounter.

    - Here's a snippet that demos the serial number retrieval:

    Set wmi = GetObject("winmgmts://./root/CIMV2")
    Set results = wmi.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
    for each result in results
    	serialNumber = result.SerialNumber
    next
    WScript.Echo serialNumber

    - here's another snippet that illustrates retrieving the current computer name and renaming it using an arbitrary name:

    Set wmi = GetObject("winmgmts://./root/CIMV2")
    newName = "SomeName"
    Set computers = wmi.InstancesOf("Win32_ComputerSystem")
    for each computer in computers
    	WScript.Echo computer.Name
    	computer.Rename(newName)
    next

    - domain join automation is a little more complicated but involves using the JoinDomainOrWorkGroup method in the Win32_ComputerSystem class.

    I suggest trying out a solution in pieces so you can see how it fits your situation; building a solution piecewise with script is much more rapid, reliable, and rewarding than trying to implement a working monolithic solution from scratch.


    Wednesday, October 10, 2012 2:35 PM

All replies

  • This is a lot of work for a beginning scripter. :) Here are a couple of things to help you get started.

    - try checking out the TechNet script repository for some of the specific items you're interested in. You'll need to do some work combining scripts, but people are always willing to help solve particular problems you encounter.

    - Here's a snippet that demos the serial number retrieval:

    Set wmi = GetObject("winmgmts://./root/CIMV2")
    Set results = wmi.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
    for each result in results
    	serialNumber = result.SerialNumber
    next
    WScript.Echo serialNumber

    - here's another snippet that illustrates retrieving the current computer name and renaming it using an arbitrary name:

    Set wmi = GetObject("winmgmts://./root/CIMV2")
    newName = "SomeName"
    Set computers = wmi.InstancesOf("Win32_ComputerSystem")
    for each computer in computers
    	WScript.Echo computer.Name
    	computer.Rename(newName)
    next

    - domain join automation is a little more complicated but involves using the JoinDomainOrWorkGroup method in the Win32_ComputerSystem class.

    I suggest trying out a solution in pieces so you can see how it fits your situation; building a solution piecewise with script is much more rapid, reliable, and rewarding than trying to implement a working monolithic solution from scratch.


    Wednesday, October 10, 2012 2:35 PM
  • Here is a VBScript I have used to join a computer to a domain using the JoinDomainOrWorkGroup method:

    ' JoinDomain.vbs
    ' VBScript program to join a computer to a domain.
    ' The computer account is created in Active Directory.
    ' The computer must have XP or above.
    ' The AD must be W2k3 or above.
    ' See c:\Windows\debug\NetSetup.log for details.

    Option Explicit

    Dim strDomain, strUser, strPassword, strOU
    Dim objNetwork, strComputer, objComputer, lngReturnValue

    Const JOIN_DOMAIN = 1
    Const ACCT_CREATE = 2
    Const ACCT_DELETE = 4
    Const WIN9X_UPGRADE = 16
    Const DOMAIN_JOIN_IF_JOINED = 32
    Const JOIN_UNSECURE = 64
    Const MACHINE_PASSWORD_PASSED = 128
    Const DEFERRED_SPN_SET = 256
    Const INSTALL_INVOCATION = 262144

    ' Prompt for credentials.
    strDomain = InputBox("Enter name of domain")
    strUser = InputBox("Enter account name to use")
    strPassword = InputBox("Enter the account password")

    ' Specify the OU where the computer object will be created.
    strOU = "ou=Computers,ou=West,dc=mydomain,dc=com"

    ' Retrieve NetBIOS name of local computer.
    Set objNetwork = CreateObject("WScript.Network")
    strComputer = objNetwork.ComputerName
     
    Set objComputer = GetObject("winmgmts:" _
        & "{impersonationLevel=Impersonate,authenticationLevel=Pkt}!\\" & _
        strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
            strComputer & "'")
     
    lngReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
        strPassword, strDomain & "\" & strUser, strOU, _
            JOIN_DOMAIN + ACCT_CREATE)

    Wscript.Echo "ReturnValue = " & CStr(lngReturnValue)

    Select Case lngReturnValue
        Case 0
            Wscript.Echo "Success joining computer to the domain!"
        Case 5
            Wscript.Echo "Access is denied"
        Case 87
            Wscript.Echo "The parameter is incorrect"
        Case 110
            Wscript.Echo "The system cannot open the specified object"
        Case 1323
            Wscript.Echo "Unable to update the password"
        Case 1326
            Wscript.Echo "Logon failure: unknown username or bad password"
        Case 1355
            Wscript.Echo "The specified domain either does not exist or could not be contacted"
        Case 2224
            Wscript.Echo "The account already exists"
        Case 2691
            Wscript.Echo "The machine is already joined to the domain"
        Case 2692
            Wscript.Echo "The machine is not currently joined to a domain"
        Case Else
            Wscript.Echo "Unknown error"
    End Select

    -----

    This must be run on the computer that you are joining, and you need to supply domain credentials with sufficient permissions.


    Richard Mueller - MVP Directory Services

    Wednesday, October 10, 2012 7:11 PM
    Moderator