Jump to content
captainu99

Rename A Computer Script

Recommended Posts

Something so simple is giving me a huge headache.

I would like to create a script that renames a computer and then prompts the user to reboot/Reboots

Any Advice?

 

 

No matter what I select "Powershell" or "Powershell as admin"

Powershell Command - Rename-computer –newname “@NewComputerName@”

 

Error

Rename-computer : Fail to rename computer 'currentcomputername' to 'newcomputername' due to the following exception: Access is 
denied.
At line:1 char:1
+ Rename-computer -newname "WLK-WS-UM1"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (WIL-WS-UM1:String) [Rename-Computer], InvalidOperationException
    + FullyQualifiedErrorId : FailToRenameComputer,Microsoft.PowerShell.Commands.RenameComputerCommand

 

RenameComputerScript.PNG

Share this post


Link to post
Share on other sites

Its not labtech, its the machine.  Make sure you have creds that are correct, you could also attempt to rename with CMD

Share this post


Link to post
Share on other sites
1 minute ago, kspooner said:

Its not labtech, its the machine.  Make sure you have creds that are correct, you could also attempt to rename with CMD

I figured so.

Any insight into a command to use that won't require credentials?

Share this post


Link to post
Share on other sites

I have a working script, you need to use Shell (NOT AS ADMIN) so it runs with administrative rights on the computer, you will have to pass domain credentials to the command so that the computer can join to the domain successfully. I'll sanitize the script (if its not already) and then upload it here. You will only be able to use it with the versions of powershell that include the Rename-Computer. Alternatively you can search around on the forums here @DarrenWhite99 has a script that doesn't use Powershell and is very reliable.

Share this post


Link to post
Share on other sites

@MGreen Thank you. I will read through it. (BTW: A server cant work if its unplugged 🙂)

For now, my script works with powershell 3.0 "Rename-Computer" 

Whats the best way for me to check if a computer has powershell 3.0 installed?

Also what command do I need in order for Autoamte to show the correct computer name? I thought it was "Resend System Information" yet it still shows the old computer name in automate.

 

Edited by captainu99

Share this post


Link to post
Share on other sites

Very nice! Look at the downloads area of The Geek, I think @DarrenWhite99 posted a custom defined role for PowerShell versions? There are also plugins like @Cubert Powershell and .NET plugin that you can purchase which will maintain the versions for you and keep them up-to-date. You can also design your own; you'll have to create monitors or run scripts to extract the version from the Registry and download the update files and run them on a schedule. Keep in mind certain softwares are not always compatible with the latest versions of .NET so that gets a little tricky.

Nice job with the server ;)

 

Share this post


Link to post
Share on other sites

@captainu @MGreen do either of you have the updated .xml file anymore?  Sorry to reopen an old post but it looks like the files are no longer available for download.  I've spent a fair bit of time trying to create my own script, but I always get access denied.  I've tried various methods (rename_computer, WMIC, netdom) but all of them result in no love.

Share this post


Link to post
Share on other sites

@DarrenWhite99 @captainu99 @MGreen

 

Hey Guys, 

 

thanks for all the work above! I don't know if this is not deprecated, but I am getting an error on anything no matter how I run the script. Any thoughts? Has something changed since this was last worked on in 2018?

 

I've tried both variants above and get the same error. Any thoughts as I need to do it through powershell and not WMIC

Rename-Computer : Object reference not set to an instance of an object.
At line:1 char:1
+ Rename-Computer -newname 'Test-Name' -DomainCredential (New-Ob ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Rename-Computer], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.RenameComputerCommand

 

Share this post


Link to post
Share on other sites
On 9/2/2018 at 4:59 PM, captainu99 said:

Thank You. I cleaned it based on some of your script.

 

1) Prompt for new computer name when running the script

2) Runs script (Thanks @MGreen)

3) Emails tech and logs script results  @DarrenWhite99

Rename Computer Requires Powershell 3.0.xml

I found a minor error in this script.  When line 8 is called it has the variable check defined as @commandresults.  This causes the script to never reboot as the @commandresults variable doesn't exist so it never jumps to :Success.

Instead it needs to be commandresults on line 8 so that Automate automatically calls the variable as @commandresults@.  Then the script will correctly advance to :success and reboot the computer.

 

Edited by WConsulting

Share this post


Link to post
Share on other sites
On 9/10/2020 at 5:48 PM, WConsulting said:

I found a minor error in this script.  When line 8 is called it has the variable check defined as @commandresults.  This causes the script to never reboot as the @commandresults variable doesn't exist so it never jumps to :Success.

Instead it needs to be commandresults on line 8 so that Automate automatically calls the variable as @commandresults@.  Then the script will correctly advance to :success and reboot the computer.

 

Thank You. I uploaded the wrong version 🙂

Share this post


Link to post
Share on other sites

on a few computers I am getting het error

 

Rename-Computer : Object reference not set to an instance of an object.
At line:1 char:1
+ Rename-Computer -newname 'CC-***-****UCTION' -DomainCredential (New-O ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Rename-Computer], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.RenameComputerCommand

Part of the computer name is redacted- all letters


I checked that it is at least PS  v 3

Get-Host | Select-Object Version

Version      
-------      
5.1.18362.752

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...