function - Powershell script works in ISE but not in console -
I am writing a power shell script that will initiate a process, monitor a folder for file changes, and then Backup folder when file
If I run my script from within PowerSheed ISE, then it works fine, I can monitor the folder and it will save the backup properly.
The problem is that I will run a batch file that will run the PowerShell script. But whenever I run the script from the PowerShell console, or whenever I run my batch file that runs the script, it no longer works at all. The script runs and the events are registered when I copy the copy of a file into the viewed folder, then I only get the changed event and not the created event, and the DUSTFOOOM function is no longer called. I'm not sure how to go about debugging this: /
The following is my script: I have removed the parts that are not currently related to actual wrong, so use some variables I am here, you will not see declared, but they are there. I get a host to write for the console, when a changed event happens, but there is no built event (though in the form of an angel in the ISE, I get both events and everything works fine)
#unregister events, if they were not registered properly before just siliently error if they enter the console process console hosts started #START unregistered event ConsoleStopped -ErrorAction SilentlyContinue unregistered event FileCrea Ted -ErrorAction SilentlyContinue Unregistered - Event FileChanged -ErrorAction SilentlyContinue Unregistered-Event TimerTick -ErrorAction SilentlyContinue does not exist. $ ConsoleProcess = Start process "$ consoleExe" -PassThru the console out of the #register register ObjectEvent $ consoleProcess to listen to stops -SourceIdentifier ConsoleStopped action {type host console events unregistered event ConsoleStopped -ErrorAction SilentlyContinue unregistered #unregister stopped -Event FileCreated -ErrorAction SilentlyContinue unregistered event FileChanged -ErrorAction SilentlyContinue if (! $ timer.Enabled) {unregistered event TimerElapsed -ErrorAction SilentlyContinue Remove-item $ timers} Remove-item $ Fsw Remove-item $ ConsoleProcess} #watch all files / f Olders $ filter = '*. * '# You can enter wildcard filter here. # In the following line, you can change the 'IncludeSubdirectories to $ true' if necessary. $ Fsw = New-object IO.FileSystemWatcher $ folder, $ filter -Property @ {IncludeSubdirectories = $ true; NotifyFilter = [IO.NotifyFilters] 'DirectoryName, file name, LastWrite'} #register register ObjectEvent made $ Fsw to FileCreated event - SourceIdentifier FileCreated replaced action FileChanged event register ObjectEvent $ fsw -SourceIdentifier FileChanged action {Write host Change event occurred doStuff ($ event)} function event designed #register {write-host doStuff have went back doStuff ($ event)} ($ event) {write-host doStuff the $ name = $ Event.SourceEventArgs .Name $ changeType = $ Event.SourceEventArgs.ChangeType $ timeStamp = $ Event.TimeGenerated Write-Host "$" in the file '$ name' folder '$ TIMESTAMP was $ changeType' Mr. -fore if (! $ Timer.Enabled) {Write hosts start saving timer register ObjectEvent $ timers passed -SourceIdentifier TimerElapsed action $ TimerAction $ timer.Start () file out "$ BackupDir \ backup.log" -Append - Force -InputObject "A request for backup order flows on the $ TimeStamp"} and {write-host a backup has already been requested}} function backupSave () {start-host backup is starting ... $ timestamp = Get-date -Format o | Foreign currency {$ _ - location ":", "." } Copy-item $ folder "$ backupDir \ backup_ $ timestamp" -Resert -Foice} < P> Try to move your tasks to the beginning of your script. i.e. take the function doStuff ($ event) and function backupSave () to the beginning of the script. The problem may be that when you are calling it works from the script that has not yet been defined. This powerhouse works in the ISE because you played the script several times and the functions were defined in one or another time.
Comments
Post a Comment