PowerShell script exchange
Post Reply
pkelly_sts
Veteran
Posts: 600
Liked: 66 times
Joined: Jun 13, 2013 10:08 am
Full Name: Paul Kelly
Contact:

Script to move media to media pools on specified dates

Post by pkelly_sts » 2 people like this post

I know this is a bit of an edge case but I'm one of a number of users having "misunderstandings" with Veeams handling of tapes and which tapes it chooses to write to, or pull in from a free pool so I've written a Powershell script to "manually" move media around to suit myself.

I'm thinking that this might be a hands-off way of pulling tapes into a Media Pool in a defined & specific order so you know exactly which tape will be available for use on a given date. I'm also thinking that after a first pass of working like this, by the time overwrite protection processes kick in, the tapes should be suitably protected by date that they'll take care of themselves in future automatically.

I don't do enough Powershell myself personally so have been forcing myself to do it more regularly so that I don't keep forgetting so much about it so this has partly been a bit of a training exercise for myself too! If you know of a significantly more efficient way of doing some of what this script does then I'd be more than happy to learn! :)

This script reads a text file (that you will need to create) in which each line contains:
Date (of the desired move operation)
Tape (Name of the tape as it appears within Veeam catalog)
MediaPool (Name of the media pool you want to move the tape to)

An example file content is included in the comments at the beginning of the script.

I'm quite pleased with it (as a relative Powershell noob) & thought someone else might find it of use so thought I might as well share it!

If you find it useful, great! If not, I'm sure you won't lose any sleep... ;-D

Code: Select all

<#  MoveTapetoMediaPool.ps1

    Read a series of lines from a .csv file to automatically move barcoded tape media into a specified destination media pool on the specified date

    Example file format below:
    ---
    Date,Tape,MediaPool
    23/06/16,GM1234L6,Daily-Pool
    24/06/16,XM4321L6,Weekly-Pool
    ---

    Edit the line in the script defining the path\name of the text file to suit your own needs.

    Important Note: date format can very by culture/country
    As originally written this script is based in the UK so uses %d/%m/%y format in the "Get-Date" line below.
    Change this format, and the format in the text file, should this not be what you require.

    If you want to test your file without actually moving any tapes, then simply comment out the following line further in the script:

    Move-VBRTapeMedium -Medium $Tape -MediaPool $DestMediaPool

    This script will error-check for the following only:
    1) That the input file exists
    2) That the specified tapes exist within Veeams catalog
    3) That the destination Media Pool exists
#>

# Load Veeam Powershell SnapIn if not already loaded
if ((Get-PSSnapin VeeamPSSnapIn -ErrorAction SilentlyContinue) -eq $null) {
    $error.clear()
    Add-PsSnapin VeeamPSSnapIn
}

# Define the path to the Media Pool Change Schedule CSV text file
$MediaPoolFile = "C:\Scripts\MediaPoolChangeSchedule.txt"

# Check text file exists before attempting to process it
If (Test-Path $MediaPoolFile){
    # File exists so read in each line of the file & see if any moves are scheduled for todays date
    import-csv $MediaPoolFile | ForEach-Object {

        if ($(Get-Date -UFormat %d/%m/%y) -eq $_.Date) {
            # Todays date was found on a line in the file so execute tape-moving process here
            # Define variables again to make them a little more readable
            $Tape = $_.Tape
            $DestMediaPool = $_.MediaPool

            Write-output "Tape $Tape needs moving today"

            # First check that the tape actually exists within Veeams catalog i.e. catch any typos in the file

            If (get-VBRTapeMedium -name $Tape -ErrorAction SilentlyContinue) {
                # Tape does exist so start processing. Next line of code below does following:
                #    Gets the barcode for the selected tape
                #    Gets the media Pool ID that the tape/barcode is currently in
                #    Converts the media pool ID to the full media pool name
                #    If the tapes current media pool name is different from the destination media pool name, then process the move tape code
                If (((Get-VBRTapeMediaPool -id ((Get-VBRTapeMedium | Where-Object {$_.Barcode -eq $Tape}).MediaPoolID)).name -ne $DestMediaPool)) {
         
                    # Check Dest Media pool exists before trying to move a tape to it
                    if (Get-VBRTapeMediaPool -name $DestMediaPool -ErrorAction SilentlyContinue) {
                        # Dest Media Pool exists so go ahead with move
                        write-output "Moving tape $Tape to Media Pool $DestMediaPool ..."
                        
                        # Move the tape to the new pool - Comment out the line below if you only want to test behaviour
                        Move-VBRTapeMedium -Medium $Tape -MediaPool $DestMediaPool

                    } else {
                        # Dest Media Pool does not exists so don't try moving the tape to it
                        write-output "$Destmediapool does not exist! Nothing moved."
                    }
 
                } Else {
                    # Tapes current and destination media pool names match so the tape must already be in the destination Media Pool
                    write-output "Tape $Tape already in Media Pool $DestMediaPool"
                }

              } Else {
                  # Tape in text file not found within Veeam
                  Write-Output "Tape $Tape does not exist. Exiting..."
              }

        }

    }
} Else {
        # Tape schedule text file not found
        Write-output "Tape Move Schedule text file $MediaPoolFile doesn't exist, skipping further processing"
}
danpushpop
Lurker
Posts: 2
Liked: never
Joined: Dec 10, 2019 3:08 pm
Full Name: Mohamad Ramadhan
Contact:

Re: Script to move media to media pools on specified dates

Post by danpushpop »

Do you know if there is a way i can add the tapes in the media pool to the weekly or the monthly set?
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Script to move media to media pools on specified dates

Post by veremin »

1) Get required tape - Get-VBRTapeMedium
2) Create new GFS media set policy while passing the given tape as one entities of -Medium parameter - New-VBRTapeGFSMediaSetPolicy
3) Get GFS media pool - Get-VBRTapeMediaPool
4) Set the created GFS media set policy as -WeeklyMediasetPolicy parameter - Set-VBRTapeMediaPool

Thanks!
danpushpop
Lurker
Posts: 2
Liked: never
Joined: Dec 10, 2019 3:08 pm
Full Name: Mohamad Ramadhan
Contact:

Re: Script to move media to media pools on specified dates

Post by danpushpop »

Hi Veremin,

Do you have an example of the script?

Thanks!
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Script to move media to media pools on specified dates

Post by veremin »

Nope, but using the provided cmdlets it should not be hard to create one. Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests