The script works for several days, and then I get an email message saying it failed because “The network path was not found”.
When I run it manually it works fine, so it’s hard to find the problem.
How do I keep it from dropping/losing the network path? Any improvements or tips are also welcome.
I have written this script to create a new folder on a shared drive in order to backup accounting logs from SharePoint. I have another script that deletes any of these folders that are older than 30 days.
- The script will create a folder with today’s date on the shared drive \sfserverspb4\accounting.
- Create a drive with a random drive letter between ‘d’ and ‘z’ not currently in use.
- Point the drive to the SharePoint path \intranet.mycompany.com\dept
- Copy all files, folders, sub-folders from the SharePoint path to the accounting shared folder created today.
- Remove the created drive when done.
This is my first PowerShell script.
Thank you!
#Copy po logs from SharePoint (intranet.mycompany.com) to sfserverspb4\accounting Try { Write-Host 'Creating New Folder' -fore black -back yellow $today_folder = New-Item -ItemType Directory -Path "\\sfserverspb4\accounting\Backups\PO_Log_Backups\$((Get-Date).ToString('yyyyMMdd'))" -ea stop Write-Host 'Copying Files...' -fore white -back blue $Drive = ls function:[d-z]: -n | ?{ !(test-path $_) } | random Write-Host "The drive is $drive" -fore green Net Use $Drive \\intranet.mycompany.com\dept /user:'corp\spadmin' 'spadminpassword' copy-item -Path $Drive\finance\Shared` Documents\PO` Logs\* -recurse -destination $today_folder -ErrorAction Stop Net Use $Drive /delete # disconnecting from intranet.mycompany.com } # If there's an error, stop, and send an email to IT Catch { Write-Host 'Error in function' -fore white -back red $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Send-MailMessage -From itdept@mycompany.com -To myself@mycompany.com -smtpServer "mail.prxy.com" -Subject "The script Copy of PO LOGS on server myserver FAILED!" -Body "Server myserver Scheduled Task 'Copy PO Logs' Failed. $FailedItem. The error message was: '$ErrorMessage'" } Finally { Write-Host 'All done.' }