PowerShell script exchange
Post Reply
menthol
Influencer
Posts: 12
Liked: 2 times
Joined: Jan 24, 2014 10:02 am
Full Name: Brad Wyatt
Contact:

v8 Broke my Powershell Script

Post by menthol »

Hi Guys

Hopefully you can help me with this...

I have a script in PS that runs after the full backup job that did the following
  1. Selects & inventories the tape drive
  2. Marks the inserted tape as Free
  3. Selects latest full backup from the backup repository (finds *.vbk files and selects the latest one)
  4. Creates a hardlink for this backup file
  5. Starts the duplication of the file to tape
  6. Ejects the tape
  7. Removes the hardlink
  8. Sends an email report
However the CmdLets appear to have changed so now it doesn't work. The inventorying of the tape drive, selecting of the tape and marking it as free do not seem to work.

Could anyone assist me with this?

Code: Select all

############################################################
### Powershell Script for Automating Backup Duplication    #
### Version: 1                                             #
### Date: 17/06/14                                         #
############################################################

# Initial Setup

Remove-Variable -Name * -Scope script -ErrorAction SilentlyContinue
$Error.clear()
$ErrorActionPreference = "Stop"

# Time & Logging

$logger = New-Object System.Text.StringBuilder
$logger.Length = 0

function Log([string]$Message)
    {
        $LogTime = Get-Date -Format "dd-MM-yyyy HH:mm:ss"
        #$logger.Append($LogTime)
        $logger.AppendLine($LogTime + " " + $Message)
    }


Log "Starting script"

# SMTP
$smtpServer = "server"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

# Email Reporting
$emailFrom = "xxx"
$emailTo = "xxxx"
$subject_success = "[SUCCESS] SRV Backup Files Replicated to Tape"
$subject_failure = "[FAILURE] SRV Backup Files NOT Replicated to Tape"

# Let's set our locations
$SRV80_b2d = "D:\Veeam\Backups\Backup Job\"

# GET Veeam Module for Commands!

try
{
    Log "Attempting to add the Veeam Snapin so commands can be executed"
    Add-PSSnapin VeeamPSSnapin
    Log "Successfully added the Veeam Snapin"
}
catch
{
    Log "Error while attempting to add the Veeam Snapin: $error"
    Log "Script execution FAILED"
    $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
    exit
}


##################################################################
######################## BEGIN PROCESSES #########################
##################################################################

## INVENTORY THE TAPE ##

try
{
    Log "Attempting to enumerate & inventory the tape drive"
    Get-VBRTapeDrive | Start-VBRTapeInventory
    Log "Successfully inventory'd the tape drive"
}
catch
{
    Log "Error while attempting to inventory the tape drive: $error"
    Log "Script execution FAILED"
    $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
    exit
}



## SELECT THE TAPE ##

$tape = Get-VBRTapeMedium | Where-Object {$_.IsOnline -like "True"}



## MARK TAPE AS FREE ##

try
{
    Log "Attempting to mark $tape as free"
    $tape.MarkAsFree()
    Log "Successfully marked $tape as free"
}
catch
{
    Log "Error while attempting to mark $tape as free: $error"
    Log "Script execution FAILED"
    $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
    exit
}


## LATEST BACKUP SELECTION ## 
# Time to get our latest full backup file and store it into a variable, ready for duplication
# It's a bit roundabout at present because the Add-VBRTapeFilesJob cmdlet can't handle files properly


Log "Finding latest full backup of SRV80"
$linkpath = "D:\Veeam\Backups\HardLinks\"
$SRV80_b2d_files = dir $SRV80_b2d -Filter "*.vbk"
$SRV80_Latest = $SRV80_b2d_files | group directory | foreach {@($_.group | sort {[datetime]$_.creationtime} -desc)[0]}

try
{
    Log "Selecting latest full backup of SRV80"
    $FilePath = $SRV80_Latest.DirectoryName
    $FileName = $SRV80_Latest.Name
    $SRV80Link = "LatestSRV80VBK.vbk"
    cmd /c mklink /H $linkpath$SRV80Link $Filepath\$Filename
}
catch
{
    Log "Error while selecting latest full backup: $error"
    Log "Script execution FAILED"
    $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
    exit
}

# Sleep for 10 seconds while Veeam releases its hold on the above hardlink

Start-Sleep -s 10

## NOT NEEDED
#try
#{
#    Log "Creating job for duplicating $SRV60_Latest")
#    #Add-VBRTapeFilesJob -Name "SRV60 to Tape" -Server "This server" -Path $linkpath$Filename -MediaPool "DSG Tape Media" -Masks vbk
#    Log "Successfully created job")
#}
#catch
#{
#    Log "Error while creating duplication job: $error")
#    Log "Script execution FAILED")
#    $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
#    exit
#}



################
## START JOB! ##
################


try
{
    Log "Starting job to duplicate $SRV80_Latest"
    Start-VBRJob -Job "SRV80 to Tape"
    Log "Successfully duplicated $SRV80_Latest to tape"
    $jobDuplicated = 1
    Log "Ejecting the tape"
    Get-VBRTapeDrive | Eject-VBRTapeDrive
    Log "Successfully ejected tape"
}
catch
{
    $jobDuplicated = 0
    Log "Error while duplicating: $error"
    Log "Script execution FAILED"
    $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
    exit
}


## NOT NEEDED
#try
#{
#    Log "Removing the SRV60 to Tape Job")
#    #Remove-VBRJob -Job "SRV60 to Tape" -Confirm:$false
#    Log "Successfully removed job")
#}
#catch
#{
#    Log "Error while removing job: $error")
#    $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
#    exit
#}


## REMOVE HARD LINKS ##

try
{
    Log "Removing the symbolic link to the backup file"
    cmd.exe /c del $linkpath$SRV80Link
    Log "Successfully removed the symbolic link to the backup file"
}
catch
{
    Log "Error while removing symbolic link: $error"
    $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
    exit
}  


# Let's wrap it up and email our report
# SRV80 will be in a different script
# EMAIL RESULTS #

if ($error.count -le 0)
        {
            if ($jobDuplicated = 1)
                {
                    Log "SRV80 backup files are on tape, yay!"
                    Log "Script executed successfully"
                    $smtp.Send($emailFrom, $emailTo, $subject_success, $logger.ToString())
                }
        }
else
        {
        Log "Script execution Error"
        $smtp.Send($emailFrom, $emailTo, $subject_failure, $logger.ToString())
        }
THANK YOU!
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: v8 Broke my Powershell Script

Post by veremin »

Let's try the following:

Inventory:

Code: Select all

$Drive = Get-VBRTapeDrive -name "Name of Drive"
Start-VBRTapeInventory -Library $Drive
Select tape:

Code: Select all

$Medium = Get-VBRTapeMedium | where {$_.location -like "Drive"}
Mark as free:

Code: Select all

$MediaPool = Get-VBRTapeMediaPool -Name "Free"
Move-VBRTapeMedium -Medium $Medium -MediaPool $MediaPool
As to the usage of hardlinks, please be aware that with version 8 revised tape PS model, it's now possible to change "source" settings of files to tape job. So, there is no need to use complex "hardlink" approach any longer.

Code: Select all

Set-VBRFileToTapeJob -Job $Job -Object <Object>
Thanks.
menthol
Influencer
Posts: 12
Liked: 2 times
Joined: Jan 24, 2014 10:02 am
Full Name: Brad Wyatt
Contact:

Re: v8 Broke my Powershell Script

Post by menthol »

Thanks for such a detailed answer.

I've tried the inventory script but it errors with:

Code: Select all

Start-VBRTapeInventory : Cannot bind parameter 'Library'. Cannot convert the "Tape0" value of type "Veeam.Backup.PowerShell.Infos.VBRTapeDrive" to type 
"Veeam.Backup.PowerShell.Infos.VBRTapeLibrary".
At line:2 char:37
+     Start-VBRTapeInventory -Library $Drive
+                                     ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-VBRTapeInventory], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Veeam.Backup.PowerShell.Cmdlets.StartVBRTapeInventory
Any ideas?

I haven't tried the other code snippets yet since they depend on each other working.

Thanks again
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: v8 Broke my Powershell Script

Post by veremin »

What if you try to use Get-VBRLibary commandlet, will the corresponding drive be listed?

Code: Select all

Get-VBRLibrary -name "Name of your drive"
Thanks.
menthol
Influencer
Posts: 12
Liked: 2 times
Joined: Jan 24, 2014 10:02 am
Full Name: Brad Wyatt
Contact:

Re: v8 Broke my Powershell Script

Post by menthol »

Thanks for that.

I've got it further than I had before... it will now try and duplicate the file, but it keeps getting stuck at 'WaitingTape'.

Even though it successfully moves the tape to the Free set.

It says the Tape is full, then says the tape currently in drive cannot be used for backup.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: v8 Broke my Powershell Script

Post by veremin »

Just to be sure - will it work, if you perform the very same steps via GUI? Thanks.
menthol
Influencer
Posts: 12
Liked: 2 times
Joined: Jan 24, 2014 10:02 am
Full Name: Brad Wyatt
Contact:

Re: v8 Broke my Powershell Script

Post by menthol »

Yep it's working via GUI.

There seems to be a difference between marking the tape as free and MOVING it to the 'Free' media pool.

Marking it as free worked.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: v8 Broke my Powershell Script

Post by veremin »

Two more thoughts. What happens if prior to executing job, you short erase a given medium (Erase-VBRTapeMedium)? What if you move a medium back to corresponding media pool after placing it inside "Free" one (Move-VBRTapeMedium)? Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests