I found the script below which watches the parent folder (including subfolders) and prints whenever a new pdf is generated in one of those. This works fine.
But… I cannot get the file moved from ex. subfolder1 to “subfolder1>printed” afterwards.
Problems: Move-Item not working, no log output (just added 2 min ago for troubleshooting).
Also I should probably make sure that the “printed” folders are not watched, so that I don’t get a print from there too.
So, you probably don’t want to move the files to a subfolder in the same hierarchy, or you’ll trigger the Watcher again. Keep that in mind.
Also keep in mind that you don’t need to concatenate paths like you’re doing, and that the Join-Path command is a better way of doing so.
$destination = Join-Path $folderpath “printed”
Are you getting any errors? My supposition is that your -Destination path isn’t correctly formed, which my suggestion will correct, but without any kind of errors it’s a little tough to troubleshoot.
Hi Don
Thanks a lot for the reply. I have never heard of join-path before. But I can see that it seems like the right way to go. I’ll see if I can make that work.
Regarding providing errors, I would love to give you some, but I am not getting any?which is why I was trying the out-file log thing.
I was thinking if this failing could be due to the script kind of “never ending”, cause it is always active (watching for new files). Don’t know if I am right in that assumption?
When I run the script in ISE, I just get this:
PS C:\Users\jeskri> P:\Powershell scripts\PDF_Printout3.ps1
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
2 ce4b160a-791... NotStarted False ...
PS C:\Users\jeskri>
No output what so ever, when dragging pdf files to the folders… or any errors from the move-item command which is not working.
I have been searching a little bit for “the right way” to get errorlogs from your scripts, but there seem to be a lot of different ways to do it. PSLogging, the transscipt way or write-debug etc? any suggestions?
There is something wrong with the $folderpath variable and I admit that I am not at all sure where in the script to place that variable? I tried placing it in the top with the other variables, but that gives an error in the console:
Get-Item : Cannot bind argument to parameter ‘Path’ because it is null.
Placing it where it is now in the above script, gives a similar error in the errorlog.
Outlog.txt gives this error:
Move-Item : Cannot bind argument to parameter 'Path' because it is null.
At P:\Powershell scripts\PDF_Printout6.ps1:13 char:18
+ Move-Item -Path $folderPath.path -Filter *.pdf -Destination ($folderpath + "\" ...
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Move-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.MoveItemCommand
Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'.
At P:\Powershell scripts\PDF_Printout6.ps1:13 char:2
+ Move-Item -Path $folderPath.path -Filter *.pdf -Destination ($folderpath + "\" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
I guess it is pretty obvious that I don’t know what the hell I am doing half the time. But I’m learning and think it is exciting, so that is a positive thing.
Method invocation failed because [System.IO.FileInfo] does not contain a method named ‘op_Addition’.
At P:\Powershell scripts\PDF_Printout6.ps1:13 char:2
It seems that this:
$folderpath = Get-Item $Event.SourceEventArgs.fullpath
…contains both the path AND the filename. So it includes the file name in the path.
Outlog says:
file moved to C:\DocsToPrint\Files\Outgoing\xxx.pdf\printed
I tried to not use the “fullpath” property (if it is infact a property?):
$folderpath = Get-Item $Event.SourceEventArgs.path
…but that doesn’t work. Not sure how to get a list of which properties can be used for $Event.SourceEventArgs ??
I know what you mean. My knowledge of Powershell, Objects, properties and so on is simply not deep enough for me to solve this on my own. and since I am from Denmark, once in a while, my understanding of English sometimes add to the confusion as well.
I have been googeling all afternoon to find something usefull.
I found and tried this: $FileSystemWatcher | gm -membertype *
gm : You must specify an object for the Get-Member cmdlet.
At line:1 char:15
+ $Folderpath | gm -membertype *
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException
+ FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand
probably because $Event.SourceEventArgs is not an object that can be piped to gm or??
I did find another thread on another forum, where NotifyFilter is marketed.
Any idea if that would be a better way to go when trying to get where I want?
This is probably not the “cleanest” code in history, but it get the job done.
Two questions!
If a file with the same name already exists in “printed” folder, it doesn’t move the files. I would like to rename the newest file by adding a number and then move. Ideas?
I would like to place the script in the startup folder on the server, so it executes the ps1 script after reboot etc. I created a .cmd file with these entries: