Link to home
Start Free TrialLog in
Avatar of Irrylyn
Irrylyn

asked on

Script to name computer with user input and add to AD

Hi experts!

I have a zinger that will really show your skills!

I've found a lot of scripts online that can be used during a Bare Metal OS Deployment Task Sequence in SCCM that will prompt the user for the Computer Name.  I'm looking for something a little more robust.

Our Naming Convention for computers is <Location><Org><xWKx><4 numbers>

So for one Org (we'll call it Sales) that has 3 Workstations, they would be named:

LOCSALESXWKX0001
LOCSALESXWKX0002
LOCSALESXWKX0003
LOCSALESXWKX0004


> LOC is always the same.
> Org depends on the organization the machine belongs to.
> XWKX is always the same
> The 4 numbers are just the next available (Not already used) in Active Directory.

With me so far?  Ok, here's what I'm looking for:

I want a script to add to the Task sequence that will prompt the user for their Org only.

Then I want it to create the computer name by adding <LOC><User input for Org><xWKx><next available number in AD>

Oh, and once it has a name, can it add the computer to Active Directory?  (Ideally for this part, the OU it would be created in would also be dependant on the Org, but we can have an Imaging OU and just put them all in there to move them later)

Anything close to this would be a huge deal!

Thank you and I hope to see some ideas soon!
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, I should be able to help you out with this logic, but not until the new year.  I'm not sure how to integrate it into a Task Sequence, but I'm sure I could organise the VBScript to work outside of that, and perhaps you could work to integrate it in.

One question, when you say the ending four numbers are not to be otherwise used in AD, do you mean the number as a whole, regardless of the department, or is the same number, but different departments, allowed?

Regards,

Rob.
Avatar of Irrylyn
Irrylyn

ASKER

Same number allowed for different departments.

Can have

LOCSALESXWKX0001
LOCSALESXWKX0002
LOCSALESXWKX0003
LOCHRXWKX0001
LOCHRXWKX0002
LOCHRXWKX0003
etc

Thank you and enjoy your holidays
Hey, I was just thinking, since you're asking for the computer name, you're going to want to rename it before running the task sequence right?  A rename requires a reboot, so the script would need to be run outside of the task sequence I would think.

I wrote a script a while ago to do a similar thing, here (it's a very long thread, so scroll to the accepted answer):
https://www.experts-exchange.com/questions/23460761/Script-to-add-the-logged-in-machine-to-the-domain.html

Test that out first off, and we'll see if you need any modifications.

Regards,

Rob.
Also, from the follow-on post of that thread, you may want to change line 56 from this:
objShell.Run "shutdown -r -t 00", 1, False

to this
objShell.Run "wscript """ & WScript.ScriptFullName & """", 1, False

so that it renames and joins the domain without the reboot in between.

I just noticed though, this is "joining" the domain, rather than just "adding" the computer account....does that still work for you?

Rob.
Avatar of Irrylyn

ASKER

Sorry, was on vacay myself.

This will be used only for bare metal installations.  Naming them for the first time, so no reboot should be required.

All I need is for it to add the computer account to AD.  The Task Sequence within SCCM will join it to the domain during one of its tasks, after it installs the Operating System.

So the task sequence itself does the installation/configuration through a PXE boot.  I want to incorporate a script to automate the computer name creation from the user's input of which department they are in.  We have 10k+ computers.  Currently when departments get new machines, they have to bring them into the helpdesk to be imaged.  SCCM allows us to set up a sequence of tasks that will do this over the network so they don't have to bring them in.  Our only problem is that we need to automate the machine name and add it to AD so it can be joined to the domain when it's time for that task to run.

I hope you had refreshing holidays!
OK, so from what I know from the Task Sequence, it has a screen that allows the user to enter the full computer name that they want, and join the domain with that computer name.

If you want to use that GUI to do what you need, then I think we'll need to modify the HTA that is launched by the Task Sequence.  When I get back to work, I'll check out which HTA is run for that "enter computer name" part of the Task Sequence, and see if I can modify it.  If I can, then it would actually suit us as well, since we have a similar naming convention, so I could benefit from that too!

Rob.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Irrylyn

ASKER

This is better than what I was hoping for, thank you.  I definately think I can work with it.  I do hope this ends up benefiting you as well for all of your hard work.

I wont be able to start testing this for a few weeks, as my project was halted due to some certificate issues, but once that is hashed out I will put this to work.

Thanks again.
Avatar of Irrylyn

ASKER

Above and beyond.  Great solution and instructions.
OK great.  Please provide feedback here when you can so I know whether it worked out for you.

In my situation, I have modifed it so that it doesn't query AD (thus not requiring any credentials), but it determines whether the machine is a laptop or desktop, then it pulls the service tag (Dell machine) and prompts for the geographical location.  Then it appends that together like
<D or N><Service Tag><Location>

it works nicely.  Thanks for giving me the motivation to get that done!

Regards,

Rob.
Rob,

I came accross this thread and would like to clarify something from you ....

I feel like I can use your solution in order to name my computers during deployrmrnt ...

I have a naming scheme  "ABC-DELLService TAG"  eg. ATL-df674rs
ABC -- location code and I have about 50 locations
Dell service Tag - I am able to query it out.

How can I utilize your solution above?
Scripting is not my strength ..
I am using task sequencer at the momment.

Thank you
Petros
Hi Petros, I'll see if I can help you.  Back up your DeployWiz_Definition_ENU.xml and DeployWiz_Validation.vbs files.

In DeployWiz_Definition_ENU.xml, locate the section that begins with
      <Pane id="ComputerName">

and find its end point at the first occurrence of
      </Pane>

Replace that section with this code:
		<Condition><![CDATA[UCase(Property("SkipComputerName"))<>"YES" and Property("DeploymentType")<>"REPLACE" and Property("DeploymentType")<>"UPGRADE" and Property("DeploymentType")<>"CUSTOM" and Property("DeploymentType") <> "StateRestore"]]></Condition>
		<Initialization><![CDATA[InitializeComputerName]]> </Initialization>
		<Body>
		<![CDATA[

    <H1>Configure the computer name.</H1>
    <span style="width: 95%;">

        <p><span class="Larger">Enter the <u class=larger>l</u>ocation for this computer:</span>
		<select id="OSDComputerLocation" name=OSDComputerLocation size=1 language=vbscript onpropertychange=ValidateComputerLocation AccessKey=l>
			<option id="--select--" value="--select--"> -- Select -- </option>
			<option id="ABC" value="ABC">ABC</option>
			<option id="DEF" value="DEF">DEF</option>
			<option id="GHI" value="GHI">GHI</option>
		</select></p>

        <p><span class="Larger">Computer n<u class=larger>a</u>me:</span>
        <input type=text id="OSDComputerName" name=OSDComputerName size=15 language=vbscript onpropertychange=ValidateComputerName AccessKey=A /></p>

        <p>&nbsp;
          <label class=ErrMsg for=OSDComputerName>* Required (MISSING)</label>
          <label class=ErrMsg id=InvalidChar>Letters, Numbers & Dashes only!</label>
          <label class=ErrMsg id=TooLong>Maximum of 15 characters!</label>
        </p>

        <!--  Computer Description Format -->
        <!--

        <p>You can also give your computer a friendly description. Unlike the computer
        name, the computer description may contain spaces and other special characters. "Frank Kim's Test machine" and
        "E-Mail Server #23, Miami Regional Office, Rm. 2305." are examples of computer descriptions.</p>

        <p><span class="Larger">Computer <u class=larger>d</u>escription:</span><br/>
        <input type=text id="Description" name=ComputerDescription size=60 language=vbscript onpropertychange=ValidateComputerName AccessKey=d /></p>
        -->

    </span>
    
		]]>
		</Body>
		<Validation><![CDATA[ValidateComputerName]]></Validation>

Open in new window


changing the OSDComputerName options as you need to, to add your 50 locations.

In DeployWiz_Validation.vbs, add this custom function:
'''''''''''''''''''''''''''''''''''''
'  Validate Computer Location : CUSTOM FUNCTION
'

Function ValidateComputerLocation

	' Check Warnings
	ParseAllWarningLabels


	If OSDComputerLocation.value = "--select--" then
		InvalidChar.style.display = "none"
		TooLong.style.display = "none"
		OSDComputerName.value = ""
		ValidateComputerLocation = False
		ButtonNext.disabled = true
	Else
		Dim strComputer, objWMIService, colBIOS, objBIOS, strServiceTag, bIsLaptop, bIsDesktop, bIsServer, sAutoComputerName, colEnclosure, objInstance
		strComputer="."
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		Set colBIOS = objWMIService.ExecQuery("Select SerialNumber from Win32_Bios")
		For Each objBIOS In colBIOS
			strServiceTag = objBIOS.SerialNumber
		Next
		
		bIsLaptop = False
		bIsDesktop = False
		bIsServer = False
		sAutoComputerName = ""
		Set colEnclosure = objWMIService.ExecQuery("Select ChassisTypes from Win32_SystemEnclosure")
		For Each objInstance In colEnclosure
			If objInstance.ChassisTypes(0) = 12 Or objInstance.ChassisTypes(0) = 21 then
				' Ignore docking stations
			Else
				Select Case objInstance.ChassisTypes(0)
					Case "8", "9", "10", "11", "12", "14", "18", "21"
						bIsLaptop = true
					Case "3", "4", "5", "6", "7", "15", "16"
						bIsDesktop = true
					Case "23"
						bIsServer = true
					Case Else
						' Do nothing
				End Select
			End If
		Next
		
		'If bIsLaptop = True Then
		'	sAutoComputerName = "N" & OSDComputerLocation.value & strServiceTag
		'ElseIf bIsDesktop = True Then
		'	sAutoComputerName = "D" & OSDComputerLocation.value & strServiceTag
		'ElseIf bIsServer = True Then
		'	sAutoComputerName = "S" & OSDComputerLocation.value & strServiceTag
		'End If
		sAutoComputerName = OSDComputerLocation.value & "-" & strServiceTag
		OSDComputerName.value = sAutoComputerName

		oEnvironment.Item("OSDComputerName") = OSDComputerName.Value
		
		ValidateComputerLocation = True
		InvalidChar.style.display = "none"
		TooLong.style.display = "none"
	End If

End Function

Open in new window


and that should do it for the deployment HTA.  You will notice that the code above has the the chassis type in it, but that's not used for your version.

Regards,

Rob.
Rob,

Thank you very much for your quick response .. let me go through it quickly and I will get back to you ... I am sure I will have a follow up question or two...

Petros
Rob,

Ok .. just to give you a little background, this is an SCCM 2012 R2 - currently we deploy 2 images x32 and x64. I am attaching a task squencer as an example. Also Our boot image is MDT with Command prompt.
IN my ALL unknown device collection is where I have a variable "OSDComputerName" and it prompst me to enter teh computer name.
We are moving from Zenworks to SCCM and we still in testing mode.


I know where to put the first Script, (I am not sure if I need to edit anything else but I will find out ... I already made a copy of that file as a backup)

The HTA, I am not that familiar with.
deploy---unknown-computer-Captur.JPG
Deploy-task-seq-1Capture.JPG
Deploy-task-seq-2Capture.JPG
Deploy-task-seq-3Capture.JPG
So when it prompts you for the computer name, are you referring to the step for approving computers before the deployment begins to run? Or is it during a screen in the task sequence selection in WinPE?
I believe you are correct, it prompts me before the deployement begins to run.
It is a Task Seq Wiz .... Edit Task Sequence Variable ... Select the Task Variable to Edit...
Here is a screen shot
photo--5-.JPG
I only use MDT so I can't see the full features of SCCM you are using.  If you remove the OSDComputerName variable from the Unknown Computers properties, and run the Task Sequence, does it prompt you for a computer name?  From what I can tell, it may not, and just call a machine MININT-XXXX

That's OK, and you should then be able to use the approach here:
http://t3chn1ck.wordpress.com/2009/04/24/script-to-prompt-for-system-name-in-sccm-osd/

But instead of that script, you can use this to prompt for the prefix (of your location) and it will automatically add the dash and the service tag.

Rob.

		Dim strComputer, objWMIService, colBIOS, objBIOS, strServiceTag
		strComputer="."
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		Set colBIOS = objWMIService.ExecQuery("Select SerialNumber from Win32_Bios")
		For Each objBIOS In colBIOS
			strServiceTag = objBIOS.SerialNumber
		Next

Dim sNewComputerName, oTaskSequence, sTSMachineName, bPromptName
Set oTaskSequence = CreateObject ("Microsoft.SMS.TSEnvironment")
 
' Get the name the computer is set to receive and truncate to first 6 letters
sTSMachineName = lcase(oTaskSequence("_SMSTSMachineName"))
If left(sTSMachineName,6) = "minint" Then
   bPromptName = True
ElseIf sTSMachineName = "minwinpc" Then
   bPromptName = True
Else
   bPromptName = False
End If
 
' Note: The wscript.echo commands are logged in SMSTS.log for troubleshooting.  They are not displayed to the end user.
If bPromptName = True Then
   wscript.echo "Detected that the computer name is scheduled to receive a random value.  Prompting user to input a standard name."
   sNewComputerName = InputBox ("Please enter a location to continue." & vbCrLf & _
   "The name will be set to XXXX-" & strServiceTag & vbCrLf & _
   "where XXXX is the location you enter.", "Computer Location", , 30,30)
   oTaskSequence("OSDComputerName") = UCase(sNewComputerName & "-" & strServiceTag)
   wscript.echo "Set Task Sequence variable OSDComputerName to: " & sNewComputerName & "-" & strServiceTag
Else
   wscript.echo "Computer set to receive a standard name, continuing as is."
End If

Open in new window

Thank you so much Rob, I will test it shortly and report back.
Again, thank you for the help.
Petros
No problem. If you still have issues, could you open a new question and post its link here? That way, I'll take a look, but your also more likely to have SCCM experts have a look too.
Ok. I will do that. Also, should I keep the previous changes? The changes I made to the Deploywiz_validation files?
No, you wouldn't need either of those files changed, since we're not using that page of the task sequence with your requirements.
Rob,

Thank you that did it .... You are very helpfull and thank you for going out of your way to help me.
I am going to get few things done and hopefully revisit this process again and hopefully one day I will be able to accomplish the location selection from a script as well (a drop down selection of location code).

I will probably open a new question and I will provide you the link if you want to followup.

Again, Thank you for all your efforts

Petros
Excellent. Good to hear.

Rob.
Rob,

Will you look at the following link and  see if you have a suggestion.
https://www.experts-exchange.com/questions/28377074/Sccm-2012-Adding-shortcut-in-the-desktop-and-scripting.html

I have about 50 Apps to package and I have few web links with a shortcut placed in the desktop and the start menu.

In Zen works there are so many controls available that made the packaging a lot easier, So far , in SCCM, msi or Exe have been fine but simple scripts dealing with shortcuts and Icons etc.  has been a pain.(at least to me)

Thank you
Petros