用于监控C盘的Power shell脚本

用于监控C盘的Power shell脚本
# find the path to the desktop folder:

 $desktop = [Environment]::GetFolderPath('Desktop')

 #specify the path to the folder you want to monitor:

 $Path = $desktop

# specify which files you want to monitor
$FileFilter = '*'  

# specify whether you want to monitor subfolders as well:
$IncludeSubfolders = $true

# specify the file or folder properties you want to monitor:
$AttributeFilter = [IO.NotifyFilters]::FileName, [IO.NotifyFilters]::LastWrite 

# specify the type of changes you want to monitor:
$ChangeTypes = [System.IO.WatcherChangeTypes]::Created, [System.IO.WatcherChangeTypes]::Deleted

# specify the maximum time (in milliseconds) you want to wait for changes:
$Timeout = 1000

# define a function that gets called for every change:
function Invoke-SomeAction
{
  param
  (
    [Parameter(Mandatory)]
    [System.IO.WaitForChangedResult]
    $ChangeInformation
  )
  
  Write-Warning 'Change detected:'
  $ChangeInformation | Out-String | Write-Host -ForegroundColor DarkYellow


}

# use a try...finally construct to release the
# filesystemwatcher once the loop is aborted
# by pressing CTRL+C

try
{
  Write-Warning "FileSystemWatcher is monitoring $Path"
  
  # create a filesystemwatcher object
  $watcher = New-Object -TypeName IO.FileSystemWatcher -ArgumentList $Path, $FileFilter -Property @{
    IncludeSubdirectories = $IncludeSubfolders
    NotifyFilter = $AttributeFilter
  }

我必须监控 C 盘是否安装或卸载了任何软件,上述脚本将提供详细信息,如果安装或卸载了任何软件,它必须显示消息,说明特定软件已安装或卸载并带有日期,但我无法扫描 C 盘,但桌面文件夹正在扫描。请让我知道如何扫描 C 盘并注意更改。我使用了 GetFolderPath() 函数。

答案1

您的脚本实际上从声明路径开始:

# find the path to the desktop folder:

 $desktop = [Environment]::GetFolderPath('Desktop')

 #specify the path to the folder you want to monitor:

 $Path = $desktop

所以如果你改变它$Path = "C:\"它应该可以工作。

相关内容