powershellexport-csv

Unsure how to get Export-Csv to work for what I need


I have to rename a little over 100 computers in the warehouse where I work. My script is setup to delay the renaming process until a desired time and gets the computer names from a .csv file. The current date listed is just when I was planning on running the next test of the script before the full scale deployment.

$Credential=Get-Credential
$ComputerList = Import-Csv -Path '\\Shares\Scripts\computers.csv' 
$end = Get-Date "2024/06/11 06:00:00"

while ((Get-Date) -lt $end) {
    start-sleep 3600
}

foreach ($Computer in $ComputerList){
    Rename-Computer -ComputerName $Computer.oldname -NewName $Computer.newname -DomainCredential $Credential -Force -Restart
}

What I need help with, is using the Export-Csv function to generate a CSV file with a list of all machines that were offline or otherwise unable to be renamed. I was hoping to use the Write-Warning command to assist, but my understanding of powershell isn't quite high enough to work this out on my own. The computers are spread throughout a rather large warehouse and while I don't mind going out to them, I'd rather not have to touch every single machine to verify if the renaming was successful or not.

I've tested what I have so far, and it will skip over any computer listed in the .csv that is offline and continue down the list. I tested this with two machines while the first machine in the list was offline; hence, why I want to export what computers are offline or any error that's that are thrown if a renaming is unsuccessful. Bonus if I could have it add "Pass" / "Fail" as a third column in my existing .scv file.

Any assistance is greatly welcomed.


Solution

  • this generates\appends a CSV of only the failed renaming.

    Should be enough of a base for you to change it to fit your purposes

    $ComputerList = Import-Csv -Path '\\Shares\Scripts\computers.csv' 
    
    $Credential = Get-Credential
    
    # no reason to use a While
    $EndSleep = Get-Date '2024/06/11 06:00:00'
    $StartSleep = Get-Date 
    Start-Sleep -Seconds ($EndSleep - $StartSleep).TotalSeconds
    
    $FailureList = foreach ($Computer in $ComputerList) {
        # Splat the parameters, so it's easier to read    
        $Splat = @{
            ComputerName     = $Computer.oldname
            NewName          = $Computer.newname
            DomainCredential = $Credential
            Force            = $true
            Restart          = $true
            # PassThru returns a boolean whether or not the rename worked
            PassThru         = $true
            # ErrorAction Stop throws a breaking error if there is an error
            ErrorAction      = 'Stop'
        }
    
        try {
            $Result = Rename-Computer @Splat
            # if the rename failed cleanly, return an object with:
            # timestamp in UTC
            # the name of the computer that failed
            # and a generic reason
            if (-not $Result) { 
                [pscustomobject]@{
                    TimestampUTC  = Get-Date -Format FileDateTimeUniversal
                    ComputerName  = $Computer.oldname
                    FailureReason = 'Rename-Computer did not succeed'
                } 
            }
        } 
        # if the rename failed throwing an error, catch it and return an object with:
        # timestamp in UTC
        # the name of the computer that failed
        # the error
        catch {
            [pscustomobject]@{
                TimestampUTC  = Get-Date -Format FileDateTimeUniversal
                ComputerName  = $Computer.oldname
                FailureReason = $_.Exception
            } 
        }
    }
    
    # Export failed renamings to a CSV, adding them to an existing one
    $FailureList | Export-Csv -Path $CsvPath -NoTypeInformation -Append