监视远程PC上的文件夹以查找更改的文件 [英] Monitor folder on remote PC for changed files

查看:129
本文介绍了监视远程PC上的文件夹以查找更改的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络中有一台PC上的测量设备。 PC未加入域,但是有一个共享文件夹,我拥有一个用户名和密码。

I have a measuring device on a PC in our network. The PC is not joined to the domain, however has a shared folder for which I have a username and password.

我正在尝试构建Powershell脚本来监视此文件夹来自远程服务器,对于新的CSV文件,脚本会将文件复制到指定的文件夹位置。我正在努力将参数传递给FileSystemWatcher cmdlet。

I am attempting to build a Powershell script to monitor this folder from a remote server, for new CSV files, the script will then copy the file to a specified folder location. I am struggling to pass through the parameters to the FileSystemWatcher cmdlet.

有什么想法吗?

$folder = '\\remoteip\folder\subfolder'# <--'<full path to the folder to watch>'
$filter = '*.csv'
$destination = '\\mynetworkstorage\folder\' # <--' Where is the file going?

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $true              # <-- set this according to your requirements
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
} 
$onCreated = Register-ObjectEvent $fsw 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"
    Copy-Item $path -Destination $destination -Force -Verbose
}

EDIT -
该脚本将从加入我们网域的服务器上运行,因此需要将凭据传递到该文件夹​​,以便我可以访问它。我有这些凭据。

EDIT - The script will be run from a server joined to our domain, so there is a need to pass through credentials to the folder in order that I can access it. I have these credentials.

推荐答案

我对代码进行了一些重构(主要是为了让我更容易理解):

I refactored the code a little (mainly so I could more easily understand it):

$folder = '\\localhost\c$\tmp'
$filter = '*.*'
$destination = '\\localhost\c$\tmp_destination\' 

$fsw = New-Object IO.FileSystemWatcher
$fsw.Path = $folder
$fsw.Filter = $filter
$fsw.IncludeSubdirectories = $true
$fsw.EnableRaisingEvents = $true  
$fsw.NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'

$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"
    Copy-Item $path -Destination $destination -Force -Verbose    
}  

$created = Register-ObjectEvent $fsw Created -Action $action

while ($true) {sleep 1}

我在本地运行此代码,并设法在<$ c中创建了文件$ c> $ folder 自动复制到 $ destination

I ran this code locally and managed to get files created in $folder automatically copied to $destination.

在FileSystemWatcher中运行当前用户的上下文。如果要访问需要登录的远程系统,则需要模拟。

FileSystemWatcher run in the context of the current user. In the case of accessing remote systems that require login, impersonation is required.

如果远程系统仅具有本地用户,则源系统无法以该用户身份登录的域用户,则似乎无法进行模拟。

If the remote system only got local users, no domain user that the source system can log on as, then impersonation does not seem to be possible.

请参见链接以获取有关模拟的更多详细信息。

See this and this link for more details on impersonation.

这篇关于监视远程PC上的文件夹以查找更改的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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