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?

link|improve this question
feedback

1 Answer

up vote 2 down vote accepted

Have you tried disabling UAC during testing? I'm pretty sure thats why you're getting the Access Denied messages

link|improve this answer
Josh, that seems to have worked, but now I need to "programatically" turn UAC back on before I give this system to the end user. Thoughts? – oo. Dec 2 '09 at 4:53
On second thought, I just need to redesign my provisioning process to somehow kick off a batch of tasks on first boot. Thanks again. – oo. Dec 2 '09 at 4:56
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.