Export-Csv Incorrect function.

Ask your 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.
Locked
User avatar
pls-sapien
Posts: 31
Joined: Tue Apr 05, 2016 9:03 pm

Export-Csv Incorrect function.

Post by pls-sapien »

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: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Export-Csv Incorrect function.

Post by jvierra »

Please post the exact and full error message.

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

Re: Export-Csv Incorrect function.

Post by jvierra »

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.

Locked