PowerShell script exchange
Post Reply
frankive
Service Provider
Posts: 1092
Liked: 134 times
Joined: May 14, 2013 8:35 pm
Full Name: Frank Iversen
Location: Norway
Contact:

Script - different behaviour - different servers

Post by frankive »

We have a dozen clients running VeeamV11 on their servers.
We are using a RMM-tool which runs a script to check if there is a Warning or Failed in the last Results of the backkup jobs. On 60% of the servers, it runs like it should, but on other servers we are seeing this kind of error:

What does this mean? It can't be the script since it's working on most servers. If I run the script manually on the server it works like it should

Code: Select all

Last Run: Apr-14-2021 - 16:16
Run Time: 0.9962 seconds
Return Code: 0

Standard Error:
Get-VBRJob : The term 'Get-VBRJob' is not recognized as the name of a cmdlet, f
unction, script file, or operable program. Check the spelling of the name, or i
f a path was included, verify that the path is correct and try again.
At C:\Windows\TEMP\trmm\852611403.ps1:3 char:20
+         $VbrJobs = Get-VBRJob | Sort-Object typetostring, name
+                    ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-VBRJob:String) [], CommandN 
   otFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Else : The term 'Else' is not recognized as the name of a cmdlet, function, scr
ipt file, or operable program. Check the spelling of the name, or if a path was
 included, verify that the path is correct and try again.
At C:\Windows\TEMP\trmm\852611403.ps1:33 char:13
+             Else {
+             ~~~~
    + CategoryInfo          : ObjectNotFound: (Else:String) [], CommandNotFoun 
   dException
    + FullyQualifiedErrorId : CommandNotFoundException

Here is the script I am running:

Code: Select all


Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue

        # Get all the jobs
        $VbrJobs = Get-VBRJob | Sort-Object typetostring, name
        
        Foreach($Job in $VbrJobs)
        {
            $JobName = $Job.Name
            $Result = $Job.GetLastResult()

            If ($Result -eq "Warning") {
            $resultc = "Warning"
  }  

           ElseIf ($Result -eq "Failed") {
            $resultc = "Failed"
  }   
 

           If ($Resultc -eq "Warning") {
            'Backups have a warning.'
			Write-Output $VbrJobs
			Exit 1
  }  

           ElseIf ($Resultc -eq "Failed") {
            'Backup has failed.'
			Write-Output $VbrJobs
			Exit 1
  }   

        }
             
            Else {

  'The backup is running successfully.'
  Write-Output $VbrJobs
  Exit 0
} 

Exit $LASTEXITCODE
jhoughes
Veeam Vanguard
Posts: 279
Liked: 112 times
Joined: Apr 20, 2017 4:19 pm
Full Name: Joe Houghes
Location: Castle Rock, CO
Contact:

Re: Script - different behaviour - different servers

Post by jhoughes » 1 person likes this post

You shouldn't be using the PSSnapIn for Veeam v11 anymore.

It was converted to a module for version 11 (link to What's New doc), and that error message is due to the 'Veeam.Backup.PowerShell' not getting imported by PowerShell's module auto-loading functionality.

You either need to import the module explicitly or use a #Requires statement in your script header, or you need to look into the underlying issue within PowerShell as to why the module is not loading automatically.

That is an issue outside of the Veeam module, and it is really a problem with the script being outdated, or PowerShell not being able to locate or load the module properly.
Husband, Father, Solutions Architect, Geek Extraordinaire | @DenverVMUG, @AustinVMUG & @ATXPowerShell leader | VMware vExpert | Cisco Champion
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Script - different behaviour - different servers

Post by oleg.feoktistov »

Frank, in your posts please use </> label to wrap your code instead of quoting it ;)
Adding to Joe comment - it is worth checking whether the module path is listed in $env:PSModulePath variable. Thanks!
frankive
Service Provider
Posts: 1092
Liked: 134 times
Joined: May 14, 2013 8:35 pm
Full Name: Frank Iversen
Location: Norway
Contact:

Re: Script - different behaviour - different servers

Post by frankive »

I had to add this to the top, then it worked out just fine:

Code: Select all

# Make sure PSModulePath includes Veeam Console
    $MyModulePath = "C:\Program Files\Veeam\Backup and Replication\Console\"
    $env:PSModulePath = $env:PSModulePath + "$([System.IO.Path]::PathSeparator)$MyModulePath"
    if ($Modules = Get-Module -ListAvailable -Name Veeam.Backup.PowerShell) {
        try {
            $Modules | Import-Module -WarningAction SilentlyContinue
            }
            catch {
                throw "Failed to load Veeam Modules"
                }
        }
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests