创建驱动器和复制文件时找不到PowerShell 2.0网络路径

时间:2016-02-24 21:07:10

标签: powershell sharepoint powershell-v2.0 filepath unc

该脚本可以运行几天,然后我收到一条电子邮件,说它失败了,因为"找不到网络路径"。

当我手动运行它时效果很好,所以很难找到问题。

如何防止其丢失/丢失网络路径?任何改进或提示也欢迎。

我编写了此脚本以在共享驱动器上创建新文件夹,以便从SharePoint备份记帐日志。我有另一个脚本删除任何超过30天的文件夹。

  1. 该脚本将在共享驱动器\ sfserverspb4 \ accounting上创建一个包含今天日期的文件夹。
  2. 在' d'之间创建一个随机驱动器号的驱动器和' z'目前尚未使用。
  3. 将驱动器指向SharePoint路径\ intranet.mycompany.com \ dept
  4. 将所有文件,文件夹,子文件夹从SharePoint路径复制到今天创建的记帐共享文件夹。
  5. 完成后删除创建的驱动器。
  6. 这是我的第一个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.'
    
    }
    

0 个答案:

没有答案