Powershell System.IO.FileSystemWatcher copy-item

时间:2017-01-28 15:22:16

标签: powershell

copy-item无效。当我替换像Send-MailMessage这样的东西时,这似乎是在我将文件放入" \ sharesrc \ test"但副本项目部分没有。

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "\\pc\sharesrc\test"
$watcher.Filter = "*.*"

$Src = "\\pc\sharesrc\test"
$Dst = "\\pc\sharedst\test"

$action = { copy-item $Src\*.* $Dst }

Register-ObjectEvent $watcher "Created" -Action $action

while ($true) {sleep 5}

2 个答案:

答案 0 :(得分:0)

声明并引用src和dst变量作为全局变量,例如:

$Global:src = 'xxxx';
$Global:dst = 'xxxx';

copy-item $global:src\*.* $global:dst;

答案 1 :(得分:0)

使用您的代码每次都复制所有文件,如果您只想复制创建的文件,可以执行此操作,并同时解决您的问题:

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "\\pc\sharesrc\test"
$watcher.Filter = "*.*"    

$action = {
$path = $Event.SourceEventArgs.FullPath ; 
copy-item $path "\\pc\sharedst\test" 
}

Register-ObjectEvent $watcher "Created" -Action $action

while ($true) {sleep 5}

Unregister-Event $watcher