Honk
Honk

Reputation: 101

Renaming a computer name to the serial number

I am Trying to have my unattend.xml file make the hostname of the new image to be the Serial of the BIOS. I honestly don't care how or what language it's done in. Reading I have come this far but I am stuck.

$Serial = Get-WMIObject -Class "Win32_BIOS" | Select -Expand SerialNumber

Rename-Computer $Serial

I Get this error

Rename-Computer : Fail to rename computer 'test-B' to 'R9Z1EBP' due to the following exception: Access is denied.
At C:\Users\xiuhtecuhtli\Desktop\rename.ps1:2 char:1
+ Rename-Computer -NewName ($Serial)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (test-B:String) [Rename-Computer], InvalidOperationException
    + FullyQualifiedErrorId : FailToRenameComputer,Microsoft.PowerShell.Commands.RenameComputerCommand

Upvotes: 0

Views: 16062

Answers (4)

mr netlord
mr netlord

Reputation: 303

I love One-Liner (probably because i use it from an batch....)

powershell rename-computer (gwmi win32_bios).serialnumber

Of course it needs an Boot.

Upvotes: 1

PatricK
PatricK

Reputation: 6433

Since you tagged vbscript, here is a untested vbs solution. This allows you to rename computers remotely. You can modify it and have it execute after imaging is done or on first reboot.

Const AdminAcc = "Administrator"
Const AdminPwd = "Administrator Password"

Sub RenameComputerBySerial()
    RenameWithSerial "." ' Local Computer that runs the script
End Sub

Sub RenameWithSerial(sComputer)
    On Error Resume Next
    Dim oWMIService, colItems, oItem, sSerial

    '--[ Connect to computer ]--
    Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,authenticationLevel=pktPrivacy}!\\" & sComputer & "\root\cimv2")
    '--[ Extract Serial number from BIOS ]--
    Set colItems = oWMIService.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
    For Each oItem In colItems
        sSerial = Trim(oItem.SerialNumber)
    Next
    Set colItems = Nothing
    '--[ Rename Computer if Serial is non zero length ]--
    If Len(sSerial) > 0 Then
        Set colItems = oWMIService.ExecQuery("Select * from Win32_ComputerSystem")
        For Each oItem In colItems
            oItem.Rename sSerial, AdminPwd, AdminAcc
        Next
        Set colItems = Nothing
        '--[ Reboot the computer ]--
        Set colItems = oWMIService.ExecQuery("Select * from Win32_OperatingSystem")
        For Each oItem In colItems
            Select Case oItem.reboot
                Case 0: Wscript.Echo "Computer renamed to """ & sSerial & """ and rebooted"
                Case Else:  Wscript.Echo "Computer Renamed but not rebooted"
            End Select
        Next
        Set colItems = Nothing
    End If
    Set oWMIService = Nothing
    If Err.Number <> 0 Then
        Wscript.Echo "ERR(" & Err.Number & "):" & Err.Description
    End If
End Sub

Upvotes: 1

UsPeoples
UsPeoples

Reputation: 81

Since the original error stipulated that a serial number had been passed from the GWMI query to the restart-computer cmdlet, I suspect the command is being performed without the proper credentials. As such, you may try adding -localcredential with a known local machine administrative user/pass.

$Serial = (gwmi win32_bios).serialnumber
Rename-computer $Serial -localcredential localhost\administrator

You will be prompted for credentials. There are various other script examples if you prefer automating the passing of credentials to the cmdlet.

Upvotes: 0

TheMadTechnician
TheMadTechnician

Reputation: 36322

$Serial does not return anything until you assign it a value. You can perform a WMI call to get the serial number.

$Serial = Get-WMIObject -Class "Win32_BIOS" | Select -Expand SerialNumber

Then your Rename-Computer cmdlet should work fine (you do not need the parenthesis around $Serial, though it won't hurt anything to have them there). For that matter, the only thing you need to supply, if you are renaming the local computer, is the new name. So this should work:

Rename-Computer $Serial

Upvotes: 2

Related Questions