Get Current or old Machine Name from WinPE VBScript

This script was designed to get the current or old computer name from WinPE and apply it to the Task Sequence Variable OSDComputerName.

It has been written in VBScript to not require PowerShell in the boot image.

Here is the script:

'====================================================
'Author: Richie Schuster - SCCMOG.com
'Date  : 24-03-2017
'Info  : Script finds old machine name for refresh Scenario and prepopulates it
'====================================================

Option Explicit
'On Error Resume Next

Dim oShell, env, objWMIService, colItems, objItem, strWinDrive, strCurrentComputer
Const strComputer = "."

Set oShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set env = CreateObject("Microsoft.SMS.TSEnvironment")

'End Header

' Worker

'Get Windows drive
Set colItems = objWMIService.ExecQuery ("Select * From Win32_LogicalDisk Where VolumeName = 'Windows'")
For Each objItem in colItems
	strWinDrive = objItem.DeviceID
Next

'Check Task Sequence found a Windows Drive
If IsNull(strWinDrive)  Or  IsEmpty(strWinDrive) Then
   MsgBox "The TaskSequence could not locate a previous Machine Name. Please PXE again and deploy this as a new machine!", 6, "No Previous Machine Name"
   WScript.Quit 1
Else
'Mount Registry from Windows Drive
oShell.Run "cmd.exe /c REG LOAD HKLM\TempHive " & strWinDrive & "\windows\system32\config\system",0,True
strCurrentComputer = oShell.RegRead("HKLM\TempHive\ControlSet001\Control\ComputerName\ComputerName\computername")
env("OSDComputerName") = strCurrentComputer
WScript.Quit 0
End If

Leave a Reply

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

Copyright 2016 SCCMOG | All Rights Reserved

css.php