Home > Software engineering >  powershell catch export to csv not working
powershell catch export to csv not working

Time:11-05

I'm writing a script that is fed a .csv and tries to make an operation and then catches into a seperate .csv but for some reason I cant seem to feed the catch info into the csv. I get the error, "Export-csv : Cannot process argument because the value of argument "name" is not valid. Change the value of the "name" argument and run the operation again."

I appreciate any input from the brains.

#imports a csv and does somthing with the data. The columns in the csv are specified by the $($_.'columnName')
Import-Csv -Path 'PathToMyCSV.csv' | ForEach-Object{
    
    #Print associated User
    Write-Host  "$($_.ModifiedEmail)" -ForegroundColor Yellow -NoNewline;
    Write-Host " is the user's email prefix, " -NoNewline


    #Print PC name to be moved
    Write-Host "SD-LT-$($_.PCName)" -ForegroundColor Yellow -NoNewline;
    Write-Host " is the PC they use, and " -NoNewline



    #Print The OU the computer is moving to
    Write-Host "$($_.OU)" -ForegroundColor Yellow -NoNewline;
    Write-Host "is the OU the computer needs to go in!"
    
    $pcname = "SD-LT-$($_.PCName)"

    Try {
    Get-ADComputer SD-LT-$($_.PCName) | Move-ADObject -TargetPath $($_.OU)
        }

    Catch  {
       Write-Host $_ -ForegroundColor Magenta 
       $pcname | Export-csv -Path 'PathToAnotherCSV.csv' -Append -Force
         }

    }

CodePudding user response:

Try creating a PSCustomObject.

[PSCustomObject]@{'pcname'=$pcname} | Export-csv -Path 'PathToAnotherCSV.csv' -Append -Force
  • Related