Read Registry while machine not running using winpe
December 9th, 2008While I was developing a winpe 2.0 cd which automatically boots up a hta that creates a full backup of a machine using imagex, i wanted to give the image file a unique name.
Like MyComputerName_UserName.wim
If you query WMI the only unique thing you get back is the computers serialnumber. Which in most cases doesn’t say much, or is hard to assign to someone.
Instead of using the machines serial number i gave it a shoot finding out the machines hostname and the last logged on user.
The trick is to load the registry file hiv first, do your query and then unload the hiv.
You’ll find the hive files in C:\Windows\system32\config\
Here some vbscript examples how I did it.
'Declaration
Const HKEY_LOCAL_MACHINE=&H80000002
MsgBox GetComputerName
MsgBox GetUserName
'Set AutoLogon on the Offline Machine ![]()
SetAutoLogon User, Password, Domain, Count
Function GetComputerName()
Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.run "reg load HKLM\TempHive C:\WINDOWS\system32\config\system", 0, True
tempComputerName = RegRead(HKEY_LOCAL_MACHINE,"TempHive\ControlSet001\Control\ComputerName\ComputerName","ComputerName")
If isNull(tempComputerName) Then
GetComputerName = "Coudn't load local hive"
Else
GetComputerName = tempComputerName
End If
WSHShell.run "reg unload HKLM\TempHive", 0, True
Set WSHShell = Nothing
End Function
Function GetUserName()
Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.run "reg load HKLM\TempHive C:\WINDOWS\system32\config\software", 0, True
tempUserName = RegRead(HKEY_LOCAL_MACHINE,"TempHive\Microsoft\Windows NT\CurrentVersion\Winlogon","DefaultUserName")
If isNull(tempComputerName) Then
GetUserName = "Coudn't load local hive"
Else
GetUserName = tempUserName
End If
WSHShell.run "reg unload HKLM\TempHive", 0, True
Set WSHShell = Nothing
End Function
Sub SetAutoLogon(User, Password, Domain)
Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.run "reg load HKLM\TempHive C:\WINDOWS\system32\config\software", 0, True
RegWrite HKEY_LOCAL_MACHINE,"TempHive\Microsoft\Windows NT\CurrentVersion\Winlogon","DefaultUserName", User
RegWrite HKEY_LOCAL_MACHINE,"TempHive\Microsoft\Windows NT\CurrentVersion\Winlogon","DefaultPassword", Password
RegWrite HKEY_LOCAL_MACHINE,"TempHive\Microsoft\Windows NT\CurrentVersion\Winlogon","DefaultDomainName", Domain
RegWrite HKEY_LOCAL_MACHINE,"TempHive\Microsoft\Windows NT\CurrentVersion\Winlogon","AutoAdminLogon", "1"
RegWriteDword HKEY_LOCAL_MACHINE,"TempHive\Microsoft\Windows NT\CurrentVersion\Winlogon","AutoLogonCount", Count
'If isNull(tempComputerName) Then
' SetAutoLogon = "Coudn't load local hive"
'Else
' SetAutoLogon = tempUserName
'End If
WSHShell.run "reg unload HKLM\TempHive", 0, True
Set WSHShell = Nothing
End Sub
Function RegRead(strRootKey, strKeyPath, strValueName)
RegRead = ""
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
oReg.GetStringValue strRootKey, strKeyPath, strValueName, value
RegRead = value
End Function
Sub RegWrite(strRootKey, strKeyPath, strValueName, value)
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
oReg.SetStringValue strRootKey, strKeyPath, strValueName, value
End Sub
Sub RegWriteDword(strRootKey, strKeyPath, strValueName, value)
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
oReg.SetDwordValue strRootKey, strKeyPath, strValueName, value
End Sub
