PowerShell script exchange
Post Reply
Posts: 31
Liked: 5 times
Joined: Oct 21, 2014 7:56 am

MultiVersion Support for Powershell Scripts

Post by Talom » 2 people like this post

I just wanted to share my short code snippet to use the same Powershell Script for different Veeam Versions.
This was my solution to migrate all my old scripts for Veeam11 without dropping the support for older versions.

Code: Select all

$module = "Veeam.Backup.PowerShell"
    $snapin = "VeeamPSSnapIn"
    if (Get-Module | Where-Object {$_.Name -eq $module}) {
        write-host "Module $module is already imported."
    else {

        # If module is not imported, but available on disk then import
        if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $module}) {
            Import-Module $module
        else {

           if ((Get-PSSnapin | ? { $_.Name -eq $snapin }) -eq $null) {
                Add-PSSnapin $snapin 

Veeam Software
Posts: 382
Liked: 205 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Location: Kentucky, USA

Re: MultiVersion Support for Powershell Scripts

Post by chris.arceneaux » 1 person likes this post

Thanks for sharing! I took a similar route in my scripts that need to support v11 and older versions:

Code: Select all

# Accounts for switch from PSSnapin to Module in v11
if (-Not (Get-Module -ListAvailable -Name Veeam.Backup.PowerShell)){
    Add-PSSnapin -PassThru VeeamPSSnapIn -ErrorAction Stop | Out-Null
Note that you shouldn't have to import the Veeam PowerShell module in your scripts anymore as modules auto-import anytime you use a cmdlet specified within the module. That said, importing the module manually is perfectly valid as well.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests