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.
This topic is 4 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
pls-sapien
Posts: 31
Last visit: Tue Dec 19, 2023 12:55 am

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.     $currentfile = import-csv -Path $file
  4.     $currentfile | Select-Object -Last 3 | Export-Csv -Path $file -Force -Confirm:$false -NoTypeInformation
  5. }
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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Export-Csv Incorrect function.

Post by jvierra »

Please post the exact and full error message.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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.
This topic is 4 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked