I'm having a hard time figuring out how to rename a Windows 7 computer remotely. This is for automating Win 7 builds in a vSphere 5 environment, and I'm trying to get it as hands-off as I can. So far I've managed to get everything but the machine rename automated (or automatable).
WinRM is working, so remote powershell methods do work.
invoke-command -computername "W7-Img3-RPT49VA" -scriptblock {commands go here; have another one}
The above does work. I can do things like get directory listings and run commands. However, getting the domain changed hasn't worked. Per this SF question, I tried the following in the scriptblock:
$comp=get-wmiobject -class computersystem ; $comp.rename("W7-clone-42")
That returns ReturnValue : 5
which after much searching translates to "Access Denied". The other suggestion on that page:
wmic computersystem rename "W7-Clone-42"
Returns the unhelpful "Invalid Verb Switch". Another internet source suggests the following formulation:
wmic computersystem where Name="W7-Img3-RPT49VA" call rename name="W7-clone-42"
Which gives alternately Invalid Verb Switch, or invalid parameter. As a test, I ran the above command directly on my management station rather than via invoke-command
and also got access-denied.
Going old-school, I copied netdom to the target machine.
netdom renamecomputer W7-Img3-RPT49VA /newname:W7-clone-42
Which gives me 'access denied'.
Throughout this all, the credentials I've been testing with have Domain Admin. The intent is to crank the exact priv down once I've identified the workable methods. The Security event-log on the target machine definitely shows the successful logins throughout all of this.
The alternate method, handle the rename through the vSphere System Customization process, is still available. I even have an answer file for it, but I don't know how to prepare the template-machine to allow it to be used. Either way will get me what I need.
What am I missing? The wmic
syntax is clearly wrong, but the other two methods return 'access denied' so I have low hopes of it working once the correct syntax is worked out. Is this a UAC interaction problem?
I've always used the
netdom renamecomputer
method to do exactly what you are trying to do. On Windows Vista and 7, I encounter the same "access denied" error you do. You can work around this, though, by using theUserD
andPasswordD
switches:This will prompt you for your password, and then it will work without the "access denied" error. I imagine it has to do with the elevated token, but I am not sure.
You must run the WMIC command in an elevated prompt. Also, if there are dashes in either your current or new computer names, you will need to surround them with double quotes.
You can use PSEXEC to start an elevated command prompt and then pass it the WMIC command to change the computer name as follows:
psexec \\current-pcname -h -u domain\domainadminuser cmd /c wmic computersystem where name="current-pcname" [call] rename name="new-pcname"
The first thing that happens is that PSEXEC will ask for your domainadminuser's password -- you could use the -p switch for PSEXEC and type it in the command -- but this is safer imho. The verb 'call' is optional for this WMIC command. Finally, after WMIC and PSEXEC both return '0' codes, the PC will need to be rebooted -- I just use the SHUTDOWN command.
If you need to remotely rename a domained windows computer the Netdom Rename Computer GUI can do the job without any command line knowledge. It is especially helpful when renaming multiple widows computers. It also has the ability to rename computers by uploading a csv file.
Download Netdom Rename Computer GUI from Colosify