首页 > 解决方案 > Powershell LocalBackup New-PSDrive 目标故障

问题描述

当我查看从 New-PSDrive 给出错误的日志文件时,我想从服务器备份到本地 bu。

LocalBackup 文件中的错误部分:

挂载备份源

New-PSDrive :指定的驱动器根“B”不存在,或者不是文件夹。

在 C:\Temp\trying2adm_trerulug\Untitled2.ps1:57 char:5 + New-PSDrive -Name $BkpSrc[0] -Root $BkpSrc[1] -PSProvider FileSys ... + ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ + CategoryInfo : ReadError: (S:PSDriveInfo) [New-PSDrive], IOException + FullyQualifiedErrorId : DriveRootError,Microsoft.PowerShell.Commands.NewPSDriveCommand

New-PSDrive : 指定的驱动器根 ":" 要么不存在,要么不是文件夹。在 C:\Temp\trying2adm_trerulug\Untitled2.ps1:57 char:5 + New-PSDrive -Name $BkpSrc[0] -Root $BkpSrc[1] -PSProvider FileSys ... + ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ + CategoryInfo : ReadError: (C:PSDriveInfo) [New-PSDrive], IOException + FullyQualifiedErrorId : DriveRootError,Microsoft.PowerShell.Commands.NewPSDriveCommand

挂载备份目标

New-PSDrive :指定的驱动器根“B”不存在,或者不是文件夹。在 C:\Temp\trying2adm_trerulug\Untitled2.ps1:62 char:5 + New-PSDrive -Name $BkpDst[0] -Root $BkpDst[1] -PSProvider FileSys ... + ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ + CategoryInfo : ReadError: (S:PSDriveInfo) [New-PSDrive], IOException + FullyQualifiedErrorId : DriveRootError,Microsoft.PowerShell.Commands.NewPSDriveCommand

New-PSDrive : 指定的驱动器根 ":" 要么不存在,要么不是文件夹。在 C:\Temp\trying2adm_trerulug\Untitled2.ps1:62 char:5 + New-PSDrive -Name $BkpDst[0] -Root $BkpDst[1] -PSProvider FileSys ... + ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ + CategoryInfo : ReadError: (C:PSDriveInfo) [New-PSDrive], IOException + FullyQualifiedErrorId : DriveRootError,Microsoft.PowerShell.Commands.NewPSDriveCommand

错误:未指定目标目录。

   Simple Usage :: ROBOCOPY source destination /MIR

         source :: Source Directory (drive:\path or \\server\share\path).
    destination :: Destination Dir  (drive:\path or \\server\share\path).
           /MIR :: Mirror a complete directory tree.

For more usage information run ROBOCOPY /?

**** /MIR 可以删除和复制文件!

**** /MIR 可以删除和复制文件!

卸载备份源

Remove-PSDrive : 找不到驱动器。名为“S”的驱动器不存在。在 C:\Temp\trying2adm_trerulug\Untitled2.ps1:81 char:5 + Remove-PSDrive -Name $BkpSrc[0] *>>$LogPath/$Logfile + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (S:String) [删除-PSDrive], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand

Remove-PSDrive:无法删除驱动器“C”,因为它正在使用中。在 C:\Temp\trying2adm_trerulug\Untitled2.ps1:81 char:5 + Remove-PSDrive -Name $BkpSrc[0] *>>$LogPath/$Logfile + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Remove-PSDrive ], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RemovePSDriveCommand

卸载备份源

Remove-PSDrive : 找不到驱动器。名为“S”的驱动器不存在。在 C:\Temp\trying2adm_trerulug\Untitled2.ps1:86 char:5 + Remove-PSDrive -Name $BkpDst[0] *>>$LogPath/$Logfile + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (S:String) [删除-PSDrive], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand

Remove-PSDrive:无法删除驱动器“C”,因为它正在使用中。在 C:\Temp\trying2adm_trerulug\Untitled2.ps1:86 char:5 + Remove-PSDrive -Name $BkpDst[0] *>>$LogPath/$Logfile + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Remove-PSDrive ], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RemovePSDriveCommand

我是从 Internet 搜索的系统的管理员,绕过执行策略和凭据,它看起来不错,但仍然无法正常工作。

代码部分:

 # @(DstName,Path)

 $BackupDestinations =   @(

 , @('Batch','\\srvr0001\TEMP\trying3')

 , @('test2','\\srvr0002\TEMP\trying2')
                          )
 # @(SrcName,Path,DstName)
 $BackupSources = @(
 , @('abcde','C:\Temp\ProjectCopy\Backup\Archives\Batch','Batch')
 , @('defgh','C:\Temp\Project - Copy\Backup\Archives\WinCCTag','test2')
                   )
 $LogPath = "C:\Temp\trying2adm_trerulug"

 $LogVersion = 50

 If (-Not (Test-Path $LogPath)){mkdir $LogPath}
 While ($LogVersion -gt 0)
 {
 $OldLogfile = "LocalBackup.$LogVersion.log"
 $Logfile = "LocalBackup.$($LogVersion-1).log"

 Copy-Item -Path $LogPath\$Logfile -Destination $LogPath\$OldLogFile -

 ErrorAction SilentlyContinue

 $LogVersion-- 

 }
 echo $null > $LogPath\$Logfile

 Function Write-Log{

 Param([string]$logstring)

 # Write-Host $LogPath\$LogFile

 Add-Content -Path $LogPath\$LogFile $logstring

 }

 $RoboArgs = 
 $("/E","/Z","/COPY:DAT","/R:3","/W:10","/XN","/XO","/XX","/NP")

 # Mount PSDrives
 ## Backup Sources
 Write-Log "### Mount Backup Sources ##########################"

 Foreach($BkpSrc in $BackupSources){

 New-PSDrive -Name $BkpSrc[0] -Root $BkpSrc[1] -PSProvider FileSystem 

 *>>$LogPath/$Logfile
 }
 ## Backup Destinations

 Write-Log "### Mounting Backup Destinations ##################"

 Foreach($BkpDst in $BackupDestinations){

 New-PSDrive -Name $BkpDst[0] -Root $BkpDst[1] -PSProvider FileSystem 

 *>>$LogPath/$Logfile
 }

 # Copy Backups

 Write-Log "### Start copyjobs ################################"

 Foreach($BkpSrc in $BackupSources){

 # Select right source directory

 Foreach($BkpDst in $BackupDestinations){

 if ($BkpDst[0] -eq $BkpSrc[2]){

 $BkpDstPath = $BkpDst[1]
 }
 }
 robocopy $BkpSrc[1] $BkpDstPath $RoboArgs *>>$LogPath/$Logfile
 }

 # Unmount the PSDrives

 ## Backup Sources

 Write-Log "### Unmount Backup Sources ########################"

 Foreach($BkpSrc in $Backupsources){

 Remove-PSDrive -Name $BkpSrc[0] *>>$LogPath/$Logfile

 }
 ## Backup Destinations

 Write-Log "### Unmount Backup Sources ########################"

 Foreach($BkpDst in $BackupDestinations){

 Remove-PSDrive -Name $BkpDst[0] *>>$LogPath/$Logfile

 }

标签: powershellloggingbackup

解决方案


您的问题是由您构建$BackupDestinations&$BackupSources收藏的方式引起的。你没有数组数组......你有一个字符串数组。循环$BckSrc[0]中当前项目的第一个字母也是如此。foreach[咧嘴笑]

要么切换到使用哈希表,要么使用,数组运算符强制将子数组添加为子数组而不是项目列表。

如果您想坚持使用数组,请使用类似这样的内容 [注意每个子数组之前的前导逗号] ...

$BackupSources = @(
    , @('abcde','C:\Temp\ProjectCopy\Backup\Archives\Batch','Batch')
    , @('defgh','C:\Temp\Project - Copy\Backup\Archives\WinCCTag','test2')
    )

使用以下代码,并且没有使用列表中名称的 PSDrive,并且已经创建了目标目录 --- 没有关于创建或删除有问题的 PSDrive 的错误消息。

$BackupDestinations =   @(
, @('Batch','\\srvr0001\TEMP\trying3')
, @('test2','\\srvr0002\TEMP\trying2')
                        )

$BackupSources = @(
, @('abcde','C:\Temp\ProjectCopy\Backup\Archives\Batch','Batch')
, @('defgh','C:\Temp\Project - Copy\Backup\Archives\WinCCTag','test2')
                )

Foreach ($BkpSrc in $BackupSources)
    {
    New-PSDrive -Name $BkpSrc[0] -Root $BkpSrc[1] -PSProvider FileSystem #-WhatIf
    }

Get-PSDrive | Out-Host

'=' * 20 | Out-Host

#<#
Foreach ($BkpSrc in $Backupsources)
    {
    Remove-PSDrive -Name $BkpSrc[0]
    pause
    }
Get-PSDrive
#>

所以......您的代码的哪一部分正在生成您在原始帖子中列出的错误?


推荐阅读