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에 의해 선택됨
사용자가 접근할 수 있는 Archive 라이브러리로 이동하세요.
필요할 것이예요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 라이브러리의 다른 폴더 구조로 인해 발생했으며 이 문제가 정확히 동일하게 해결되었는지 확인했습니다.