rename and reboot machine
-
Wednesday, February 29, 2012 6:34 AM
this is not quite working....
function renameAndReboot([string]$computer, [string]$newname)
{
$comp = gwmi win32_computersystem -computer $computer
$os = gwmi win32_operatingsystem -computer $computer
$comp.Rename($newname)
$os.Reboot()
}renameAndReboot oldmachine newmachine
I keep getting a return value of 5 and cannot get the machine renamed
All Replies
-
Wednesday, February 29, 2012 6:58 AM
win32_computersystem does not have a "Reboot" method. I quote from the literature:
Methods
The Win32_ComputerSystem class has these methods.
MethodDescription
JoinDomainOrWorkgroup
Adds a computer system to a domain or workgroup.
Rename
Renames a local computer.
SetPowerStateNot implemented. For more information about how to implement this method, see the SetPowerState method in CIM_UnitaryComputerSystem.
UnjoinDomainOrWorkgroupRemoves a computer system from a domain or workgroup.
Is your computer part of a domain?
Grant Ward, a.k.a. Bigteddy
What's new in Powershell 3.0 (Technet Wiki)
- Edited by BigteddyMicrosoft Community Contributor Wednesday, February 29, 2012 7:00 AM
-
Wednesday, February 29, 2012 7:06 AMthe problem is not with the reboot. the reboot works fine. it does not rename. I tried adding sleep 10 but still does not work
-
Wednesday, February 29, 2012 7:06 AMI have just tested the Rename method on one of my lab domain workstations, and it does indeed work. I got a result code of 0. Then I had to reboot using "shutdown /r"
Grant Ward, a.k.a. Bigteddy
-
Wednesday, February 29, 2012 7:17 AMdo i need to put in -authentication
-
Wednesday, February 29, 2012 7:22 AMYou need to be a domain admin if the pc is on a domain, or an Administrator if it is not.
Grant Ward, a.k.a. Bigteddy
-
Wednesday, February 29, 2012 7:24 AM
I don't understand how the reboot works:
PS C:\scripts> (gwmi -Class win32_computersystem).reboot()
Method invocation failed because [System.Management.ManagementObject#root\cimv2\Win32_ComputerSystem] doesn't contain a method named 'reboot'.
At line:1 char:42
+ (gwmi -Class win32_computersystem).reboot <<<< ()
+ CategoryInfo : InvalidOperation: (reboot:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFoundGrant Ward, a.k.a. Bigteddy
-
Wednesday, February 29, 2012 7:24 AM
how do i pass my username and password into the script?
I can use either local admin or a domain admin account to rename
-
Wednesday, February 29, 2012 7:26 AM
-
Wednesday, February 29, 2012 7:28 AM
You don't need to. The script will run under the credentials you are logged into Windows with. If you are running as another user, you can use the -credential parameter of Get-WMIObject.
gwmi -credential domain\username
Note, the rename will only work on the local computer. You can't rename another computer with this.
Grant Ward, a.k.a. Bigteddy
-
Wednesday, February 29, 2012 7:30 AMyou mean I cannot remotely run this script to rename a remote computer with this?
-
Wednesday, February 29, 2012 7:38 AM
I haven't tried it. I'm reading from the documentation of the WMI class on MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394102(v=vs.85).aspx#methods
The salient point is this:
Rename
Renames a local computer.
Why would they say local if they meant local or remote?
Grant Ward, a.k.a. Bigteddy
-
Wednesday, February 29, 2012 7:46 AM
Try this way:
Rename-Computer -ComputerName $Old_ComputerName -NewName $New_ComputerName
For reference: http://technet.microsoft.com/en-us/library/dd391890(v=ws.10).aspx
Hope this helps...!!!
Thanks & Regards
Bhavik Solanki
Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.
- Edited by Bhavik Solanki Wednesday, February 29, 2012 7:49 AM
-
Wednesday, February 29, 2012 7:59 AM
To Reboot Computer use following script
Restart-Computer -ComputerName $CompName
Hope this helps...!!!Thanks & Regards
Bhavik Solanki
Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you. -
Wednesday, February 29, 2012 8:30 AM
See today's Scripting Guy's blog:
Grant Ward, a.k.a. Bigteddy
-
Wednesday, February 29, 2012 6:35 PM
how do I rename the 2008 server from a remote computer and reboot?
according to this
http://theessentialexchange.com/blogs/michael/archive/2008/02/04/Rename-Reboot-with-Powershell.aspx
it says it can be done remotely, but I am getting the return 5
- Edited by JonDoe321 Wednesday, February 29, 2012 6:36 PM
-
Wednesday, February 29, 2012 6:43 PM
He doesn't say he tested it against a remote computer. What you are referring to is a very brief blog by someone. It is not authoritative. He may have assumed that it would work against remote computers because gwmi has a -computername parameter.
But the Microsoft documentation says otherwise.
Grant Ward, a.k.a. Bigteddy
-
Wednesday, February 29, 2012 7:58 PMim missing the rename-computer cmdlet. where can i download this?
-
Wednesday, February 29, 2012 8:08 PM
It is new to Powershell Version 3 (CTP).
http://www.microsoft.com/download/en/details.aspx?id=27548
(That's one for my Wiki!)
Grant Ward, a.k.a. Bigteddy
- Marked As Answer by Yan Li_Microsoft Contingent Staff, Moderator Monday, March 05, 2012 4:48 AM
-
Wednesday, February 29, 2012 9:15 PMyes this works quite nicely. so there isnt a way to use gwmi to work remotely by changing the name?
-
Thursday, March 01, 2012 6:10 AMUse "netdom renamcomputer" with psexec.
Grant Ward, a.k.a. Bigteddy
-
Thursday, March 01, 2012 6:11 AM
Well...I search on web to get more detail arround it and most of the case I find the same implementation what you did. I am not sure why its not working. I suggest, upgrade to PowerShell 3.0 if possible to have better implementation in place.
Thanks & Regards
Bhavik Solanki
Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.- Marked As Answer by Yan Li_Microsoft Contingent Staff, Moderator Monday, March 05, 2012 4:48 AM

