观察文件的更改和运行命令与powershell [英] Watch file for changes and run command with powershell

查看:269
本文介绍了观察文件的更改和运行命令与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.


EDIT

EDIT

Ok我想我犯了一个错误。我不需要脚本,一个需要的功能,我可以包括在我的 $ 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 truing 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模块正在做我想要的,观察,但它只监视文件夹而不是文件,它不是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:\Users\LOCAL_~1\AppData\Local\Temp\3"
$filter = "*.LOG"
$Watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
    IncludeSubdirectories = $false
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $Watcher 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 "Changed" -Action $changeAction
}

 Register-Watcher "c:\temp"

运行此代码后,更改C:\temp目录(或您指定的任何其他目录)。

After running this code, change any file in the "C:\temp" 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天全站免登陆