Author Posts

February 25, 2016 at 10:23 am

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.

1. The script will create a folder with today's date on the shared drive \\sfserverspb4\accounting.
2. Create a drive with a random drive letter between 'd' and 'z' not currently in use.
3. Point the drive to the SharePoint path \\intranet.mycompany.com\dept
4. Copy all files, folders, sub-folders from the SharePoint path to the accounting shared folder created today.
5. 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.'

}

February 25, 2016 at 1:06 pm

Since this is a sporadic issue, you might have some trouble finding the cause. My first thoughts are your session has been active too long (in a DC env). Or your SMB server is just a bit laggy. This is a best guess (this is my first reply helping someone, and your first question)

It's possible that the SMB connection just needs a good old kick in the pants. Try this:

Try {
Write-Host 'Opening connection...' -fore black -back yellow
$smbtest = $null
$sleep = 2
$imax = 5
for ($i = 1; (($i -lt $imax) -and ($smbtest -eq $null)); $i++)
{ 
$smbtest = Get-Item "\\sfserverspb4\accounting\Backups\PO_Log_Backups" -ea 0
sleep $sleep 
} # loop max runtime is: $i(max) * $sleep
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
#...the rest of your script.  

Or you can use a nested try-catch and stop instead of 0 at the end of the get-item line to handle any errors that might come up.