我只想獲取 pdf 和 rtf 文件,但在回顯變數時得到“true”結果
$watcher = New-Object System.IO.FileSystemWatcher
Write-Host "First line of the script, getting the watcher folder " $watcher -ForegroundColor white
$watcher.Path = "C:\Users\Demo Site"
Write-Host "This is the current path " $watcher.Path -ForegroundColor white
$watcher.Filter = "*.rtf" -or "*.pdf"
Write-Host "Filtering by: " $watcher.Filter -ForegroundColor white
$watcher.IncludeSubdirectories = $true
Write-Host "Including Subdirectories" -ForegroundColor white
$watcher.EnableRaisingEvents = $true
$action = {
$path = $Event.SourceEventArgs.FullPath
$path2 = $path.replace("C:\Users\", "")
$path2 = $path2.replace("\","/")
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date -Format F) , File was $changeType in: $path"
$logFile = "D:\MoveLog.txt"
Add-content -Path $logFile -value $logline
# moves the file captured by the watcher
Start-Sleep -s 2
Move-PnPFile -ServerRelativeUrl "/sites/1/$path2"
-TargetUrl "/sites/2" -OverwriteIfAlreadyExists
-Force -Confirm:$True
-ErrorAction SilentlyContinue -ErrorVariable ProcessError
# added to make sure that the larger files finish uploading
If ($ProcessError)
{
Write-Warning -Message "File not copied!"
Write-Warning -Message "Waiting 5 seconds and trying again"
Start-Sleep -s 5
$logFile = "D:\MoveLog.txt"
Add-content -Path $logFile -value " $(Get-Date -Format F), Error while Copying file - Let's try this again!"
Move-PnPFile -ServerRelativeUrl "/sites/1/$path2"
-TargetUrl "/sites/1/$path2" -Force -Confirm:$False
-ErrorAction SilentlyContinue -ErrorVariable ProcessError
If ($ProcessError)
{
Write-Warning -Message "File not copied!"
Write-Warning -Message $error[0].Exception.Message
$errorLog = " $(Get-Date -Format F), $error[0].Exception.Message"
Write-Host "12. Writing to Log" -ForegroundColor red
$logFile = "D:\MoveLog.txt"
Add-content -Path $logFile -value $errorLog
$to_mail = ""
$fr_mail = ""
$subject = "An error has occured when moving in SharePoint"
$serverDetails = ""
$port =
Send-MailMessage -To $to_mail -From $fr_mail -Subject $subject -Body $errorLog
-Priority High -SmtpServer $serverDetails -Port $port
}
else
{
Write-Host "n 8. After 1 error, Succesfully moved newly created file to different sharepoint library" -ForegroundColor green
}
}
else
{
Write-Host "n 8. Successfully moved newly created file to different sharepoint library with no errors" -ForegroundColor green
}
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
# Register-ObjectEvent $watcher "Changed" -Action $action
while($true){
sleep 5
}
如何更改$watcher.Filter = "*.rtf" -or "*.pdf"
為只獲取 2 個檔案?
當我拾取所有檔案副檔名 .tmp 時,它也會被拾取,但當我對其執行某些操作時,它會被刪除,因此會產生 FAIL 訊息。
我也嘗試過dir .\* -include ('*.xsl', '*.xslt') -recurse
我沒有收到錯誤,但是當我$watcher.Filter
用 2 個擴充功能來回顯時,我得到了true
答案1
經過幾次錯誤後我設法讓它工作,
我建立此腳本是為了在 SharePoint 庫之間移動文件,以便文件不會佔用伺服器上的空間。
基本上:
透過 ERP 將檔案寫入伺服器上同步的 OneDrive 資料夾
被 FileSystemWatcher 拾取
移至存檔庫,用戶可以在其中存取它。
你會需要SharePointPnP
Connect-PnpOnline -Url https://site.sharepoint.com/sites/ -UseWebLogin
#disconects from Sharepoint
#disconnect-PnpOnline
$logFile = "D:\SharePointMoveLog.txt"
#for debugging
start-transcript
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
Write-Host "First line of the script, getting the watcher folder " $watcher -ForegroundColor white
$watcher.Path = "C:\Users\Demo Site - Staging"
Write-Host "This is the current path " $watcher.Path -ForegroundColor white
$watcher.Filter = "*.*"
Write-Host "Filtering by: " $watcher.Filter -ForegroundColor white
$watcher.IncludeSubdirectories = $true
Write-Host "Including Subdirectories" -ForegroundColor white
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action =
{
$fileName = Split-Path $Event.SourceEventArgs.FullPath -leaf
Write-Host $fileName
If ($fileName -like '*.pdf')
{
$path = $Event.SourceEventArgs.FullPath
###PDF
Write-Host "1. New File has been Created in: " $path -ForegroundColor green
$path2 = $path.replace("C:\Users\Demo Site - Staging\", "")
Write-Host "`n 2. Removing the begining of directory from Path: " $path2 -ForegroundColor green
$path2 = $path2.replace("\","/")
Write-Host "`n 3. Replacing \ with / in order to get file to copy to SP: " $path2 -ForegroundColor green
$changeType = $Event.SourceEventArgs.ChangeType
Write-Host "`n 4. File was changed - Type: " $changeType -ForegroundColor green
$logline = "$(Get-Date -Format F) , `r`n File was $changeType in: `r`n $path"
Write-Host "`n 5. Creating a line for the logs: " $logline -ForegroundColor green
$logFile = "D:\SharePointMoveLog.txt"
Add-content -Path $logFile -value $logline
Write-Host "`n New line added to the log ${logline}" -ForegroundColor green
#moves the file captured by the watcher
Write-Host "`n 6. Waiting 2 seconds before moving to ensure file uploads succesfuly" -ForegroundColor green
Start-Sleep -s 2
Move-PnPFile -ServerRelativeUrl "/sites/test1/Staging/$path2" `
-TargetUrl "/sites/test1/Archive/$path2" -OverwriteIfAlreadyExists `
-Force -Confirm:$True `
-ErrorAction SilentlyContinue -ErrorVariable ProcessError
#added to make sure that the larger files finish uploading
If ($ProcessError)
{
Write-Warning -Message "File not copied!"
Write-Warning -Message "Waiting 5 seconds and trying again"
Start-Sleep -s 5
$logFile = "D:\SharePointMoveLog.txt"
Add-content -Path $logFile -value "`r`n $(Get-Date -Format F), `r`n Error while Copying file - Let's try this again!"
Move-PnPFile -ServerRelativeUrl "/sites/test1/Staging/$path2" `
-TargetUrl "/sites/test1/Archive/$path2" -Force -Confirm:$False `
-ErrorAction SilentlyContinue -ErrorVariable ProcessError
If ($ProcessError)
{
Write-Warning -Message "File not copied!"
Write-Warning -Message $error[0].Exception.Message
$errorLog = "`r`n $(Get-Date -Format F), `r`n $error[0].Exception.Message"
Write-Host "12. Writing to Log" -ForegroundColor red
$logFile = "D:\SharePointMoveLog.txt"
Add-content -Path $logFile -value $errorLog
$to_mail = ""
$fr_mail = ""
$subject = "An error has occured when moving in SharePoint Test Site"
$serverDetails = ""
$port =
Send-MailMessage -To $to_mail -From $fr_mail -Subject $subject -Body $errorLog `
-Priority High -SmtpServer $serverDetails -Port $port
}
else
{
Write-Host "`n 7. After 1 error, Succesfully moved newly created file to different sharepoint library" -ForegroundColor green
$logFile = "D:\SharePointMoveLog.txt"
Add-content -Path $logFile -value "`r`n $(Get-Date -Format F), `r`n After 1 error, Succesfully moved newly created file to different sharepoint library"
}
}
else
{
Write-Host "`n 7. Succesfully moved newly created file to different sharepoint library with no errors" -ForegroundColor green
$logFile = "D:\SharePointMoveLog.txt"
Add-content -Path $logFile -value "`r`n $(Get-Date -Format F), `r`n Succesfully moved newly created file to different sharepoint library with no errors"
}
}
elseif ($fileName -like '*.rtf')
{
###RTF
$path = $Event.SourceEventArgs.FullPath
Write-Host "1. New File has been Created in: " $path -ForegroundColor green
$path2 = $path.replace("C:\Users\Demo Site - Staging\", "")
Write-Host "`n 2. Removing the begining of directory from Path: " $path2 -ForegroundColor green
$path2 = $path2.replace("\","/")
Write-Host "`n 3. Replacing \ with / in order to get file to copy to SP: " $path2 -ForegroundColor green
$changeType = $Event.SourceEventArgs.ChangeType
Write-Host "`n 4. File was changed - Type: " $changeType -ForegroundColor green
$logline = "$(Get-Date -Format F) , `r`n File was $changeType in: `r`n $path"
Write-Host "`n 5. Creating a line for the logs: " $logline -ForegroundColor green
$logFile = "D:\SharePointMoveLog.txt"
Add-content -Path $logFile -value $logline
Write-Host "`n New line added to the log ${logline}" -ForegroundColor green
#moves the file captured by the watcher
Write-Host "`n 6. Waiting 2 seconds before moving to ensure file uploads succesfuly" -ForegroundColor green
Start-Sleep -s 2
Move-PnPFile -ServerRelativeUrl "/sites/test1/Staging/$path2" `
-TargetUrl "/sites/test1/Archive/$path2" -OverwriteIfAlreadyExists `
-Force -Confirm:$True `
-ErrorAction SilentlyContinue -ErrorVariable ProcessError
#added to make sure that the larger files finish uploading
If ($ProcessError)
{
Write-Warning -Message "File not copied!"
Write-Warning -Message "Waiting 5 seconds and trying again"
Start-Sleep -s 5
$logFile = "D:\ManagementGateway\SharePointLogs\USA\SharePointMoveLog.txt"
Add-content -Path $logFile -value "`r`n $(Get-Date -Format F), `r`n Error while Copying file - Let's try this again!"
Move-PnPFile -ServerRelativeUrl "/sites/test1/Staging/$path2" `
-TargetUrl "/sites/test1/Archive/$path2" -Force -Confirm:$False `
-ErrorAction SilentlyContinue -ErrorVariable ProcessError
If ($ProcessError)
{
Write-Warning -Message "File not copied!"
Write-Warning -Message $error[0].Exception.Message
$errorLog = "`r`n $(Get-Date -Format F), `r`n $error[0].Exception.Message"
Write-Host "12. Writing to Log" -ForegroundColor red
$logFile = "D:\SharePointMoveLog.txt"
Add-content -Path $logFile -value $errorLog
$to_mail = ""
$fr_mail = ""
$subject = "An error has occured when moving in SharePoint"
$serverDetails = ""
$port =
Send-MailMessage -To $to_mail -From $fr_mail -Subject $subject -Body $errorLog `
-Priority High -SmtpServer $serverDetails -Port $port
}
else
{
Write-Host "`n 7. After 1 error, Succesfully moved newly created file to different sharepoint library" -ForegroundColor green
$logFile = "D:\SharePointMoveLog.txt"
Add-content -Path $logFile -value "`r`n $(Get-Date -Format F), `r`n After 1 error, Succesfully moved newly created file to different sharepoint library"
}
}
else
{
Write-Host "`n 7. Succesfully moved newly created file to different sharepoint library with no errors" -ForegroundColor green
$logFile = "D:\SharePointMoveLog.txt"
Add-content -Path $logFile -value "`r`n $(Get-Date -Format F), `r`n Succesfully moved newly created file to different sharepoint library with no errors"
}
}
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
# Register-ObjectEvent $watcher "Changed" -Action $action
while ($true) {sleep 5}
目前,有時會出現一個小錯誤,我只看到它在 RTF 上失敗,並出現錯誤:
Server relative urls must start with SPWeb.ServerRelativeUrl Cannot open "Doc12345.rtf": no such file or folder.
我不清楚為什麼會發生此錯誤,因為當我手動放入文件時它就會起作用。
我之前遇到過類似的錯誤,但它是由 SharePoint 庫中不同的資料夾結構引起的,確保它們完全相同可以解決此問題。