该脚本可以运行几天,然后我收到一条电子邮件,说它失败了,因为"找不到网络路径"。
当我手动运行它时效果很好,所以很难找到问题。
如何防止其丢失/丢失网络路径?任何改进或提示也欢迎。
我编写了此脚本以在共享驱动器上创建新文件夹,以便从SharePoint备份记帐日志。我有另一个脚本删除任何超过30天的文件夹。
这是我的第一个PowerShell脚本。
谢谢!
#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.'
}