Rename multiple computers using a CSV file

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Aurelie Vaul Aurelie Vaul 11 months ago.

  • Author
    Posts
  • #22333
    Profile photo of Byron Grogan
    Byron Grogan
    Participant

    I'm trying to use the below script to rename a group of computers on our domain. I can't figure out what the "$Server" variable contains.

    Script from TN

    PS C:\>$a = Import-Csv ServerNames.csv -Header OldName, NewName
    PS C:\>Foreach [ $Server in $a ] {Rename-Computer -ComputerName $Server.OldName -NewName $Server.NewName -DomainCredential Domain01\Admin01 -Force -Restart}
  • #22335
    Profile photo of Don Jones
    Don Jones
    Keymaster

    $server will contain an object that has an OldName property and a NewName property – e.g., one line from the CSV file. It looks to me as if you're using it correctly.

    • #22337
      Profile photo of Byron Grogan
      Byron Grogan
      Participant

      Thanks for your response Don! I understand now. $server is specifying what parameter to use on each Header in the csv file.

  • #22338
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Mmm, well, let's make sure the terminology is right ;).

    Import-CSV produces a [i]collection[/i] of [i]objects[/i]. Each object represents one data row from the CSV. The objects have [i]properties[/i] that correspond to the CSV's header row.

    $rows = Import-CSV something.csv
    

    $rows will contain all the objects

    foreach ($row in $rows) {
      $row.myproperty
    }
    

    Goes through each object in $rows, placing one object at a time into $row. $row.myproperty accesses the value in the "MyProperty" column of the current CSV data row. In your example, those property values are being passed to the -ComputerName and -NewName [i]parameters[/i] of the command you're running. So, $server.oldname refers to "the OldName column of the current CSV data row." $server represents a single object, and OldName has become a property of that object.

    I'm a bit of a stickler for terminology, because things get confusing otherwise. Commands have parameters; objects have properties. Parameters are a way of feeding input to a command; properties contain values.

    • #22339
      Profile photo of Byron Grogan
      Byron Grogan
      Participant

      Thanks for your thorough explanation! greatly appreciated and completely understood. thou shall use correct terms for future ref 🙂

  • #32195
    Profile photo of Dave Colosify
    Dave Colosify
    Participant

    For anyone interested in easily renaming multiple computers using a CSV file without having to understand or learn powershell it may be worthwhile giving Netdom Rename Computer GUI a try. Although it doesn't use powershell, Netdom can still get the job done and we have made it even more simple with a graphical user interface. All you need to do is import your CSV file, input your domain credentials and click Rename.

    Get Netdom Rename Computer GUI

  • #33651
    Profile photo of Aurelie Vaul
    Aurelie Vaul
    Participant

    Bonjour,

    Quelqu'un serait-il au courant si il est possible de renommer des machines qui sont deja intégré au domaine , sans devoir les enlever du domaine...

    J'ai toujours l'erreur "Accès refusé"
    Voici le code :
    # Fonction renmame permet de renommer les postes

    function RenameDomaine ($tabArgument)
    {

    $adminlogin = "domaine\administrateur"
    Read-Host -prompt "pwd admin du domaine " -AsSecureString |
    ConvertFrom-SecureString | out-file Monpwdadmin.pwd
    $adminpassword = Cat .\Monpwdadmin.pwd | ConvertTo-SecureString
    $adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminlogin, $adminpassword

    if ($adobjSalle -ne $null)
    {
    $confirmation = Read-Host "Etes-vous bien sur de vouloir renommer la salle $salle y/n ?"

    if ($confirmation -eq 'y')
    {
    foreach ($arg in $tabArgument)
    {
    foreach($entry in $list)
    {
    if ($arg -eq 'all' -or ($arg -eq $($entry.IP)))
    {
    write-host "Traitement du poste: " $($entry.Nom) -foregroundcolor green
    $result = Get-WmiObject Win32_PingStatus -filter "address='$($entry.IP)'"
    if ($result.statuscode -eq 0)
    {
    $newname2 = $($entry.Nom2)
    $newname = $($entry.Nom)
    Invoke-Command -computername $($entry.IP) -credential $admincredential -Scriptblock {param($newname2);rename-computer -NewName ($newname2) -force -restart} -argumentlist $newname2,$admincredential -ErrorVariable error

    if ($error -ne 0)
    {
    write-host "Rename echoue – voir detail sur log.txt" -foregroundcolor red
    Add-Content -Path $event -Value "Rename KO — $($entry.IP) $error "
    }
    else
    {
    write-host "Rename Ok" -foregroundcolor green

    }
    }
    else
    {
    write-host "Operation annulee verifier la connexion reseau" -foregroundcolor red
    }
    }
    }
    }
    }
    }
    else
    {
    Write-host " Operation annulee Verifier l'orthographe de la salle $salle" -foregroundcolor green
    }
    }

You must be logged in to reply to this topic.