Export-Csv Incorrect function.

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
pls-sapien
Posts: 29
Joined: Tue Apr 05, 2016 9:03 pm

Export-Csv Incorrect function.

Post by pls-sapien » Sun Dec 01, 2019 11:44 pm

Hello,
when i run the function below in Studio i get: Incorrect function.
  1. function retention-file ($file)
  2.  
  3. {
  4.  
  5.     $currentfile = import-csv -Path $file
  6.  
  7.     $currentfile | Select-Object -Last 3 | Export-Csv -Path $file -Force -Confirm:$false -NoTypeInformation
  8.  
  9. }
here is an example of the csv file:
ComputerName,MAC,IP,Date
"PC-01-01","74-D4-25-B3-77-7A","10.0.0.1","02/12/2019"
"PC-00-00","74-D4-25-B3-77-7B","10.0.0.2","02/12/2019"
"PC-11-01","74-D4-25-B3-77-7C","10.0.0.3","02/12/2019"
"PC-01-11","74-D4-25-B3-77-7D","10.0.0.4","02/12/2019"

it works in ISE...
Any help would be great
Thanks,
Sean

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Export-Csv Incorrect function.

Post by jvierra » Sun Dec 01, 2019 11:52 pm

Please post the exact and full error message.

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Export-Csv Incorrect function.

Post by jvierra » Sun Dec 01, 2019 11:57 pm

I will also point out that the style you are using is prone to these kinds of errors. If we use the correct open style of bracketing then this will never happen.

Code: Select all


function retention-file ($file){
    $currentfile = import-csv -Path $file
    $currentfile | Select-Object -Last 3 | Export-Csv -Path $file -Force -Confirm:$false -NoTypeInformation
}
retention-file file.csv
Notice the opening brace is on the end of the calling line and not on the next line. This is one of the best reasons to not use the newer next line brace style.

Post Reply