Hello experts,I found the promising script below at the following link:[b][url=https://sqlactions.com/2015/03/30/powershell-script-to-manipulate-sql-server-backup-files/]https://sqlactions.com/2015/03/30/powershell-script-to-manipulate-sql-server-backup-files/[/url][u][/u][/b]The purpose of the script is to allow copy of backup files from one SQL Server to another assuming the backups are made by the Ola Hallengren backup tool. The script text below is a little different from what is at the link above because I've been attempting to troubleshoot it for my system. However, I ran into the 3 errors here and was wondering if anyone can help me decipher and fix them:[code](1)The job script encountered the following errors. These errors did not stop the script:A job step received an error at line 45 in a PowerShell script. The corresponding line is ' New-Item -ItemType Dir -Path $PathToCopy -Force'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'SQL Server PowerShell provider error: Object does not support this operation.'(2)A job step received an error at line 48 in a PowerShell script. The corresponding line is ' Copy-Item $FileToCopy $PathToCopy'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'SQL Server PowerShell provider error: Copy-Item is not supported.'(3)A job step received an error at line 52 in a PowerShell script. The corresponding line is ' $RenamedFile = ($DestinationFile.substring(0,$DestinationFile.length-20))+'.bak''. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero.Parameter name: length"'[/code][code="other"]<################################################################################# Script Name: CopyLatestBackupandRename.ps1 Author : Prashant Kumar Date : March 29th, 2015 Description: The script is useful for those using Ola Hallengren's backup solution. This script takes SQL Server full backup parent folder as an input, a remote UNC path as another input and copies the latest backup file for each database, renames the backup file to the remote UNC path. This Sample Code is provided for the purpose of illustration only and is notintended to be used in a production environment. THIS SAMPLE CODE AND ANYRELATED INFORMATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHEREXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OFMERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.##################################################################################> #Specify Parent folder where Full backup files are originally being taken$SourcePath = 'D:\SQLBackup\InstanceName' #Specify UNC path ot network share where backup files has to be copied$UNCpath = '\\RemoteServer\UNCBackup' #Browse thru subfolders (identical to database names) inside $SourcePath$SubDirs = dir $SourcePath -Recurse | Where-Object {$_.PSIsContainer} | ForEach-Object -Process {$_.FullName} #Browse through each sub-drorectory inside parent folderForEach ($Dirs in $SubDirs) { #List recent file (only one) within sub-directories $RecentFile = dir $Dirs | Where-Object {!$_.PSIsContainer} | Sort-Object {$_.LastWriteTime} -Descending | Select-Object -First 1 #Perform operation on each file (listed above) one-by-one ForEach ($File in $RecentFile) { $FilePath = $File.DirectoryName $FileName = $File.Name $FileToCopy=$FilePath+'\'+$FileName $PathToCopy=($filepath -replace [regex]::Escape($SourcePath), $UNCpath)+'\' #Forecfully create the desired directory structure at destination if one doesn't exist New-Item -ItemType Dir -Path $PathToCopy -Force #Copy the backup file Copy-Item $FileToCopy $PathToCopy #Trim the date time from the copied file name, store in a variable $DestinationFile = $PathToCopy+$FileName $RenamedFile = ($DestinationFile.substring(0,$DestinationFile.length–20))+'.bak' #Rename the copied file Rename-Item $DestinationFile $RenamedFile } }[/code]Thanks for any help!!- webrunner
↧