none
Change a list of Computer Names from a Text file or CSV using Powershell only

    Domanda

  • I am new to Powershell scripting and have a small job I need to complete.  I have a list of 100 PC's at our Disaster Recover site that are named using our old naming convention.  i would like to rename them using Powershell only - not netdom!

    I have found this script which works great if I manually put the OLD and NEW PC names in:

    $credential = Get-Credential
    
    Get-WmiObject Win32_ComputerSystem -ComputerName OLDNAME -Authentication 6 |
    ForEach-Object {$_.Rename("NEWNAME",$credential.GetNetworkCredential().Password,$credential.Username)}
    
    Get-WmiObject Win32_OperatingSystem -ComputerName OLDNAME |
    ForEach-Object {$_.Win32Shutdown(6)}  

    I would like to modify this script to read the old PC name and new PC name from a text file that has all the old and new names listed in this format:

    OldName,NewName

    QTKDILLONM,Q-DILLONM

    Playing around with scripts, I am able to get this script

    $file = "C:\temp\newname.txt";
    Import-Csv -path $file | foreach {
    
    $oldName = $_.OldName;
    $newName = $_.NewName;
    
    Write-Host "Rename machine from $oldName to $newName";
    }

    to give me the following output:

    PS C:\Users\MJD> C:\test.ps1
    Rename machine from QTKCICCONEA to Q-CICCONEA
    Rename machine from  to

    So it is reading from the text file and then doing the Write-Host

    Attempting to combine the two scripts, I came up with this:

    $file = "C:\temp\newname.txt";
    Import-Csv -path $file | foreach {
    
    $oldName = $_.OldName;
    $newName = $_.NewName;
            
          Write-Host "Rename machine from $oldName to $newName";
    
          Get-WmiObject Win32_ComputerSystem -ComputerName $oldName -Authentication 6 |
          ForEach-Object {$_.Rename($newName,$credential.GetNetworkCredential().Password,$credential.Username)}
    
          Get-WmiObject Win32_OperatingSystem -ComputerName $oldName |
          ForEach-Object {$_.Win32Shutdown(6)}
         
                                    }

    I get these results:

    PS C:\Users\MJD> C:\test.ps1
    Rename machine from QTKEBERLEJ to Q-EBERLEJ
    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    At C:\_Task\test.ps1:9 char:20
    +       Get-WmiObject <<<<  Win32_ComputerSystem -ComputerName $oldName -Authentication 6 |
        + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
        + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
     
    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    At C:\_Task\test.ps1:12 char:20
    +       Get-WmiObject <<<<  Win32_OperatingSystem -ComputerName $oldName |
        + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
        + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    I am hoping someone out there can assist me with this script. I am surprised there is nothing out there already.Thanks,

    Matt

    mercoledì 27 giugno 2012 17:27

Risposte

  • Yeah.  The script was fine.  The two random PC's I chose to test this on were powered off.  Thanks for the replies. 
    • Contrassegnato come risposta pugmohone venerdì 29 giugno 2012 19:21
    venerdì 29 giugno 2012 19:21

Tutte le risposte