Powershell Tool in Inventory - Rename domain joined computer

Comments

10 comments

  • sean johnson
    get-item cert:\LocalMachine\My\* | remove-item
    $SN = gwmi win32_bios | select -ExpandProperty SerialNumber
    $NewComputerName = "whateverfrontpartyouwant" + $SN
    Rename-Computer -NewName $NewComputerName -Force -Restart
    

    This purges the local certs for 802.1x , Gets the Serial number from bios Creates a variable with a preset front part and serial number Renames the computer and forces a restart

    we use the above as an inventory Tool with remote run. You could modify to change the host name based off the old by calling host name or doing

    $hostname = hostname
    // returns something like Funhouse3
    $hostname.replace ("Funhouse", "NoFun")
    // returns NoFun3
    

    so

    $hostname = hostname
    Rename-Computer -NewName $hostname.replace ("Funhouse", "NoFun") -Force -Restart
    

    You could probably use the built in PDQ variables to modify this to run local but we had the best success running it as remote.

    0
    Comment actions Permalink
  • mis_super

    Thanks sean, that looks quite nice but still missed the basic pop-up window that allow the "whateverfrontpartwewant" to be keyed then inserted into new name using powershell. the front part for us is usually a 3 letter abbreviation that changes.

    0
    Comment actions Permalink
  • sean johnson

    I did some digging. best way I could find to create the popup and return it as a value is below. Running this local now instead of remote.

    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    $NewHname = [Microsoft.VisualBasic.Interaction]::InputBox("Enter New Hostname", "Get New Name Box", '$(Computer:TARGET)')
    Rename-Computer -computername '$(Computer:TARGET)' -NewName $NewHname -force -restart
    

    my reference for this https://stackoverflow.com/questions/30534273/simple-inputbox-function https://stackoverflow.com/questions/16208013/make-input-box-with-predefined-value

    Edit revised and cleaned up some Worked on my end up to getting access denied on rename, I think I remember this being some quirk that I didn't want to research. This is why I ran our other script local. Perhaps I'm missing something but your box popup is in this one. It references the computer name from PDQs variable, to test just the popup in PS without PDQ first run. I'm sure you can edit it to get what you need out of it.

    $CurHname = "test"
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    $Value = [Microsoft.VisualBasic.Interaction]::InputBox("Enter New Hostname", "Get New Name Box", $CurHname)
    
    0
    Comment actions Permalink
  • mis_super

    champion, never played with powershell windows so didnt occur to me. this worked perfectly. 😄

    0
    Comment actions Permalink
  • mis_super

    Cant make variables work??

    $oldComputerName = $(COMPUTER:TARGET)
    

    PC-1234 : Ther term 'PC-1234' is not recognised as the name of a cmdlet, function, script file or operable program. How does one pass the PDQ variable into powershell as it doesnt work.

    also when i use the command:

    $cred = Get-credential
    

    no windows pops up for credentials to be entered. I'm trying to do this as need to run the Rename-Computer with credentials and the Tool doesnt pass them to powershell.

    0
    Comment actions Permalink
  • sean johnson

    $oldComputerName = '$(COMPUTER:TARGET)' pass it with ' '
    For the pop up box you may need to select close shell on completion (or leave it open) in the tool editor under Shell

    If you use the tool window I think it suppresses input like that.

    0
    Comment actions Permalink
  • mis_super

    champion

    0
    Comment actions Permalink
  • mis_super

    The issue i'm having with this is that in order to get the popup to work the command needs to be run local. but then because its local the pop-up works and the script gives me a Sucess Code: 0.. But nothing happens on the remote machine. In this case i can run it as the PDQ service account or a Domain\Admin with the same result.

    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    $oldHostName = '$(Computer:TARGET)'
    $newHostName = [Microsoft.VisualBasic.Interaction]::InputBox("What's the computers new name?", "ComputerName", $oldHostName)
    Rename-Computer -ComputerName $oldHostName -NewName = $newHostName -force -restart
    

    If I just run the command without prompts as "Remote" all works fine, its the input above that breaks it.

    Rename-Computer -ComputerName ABC123 -NewName = XYZ123 -force -restart
    
    0
    Comment actions Permalink
  • mis_super

    do the credentials field lower in the tool apply to the powershell commands being executed, or is that only for scanning??

    0
    Comment actions Permalink
  • Mark Blackham

    Recently had to rename 500 computers on my campus. I used this script in powershell to make it work. Use PDQ to make sure the powershell that is installed on your computers is Powershell 3.0 or above and then run it directly from powershell no need for pdq at this point. Place a csv file with the list of old computer names and new computers and call for it with the following script. Script will also have you authenticate with your admin credentials one time with a popup and then it will automatically repeat it for all of your hosts. If you get a wmi error you will need to change your remote administration settings on the computer who's name you are trying to change to be allowed in windows firewall. As far as the csv file goes do one column with heading OldName and one column with Heading NewName make sure to designate the path for where you want to pull your csv from so for example if you have put your file in the root of C:\ change the directory in your script to C:\ best of luck to you once I figured this out it saved me so much time.

    cd C:
    $Credential = Get-Credential $a = Import-Csv yourcsvfile.csv -Header OldName, NewName Foreach ( $Server in $a ) {Rename-Computer -ComputerName $Server.OldName -NewName $Server.NewName -DomainCredential $Credential -Force -Restart}

    0
    Comment actions Permalink

Please sign in to leave a comment.