I'm having trouble renaming a local computer via WMI.
I'm following the MSDN documentation (and not following it -- in an attempt to get SOME sort of result), but I can't seem to get the Win32_ComputerSystem.Rename() method to work.
Set colComputers = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objComputer in colComputers
If StrComp(objComputer.Name, Left(objNewComputerName, 15), vbTextCompare) <> 0 Then
errCode = objComputer.UnjoinDomainOrWorkgroup()
If errCode <> 0 Then
MsgBox "Error leaving domain/workgroup. Error: " & errCode
End If
errCode = objComputer.Rename(objNewComputerName)
If errCode <> 0 Then
MsgBox "Error changing computer name from '" & objComputer.Name & "' to '" & Left(objNewComputerName, 15) & "'. Error: " & errCode
Else
objComputer.JoinDomainOrWorkgroup domain, password, username
' Blindly reboot after attempting to join workgroup/domain
Set colOs = GetObject("winmgmts:{(Shutdown)}root\cimv2")
For Each objOs in colOs
objOs.Reboot()
Next
End If
End If
Next
Whenever I call Rename(), I get an error code 5 (access denied). So maybe I need to leave the domain first? When I call UnjoinDomainOrWorkgroup(), I also get an error code 5.
This is a vanilla Windows 7 installation, with only one account, which is of course, designated as an administrator. I cannot understand why this doesn't work.
Halp?