I have been working on a short powershell script that loads a CSV and for each row of data it finds the file in the folder and moves it to a separate destination. The purpose of this is to find and move thousands of specific files out of 400k of files in a storage archive.
IMPORT-CSV -PATH process-docs.csv -ERRORACTION STOP | foreach {MOVE-ITEM -PATH $_ -DESTINATION \to-here\ }
That's my current working script but now I need to add error logging to it so that it outputs the error as a text file for review purposes. Since we could be running this on 100k files, it would be useful to know when it hits an error via the log.
Here is a version I tried creating but I run into problems with the following error:
IMPORT-CSV -PATH moveFileDoc.csv -ERRORACTION STOP -ERRORVARIABLE +Errors | SELECT -ExpandProperty Filename | foreach-object {MOVE-ITEM -PATH $_ -DESTINATION move-here\ } | $Errors | Out-File c:\temp\log.txt -Append
The error: Expressions are only allowed as the first element of a pipeline.
I'm very much new to Powershell and from my investigations it seems to be because the $Errors part is not part of the foreach loop so I modified it to a foreach-object but still no luck. I suspect I'm missing something obvious in the structure of my script.
Any thoughts?