PowerShell FSW脚本无法正常运行

时间:2016-01-15 14:50:57

标签: .net powershell

我正在尝试监控用户将文件夹复制到公共空间的文件夹。复制文件夹后,脚本应该创建一个目标文件夹,并且对于源文件夹中的每个文件,执行一个传递了两个变量的批处理文件;第一个是文件名,第二个是目标文件夹。处理完所有文件后,脚本应将源文件夹移动到存档,并使用添加到名称的“_Archive”文本对其进行重命名。

我无法正确执行脚本的任何部分。当我检查作业状态时,它显示为失败。如果我停止工作并将其删除,我无法取消注册该事件。任何帮助将不胜感激。

以下是剧本:

$inputFolder = 'c:\cat2proe\convertSource'      # Enter the root path of the   source folder.
$filter = '*.*'                                 # You can enter a wildcard filter here.
$outputFolder = 'c:\cat2proe\convertDest'       # Enter the root path of the destination folder
$storageFolder = 'c:\cat2proe\Archive'          # Enter the root path of the archive folder

$fsw = New-Object IO.FileSystemWatcher $inputFolder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'DirectoryName'}

Register-ObjectEvent $fsw Created -SourceIdentifier FolderCreated -Action {
$convFolder = $Event.SourceEventArgs.Name
$fpath = $Event.SourceEventArgs.FullPath
$workFolder= $inputFolder + "\" + $convFolder
$targetFolder = $outputFolder + "\" + $convFolder + "_converted"
$archiveFolder = $storageFolder + "\" + $convFolder + "_archived"
$files = Get-ChildItem $workFolder
New-Item -Path $targetFolder -ItemType directory -Force
New-Item -Path $targetFolder + "\log" -ItemType directory -Force
ForEach ($file in $files) { 
catia5batch.bat $file.fullName $targetFolder | Out-File -FilePath $targetFolder + "\log\conversionLog.log" -Append -InputObject log "$($_.BaseName)$(get-date -Format MM-dd-yyyy)$($_.extension) has been converted successfully."}
Move-Item -path $workFolder -destination $archiveFolder -Force
}

0 个答案:

没有答案