PowerShell无法正确监视目录

时间:2019-09-30 05:57:19

标签: powershell monitor taskscheduler

我在服务器上运行了Powershell脚本,但找不到停止运行的原因。我将不胜感激给您/建议。 Powershell计划于每天4:15运行(重新启动)。过去工作正常,但由于某种原因,我现在遇到问题:

1)Powershell没有在日志文件中写入任何内容。我使用以下操作来记录事件。

$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)我从一个Powershell脚本中添加了两个监视,因此第1部分将再次用于记录对不同文件夹的更改。

3)Powershell不再触发该批处理文件。它也曾经触发过这种罚款。如果没有Powershell,该过程将无法正常运行(因此,如果我运行以下批处理文件,它们将产生正确的结果)。

$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)我每天从任务计划程序运行此任务。它将触发一个批处理文件,该文件会将PowerShell脚本置于后台。

   @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)Powershell正在监视两个不同的目录。它应该从两个不同的目录运行一个批处理文件,并将更改记录在两个不同的文件夹中。我不认为这是文件夹问题,但是请确保有人可以确认吗?

6)Powershell的完整代码是这样的:

1)我尝试运行Powershell脚本,转到该文件夹​​并添加了一些文件(创建了.txt文件),然后将其删除,重命名等。这并未触发日志文件写入其中。

2)放回旧版本的PowerShell(见下面的一个),这也不起作用。

$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}

0 个答案:

没有答案