none
Rename Computers using CSV RRS feed

  • Question

  • I wrote this script to rename computers using a CSV and the renaming works but the logging doesn't, the file is empty. Thanks in advance for any hints.

    $id = (Get-Credential)

    $line ="`r`n"

    $logfile = "c:\renamelog.txt"

    $success = " successfully changed to "

    $fail = " failed to change to "

    foreach ($computer in (Import-Csv -Path c:\rename.csv))

    {

      Rename-Computer -ComputerName $computer.OldName -NewName $computer.NewName -DomainCredential $id -PassThru -Force

      if ($ComputerChangeInfo.HasSucceeded -eq "True")

      {

        $line | Out-File $logfile

        $computer.OldName | Out-File $logfile

        $success | Out-File $logfile

        $computer.NewName | Out-File $logfile

      }

      elseif ($ComputerChangeInfo.HasSucceeded -eq "False")

      {

        $line | Out-File $logfile

        $computer.OldName | Out-File $logfile

        $fail | Out-File $logfile

        $computer.NewName | Out-File $logfile

      }

    }

    Read-Host "Press Enter to Exit" 


    • Moved by Bill_Stewart Monday, July 29, 2019 7:49 PM This is not "fix/debug/rewrite script for me" forum
    Monday, November 12, 2018 9:05 PM

All replies

  • You must use "-Append" or the file will be constantly overwritten.


    \_(ツ)_/

    Monday, November 12, 2018 9:23 PM
  • Idk if I'll can help you with powershell, but certainly in bat script I can.

    How this rename works?

    Here in my job the computers had a name like PC001, PC002, PC003... And the rename process put a prefix in the names, like COMPANYNAME_PC001, COMPANYNAME_002, COMPANYNAME_003 and so on.

    If what you wanna do is something like this, reply me.

    Monday, November 12, 2018 9:47 PM
  • I don't follow where this is getting a value ? ComputerChangeInfo.HasSucceeded - maye try what I set below, may not resolve to True or False but at least have a value assigned to the variable. Also add the -append

    Foreach ($computer in (Import-Csv -Path c:\rename.csv))
    
    {
    
     $ComputerChangeInfo= Rename-Computer -ComputerName $computer.OldName -NewName $computer.NewName -DomainCredential $id -PassThru -Force
    
      if ($ComputerChangeInfo -eq "True")
    
      {
    
        $line | Out-File $logfile -append
    
        $computer.OldName | Out-File $logfile -append
    
        $success | Out-File $logfile -append
    
        $computer.NewName | Out-File $logfile -append
    
      }
    
    }


    • Edited by ComputerScott Wednesday, November 28, 2018 3:49 AM
    • Proposed as answer by ComputerScott Wednesday, November 28, 2018 3:50 AM
    Wednesday, November 28, 2018 3:42 AM
  • I wouldn't worry about this.  It is a very badly written script and the user has abandoned the thread.  It is about time to move this to the recycle bin.


    \_(ツ)_/


    • Edited by jrv Wednesday, November 28, 2018 3:47 AM
    Wednesday, November 28, 2018 3:46 AM
  • FYI. The following is nonsense code:

     if ($ComputerChangeInfo -eq "True")

    This is how to test a Boolean:

     if ($ComputerChangeInfo)

    This is the purpose of a Boolean.  It is either $true or $false.  Testing it against a string makes very little logical sense.



    \_(ツ)_/

    Wednesday, November 28, 2018 3:53 AM