[英]Monitor folder on remote PC for changed files
我的網絡中有一台PC上的測量設備。 PC未加入域,但是具有我具有用戶名和密碼的共享文件夾。
我正在嘗試構建Powershell腳本以從遠程服務器監視此文件夾,對於新的CSV文件,該腳本會將文件復制到指定的文件夾位置。 我正在努力將參數傳遞給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
}
編輯 -該腳本將從加入我們域的服務器上運行,因此需要將憑據傳遞到該文件夾,以便我可以訪問它。 我有這些憑證。
我對代碼進行了一些重構(主要是為了讓我更容易理解):
$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}
我在本地運行此代碼,並設法將在$folder
創建的$folder
自動復制到$destination
。
FileSystemWatcher在當前用戶的上下文中運行。 在訪問需要登錄的遠程系統的情況下,需要模擬。
如果遠程系統僅具有本地用戶,則源系統無法以其身份登錄任何域用戶,那么似乎無法進行模擬。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.