Powershell LocalBackup New-PSDrive目标故障

时间:2019-04-26 07:47:31

标签: powershell logging backup

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

LocalBackup文件中的错误部分:

挂载备份源

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

在C:\ Temp \ trying2adm_trerulug \ Untitled2.ps1:57 char:5 + New-PSDrive-名称$ BkpSrc [0]-根$ 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-名称$ BkpSrc [0]-根$ 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-名称$ BkpDst [0]-根$ 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-名称$ BkpDst [0]-根$ 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-名称$ BkpSrc [0] * >> $ LogPath / $ Logfile + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~     + CategoryInfo:ObjectNotFound:(S:字符串)[Remove-PSDrive],DriveNotFoundException     + FullyQualifiedErrorId:DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand

Remove-PSDrive:因为正在使用驱动器'C',所以无法删除它。 在C:\ Temp \ trying2adm_trerulug \ Untitled2.ps1:81 char:5 + Remove-PSDrive-名称$ 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-名称$ BkpDst [0] * >> $ LogPath / $ Logfile + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~     + CategoryInfo:ObjectNotFound:(S:字符串)[Remove-PSDrive],DriveNotFoundException     + FullyQualifiedErrorId:DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand

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

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

代码部分:

 # @(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

 }

1 个答案:

答案 0 :(得分:0)

您的问题是由您构建$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
#>

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