监视文件的更改并使用 powershell 运行命令 [英] Watch file for changes and run command with powershell

查看:33
本文介绍了监视文件的更改并使用 powershell 运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何简单的方法(即脚本)在 Powershell 中查看文件并在文件更改时运行命令.我一直在谷歌搜索,但找不到简单的解决方案.基本上我在 Powershell 中运行脚本,如果文件更改,则 Powershell 运行其他命令.

Is there any simple way(i.e., script) to watch file in Powershell and run commands if file changes. I have been googling but can't find simple solution. Basically I run script in Powershell and if file changes then Powershell run other commands.

编辑

好吧,我想我犯了一个错误.我不需要脚本,一个可以包含在我的 $PROFILE.ps1 文件中的需要函数.但是,我仍然很努力,仍然无法写出来,所以我会给予赏金.它必须看起来像这样:

Ok I think I made a mistake. I don't need script, a need function that I can include in my $PROFILE.ps1 file. But still, I was trying hard and still I'm unable to write it, so I will give bounty. It have to look like this:

function watch($command, $file) {
  if($file #changed) {
    #run $command
  }
}

有一个 NPM 模块正在做我想做的事情,watch,但它只监视文件夹而不是文件,而且它不是 Powershell xD.

There is a NPM module that is doing what I want, watch , but it only watches for folders not files, and it's not Powershell xD.

推荐答案

这是我在我的片段中找到的一个例子.希望它更全面一点.

Here is an example I have found in my snippets. Hopefully it is a little bit more comprehensive.

首先,您需要创建一个文件系统观察器,然后订阅观察器正在生成的事件.此示例侦听创建"事件,但可以轻松修改以注意更改".

First you need to create a file system watcher and subsequently you subscribe to an event that the watcher is generating. This example listens for "Create" events, but could easily be modified to watch out for "Change".

$folder = "C:UsersLOCAL_~1AppDataLocalTemp3"
$filter = "*.LOG"
$Watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
    IncludeSubdirectories = $false
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $Watcher -EventName Created -SourceIdentifier FileCreated -Action {
   $path = $Event.SourceEventArgs.FullPath
   $name = $Event.SourceEventArgs.Name
   $changeType = $Event.SourceEventArgs.ChangeType
   $timeStamp = $Event.TimeGenerated
   Write-Host "The file '$name' was $changeType at $timeStamp"
   Write-Host $path
   #Move-Item $path -Destination $destination -Force -Verbose
}

我会尽量缩小范围以满足您的要求.

I will try to narrow this down to your requirements.

如果您将此作为profile.ps1"脚本的一部分运行,您应该阅读配置文件的力量 解释了可用的不同配置文件脚本等等.

If you run this as part of your "profile.ps1" script you should read The Power of Profiles which explains the different profile scripts available and more.

此外,您应该了解等待文件夹中的更改不能作为脚本中的函数运行.配置文件脚本必须完成,您的 PowerShell 会话才能启动.但是,您可以使用函数来注册事件.

Also, you should understand that waiting for a change in a folder can't be run as a function in the script. The profile script has to be finished, for your PowerShell session to start. You can, however use a function to register an event.

它的作用是注册一段代码,在每次触发事件时执行.当会话保持打开状态时,此代码将在当前 PowerShell 主机(或 shell)的上下文中执行.它可以与主机会话交互,但不知道注册代码的原始脚本.当您的代码被触发时,原始脚本可能已经完成.

What this does, is register a piece of code, to be executed every time an event is triggered. This code will be executed in the context of your current PowerShell host (or shell) while the session remains open. It can interact with the host session, but has no knowledge of the original script that registered the code. The original script has probably finished already, by the time your code is triggered.

代码如下:

Function Register-Watcher {
    param ($folder)
    $filter = "*.*" #all files
    $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
        IncludeSubdirectories = $false
        EnableRaisingEvents = $true
    }

    $changeAction = [scriptblock]::Create('
        # This is the code which will be executed every time a file change is detected
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        Write-Host "The file $name was $changeType at $timeStamp"
    ')

    Register-ObjectEvent $Watcher -EventName "Changed" -Action $changeAction
}

 Register-Watcher "c:	emp"

运行此代码后,更改C: emp"目录(或您指定的任何其他目录)中的任何文件.您将看到触发代码执行的事件.

After running this code, change any file in the "C: emp" directory (or any other directory you specify). You will see an event triggering execution of your code.

此外,您可以注册的有效 FileSystemWatcher 事件包括已更改"、已创建"、已删除"和已重命名".

Also, valid FileSystemWatcher events you can register are "Changed", "Created", "Deleted" and "Renamed".

这篇关于监视文件的更改并使用 powershell 运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆