Even Easier (and automated) Computer Naming

Following on from my post on Friday about naming machines automatically, I wanted to share another bit of PowerShell which is arguably easier to use and easier to implement. If you are not using MDT it is also a great solution.

If you are using ConfigMgr 2012 R2, then you can use the Run PowerShell Script task sequence step to execute this script, I just call it Set-OSDComputerName.ps1, put it into a package, send it out to your distribution points and then you are done. I love using MDT and I love the way you can automate things like running user exit scripts but this is just easier and quicker to implement. It means I can put it with my standard package of PowerShell scripts that I use in my OS deployment engagements and off I go.

Here is the script.

$MacAddress = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IpEnabled = true" | Select-Object MacAddress).MacAddress
$Escaped = $MacAddress.Replace(":", "")
$OSDComputerName = $Escaped.SubString($Escaped.Length - 8,8)
$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$TSEnv.Value("OSDComputerName") = "$OSDComputerName"

What I like about this short script is that I probably have multiple network cards in my device but this will only pick up the one which is IP enabled. It’s easy to run, and quick to setup.

Advertisements

Tags: , , ,

About Martyn

Martyn is one of the Senior Cloud Architects and DevOps Team Leader at one of the worlds leading Cloud Transformation Specialists Inframon. Martyn is responsible for the architecture of some of the largest Azure deployments in EMEA and is a advisor to a many businesses on their strategies. Martyn is a regular speaker at Microsoft events and community events on Azure and DevOps, giving his insight to a growing number of audiences.

One response to “Even Easier (and automated) Computer Naming”

  1. Jim says :

    Hi Martyn,
    I’m testing SCCM 2012 R2 run powershell script command in OSD but haven’t had much luck. I created a package with and without program, set the executionpolicy to Bypass and did not set any parameters.
    Powershell script is placed between Apply Operating System and Apply Windows Settings. Nothing applied.
    The script is suppost to query the database and rename the computer before joining it to domain. Script runs successfully if I run it manually from a share.
    Did I miss something? Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: