Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell中的复制无法正常工作_Powershell_Copy Item - Fatal编程技术网

powershell中的复制无法正常工作

powershell中的复制无法正常工作,powershell,copy-item,Powershell,Copy Item,我有下面的powershell脚本,它不像我想象的那样工作。我想将源文件夹$folder中新创建的文件复制到目标文件夹$DestFolder,但未复制任何文件。有人知道可能出了什么问题吗 $SourceFolder = 'd:\temp\' # Enter the root path you want to monitor. $folder = 'd:\temp' # Enter the root path you want to monitor. $Destfolder = 'd:\temp2

我有下面的powershell脚本,它不像我想象的那样工作。我想将源文件夹$folder中新创建的文件复制到目标文件夹$DestFolder,但未复制任何文件。有人知道可能出了什么问题吗

$SourceFolder = 'd:\temp\' # Enter the root path you want to monitor.
$folder = 'd:\temp' # Enter the root path you want to monitor.
$Destfolder = 'd:\temp2\' # Enter the root path you want to monitor.
$global:MySourceFolder = 'd:\temp\' # Enter the root path you want to monitor.
$global:MyDestfolder = 'd:\temp2\' # Enter the root path you want to monitor.
$filter = '*.*'  # You can enter a wildcard filter here.

# In the following line, you can change 'IncludeSubdirectories to $true if required.                          
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}


Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
#Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
Write-Host "path :  $MyDestfolder$name" -fore green
Copy-Item (Join-Path $MySourceFolder $name) ($Destfolder)
Out-File -FilePath d:\temp\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"}

我相信它正在按预期工作。如果副本实际上是动作的一部分,请尝试在动作脚本块内移动该行

变量$name在action scriptblock之外为空,因为ObjectEvent有自己的作用域。如果需要从Action scriptblock外部访问此变量,可以使用$global:name声明该变量

下面的代码应该可以用它替换,但是要保存一份代码副本,这样您就可以先进行备份

$folder = 'd:\temp'
$Destfolder = 'd:\temp2'
$filter = '*.*'

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
    IncludeSubdirectories = $false
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated

    Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
    Copy-Item -Path (Join-Path $folder $name) -Destination $Destfolder
    Out-File -FilePath "d:\temp\filechange\outlog.txt" -Append -InputObject "The file '$name' was $changeType at $timeStamp"
}

谢谢,我错过了动作部分,但在我把它放在动作中并尝试了这个。。。写入主机路径:$folder$name-fore green我可以看到实际的$folder路径由于某种原因是空的,这一定是复制项无法工作的原因,但我不知道为什么..这对我来说毫无意义。。。如果没有正确的路径,您将无法创建filesystemwatcher对象。顺便说一句,要获得写入主机中的预期路径,应在变量之间添加一个\ n;像这个$folder\$name我改成了这个写主机路径:$folder\$name-fore green和那个写出来的路径:\filename.jpg,奇怪……我会关闭/重新打开PowerShell并从一开始就运行整个过程,以确保你能真正创建FileSystemWatcher对象。我在对脚本进行任何更改之前都会这样做,我还可以看到,当我创建一个新文件时,这个更改会反映在d:\temp\filechange\outlog.txt文件中。这一定意味着FileSystemWatcher对象可以工作,对吗?