PowerShell isn't monitoring a directory correctly

安稳与你 提交于 2020-03-02 06:57:06

问题


I have a Powershell script running on a server and I can't find the reason for it to stop working. I would appreciate any help given/suggested. The Powershell is scheduled to be run (restarted) every day at 4:15. It used to work fine, but for some reason I am experiencing issues now:

1) Powershell is not writing anything in the log file. I use the below action in order to log the event.

$action = { $path = $Event.SourceEventArgs.FullPath
            $changeType = $Event.SourceEventArgs.ChangeType
            $logline = "$(Get-Date), $changeType, $path"
            Add-content "D:\tdp\bin\UpgradeOBJECT\logs\PowerShellLog.txt" -value $logline

          }  

2) I have added two monitoring from one Powershell script, so the part 1 would be used again in logging the changes on different folders.

3) The Powershell is not triggering the batch file's anymore. It used to trigger this fine as well. Without the Powershell the process is running fine (so if I run the below batch files, they would produce the correct result's).

$action2 = { 
            cmd.exe /c "D:\tdp\bin\UpgradeOBJECT\upgrade_PNL_OBJECT_680.bat"
            cmd.exe /c "D:\tdp\bin\UpgradeOBJECT\upgrade_OBJECT_680.bat"
           } 

4) I am running this task from task scheduler daily. It would trigger a batch file which would put the PowerShell script in the background.

   @echo off

   Taskkill /IM powershell.exe /F

  %~dp0

  powershell.exe -windowstyle hidden -file 
  D:\tdp\bin\UpgradeOBJECT\SS_DS_SCHEMA_UPGRADE.ps1

  exit /b 1    

5) The Powershell is monitoring two different directories. It should run a batch file from two different directories and log the changes in two different folders. I don't think this is a folder issue, but just to be sure, can anyone confirm?

6) The full code of the Powershell is this:

1) I tried running the Powershell script, went to the folder and added some files (created a .txt file) and deleted, renamed etc. This did not trigger the log file to write in it.

2) Put back the old version of PowerShell (the below one seen) This also did not work.

$watchSSDS = New-Object System.IO.FileSystemWatcher
$watchSSDS.Path = "Y:\LA\QA\Build_Archive\PRODUCT\PRODUCT 6.8.0\Servers"
$watchSSDS.Filter = "*.*"
$watchSSDS.IncludeSubdirectories = $false
$watchSSDS.EnableRaisingEvents = $true 

$WatchSchema = New-Object System.IO.FileSystemWatcher
$WatchSchema.Path = "Y:\LA\QA\Builds\Build Area\OBJECT\PRODUCT 6.8.0\QA1\OBJECT"
$WatchSchema.Filter = "*.*"
$WatchSchema.IncludeSubdirectories = $false
$WatchSchema.EnableRaisingEvents = $true 


$action = { $path = $Event.SourceEventArgs.FullPath
            $changeType = $Event.SourceEventArgs.ChangeType
            $logline = "$(Get-Date), $changeType, $path"
            Add-content "D:\tdp\bin\UpgradeSS\logs\PowerShellLog.txt" -value $logline

          }  

$action2 = { 
            cmd.exe /c "D:\tdp\bin\UpgradeSS\upgrade_PNL_OBJECT_680.bat"
            cmd.exe /c "D:\tdp\bin\UpgradeSS\upgrade_OBJECT_680.bat"
           } 

$action3 = { 

            cmd.exe /c "D:\tdp\bin\UpgradeSchema\full_schema_upgrade_OBJECT_680.bat"
            cmd.exe /c "D:\tdp\bin\UpgradeSchema\full_schema_upgrade_PNL_OBJECT_680.bat"                

           } 

$action4 = { $path = $Event.SourceEventArgs.FullPath
            $changeType = $Event.SourceEventArgs.ChangeType
            $logline = "$(Get-Date), $changeType, $path"
            Add-content "D:\tdp\bin\UpgradeSchema\logs\PowerShellLogSchema.txt" -value $logline

          }              

Register-ObjectEvent $watchSSDS "Created" -Action $action2

Register-ObjectEvent $watchSSDS "Created" -Action $action
Register-ObjectEvent $watchSSDS "Changed" -Action $action
Register-ObjectEvent $watchSSDS "Deleted" -Action $action
Register-ObjectEvent $watchSSDS "Renamed" -Action $action


Register-ObjectEvent $WatchSchema "Created" -Action $action3
Register-ObjectEvent $WatchSchema "Changed" -Action $action3
Register-ObjectEvent $WatchSchema "Deleted" -Action $action3
Register-ObjectEvent $WatchSchema "Renamed" -Action $action3


Register-ObjectEvent $WatchSchema "Created" -Action $action4
Register-ObjectEvent $WatchSchema "Changed" -Action $action4
Register-ObjectEvent $WatchSchema "Deleted" -Action $action4
Register-ObjectEvent $WatchSchema "Renamed" -Action $action4

while ($true) {sleep 10}

来源:https://stackoverflow.com/questions/58162178/powershell-isnt-monitoring-a-directory-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!