Powershell. Report. Warnings. Failures.

PowerShell script exchange

Powershell. Report. Warnings. Failures.

Veeam Logoby dannordquist » Wed Nov 19, 2014 2:20 pm 1 person likes this post

Below is the code I'm using (testing with a console application) to gather the veeam warnings/failures and writing to a file for xymon to pick up. Please post to the appropriate forum or let me know where to create a post for this that will be most beneficial. I'm sharing it just so others who have the same needs can have a place to start, and if anyone has improvements, I'm happy to see them.

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Net.Mail;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Security;

namespace veeamBU
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string machineName = System.Environment.MachineName;
                string bbWinDir = "c:\\hobbit";
                string bbWinExt = "";
                string dispName = "veeamBU";
                string bbState = "green";

                ArrayList jobs = new ArrayList();

                jobs.Add("This is a test to determine if Veeam has either failures or warnings for");
                jobs.Add("current jobs, and to test for long running jobs that may be hung.");
                jobs.Add("\n");
                jobs.Add("The jobs displayed have either returned a warning or failure, and the servers");
                jobs.Add("that have problems are listed under the job with error/warning log entries.");
                jobs.Add("\n");
                jobs.Add("\n");

                if (Directory.Exists("c:\\program files\\bbwin"))
                {
                    bbWinDir = "c:\\program files\\bbwin\\tmp";
                    bbWinExt = "c:\\program files\\bbwin\\ext";
                }
                else if
                    (Directory.Exists("c:\\program files (x86)\\bbwin"))
                {
                    bbWinDir = "c:\\program files (x86)\\bbwin\\tmp";
                    bbWinExt = "c:\\program files (x86)\\bbwin\\ext";
                }
                else if
                    (Directory.Exists("c:\\hobbit"))
                {
                    bbWinDir = "c:\\hobbit\\tmp";
                    bbWinExt = "c:\\hobbit\\ext";
                }
                else
                    throw new Exception("Hobbit directory not found.");

                try
                {
                    Runspace rs = RunspaceFactory.CreateRunspace();
                    rs.Open();
                    PowerShell ps = PowerShell.Create();
                    ps.Runspace = rs;
                    ps.AddScript("Add-PSSnapin VeeamPSSnapin");
                    ps.AddScript("Get-VBRJob | where {($_.GetLastResult() -eq \"Failed\") -OR ($_.GetLastResult() -eq \"Warning\")} | select name");
                    foreach (PSObject result in ps.Invoke())
                    {
                        jobs.Add(result.Members["Name"].Value);
                        jobs.Add("-------------");
                        jobs.Add("\n");

                        Runspace rs2 = RunspaceFactory.CreateRunspace();
                        rs2.Open();
                        PowerShell ps2 = PowerShell.Create();
                        ps2.Runspace = rs2;
                        ps2.AddScript("Add-PSSnapin VeeamPSSnapin");
                        ps2.AddScript("$Job = Get-VBRJob -name " + result.Members["Name"].Value);
                        ps2.AddScript("$LastSession = $Job.FindLastSession()");
                        ps2.AddScript("$LastSession | Get-VBRTaskSession | ?{($_.status -eq \"Warning\") -OR ($_.status -eq \"Failed\")} | select name");
                        foreach (PSObject result2 in ps2.Invoke())
                        {
                            jobs.Add(result2.Members["Name"].Value);

                            ps2.AddScript("$TaskSession = $LastSession | Get-VBRTaskSession | where {$_.name -eq \"" + result2.Members["Name"].Value + "\"}");
                            ps2.AddScript("$TaskSession.Logger.GetLog().updatedrecords | sort starttime | select title");
                            foreach (PSObject result3 in ps2.Invoke())
                            {
                                if (result3.Members["title"].Value.ToString().Contains("Warning") ||
                                    result3.Members["title"].Value.ToString().Contains("Failed") ||
                                    result3.Members["title"].Value.ToString().Contains("Error"))
                                {
                                    string tempAdd = result3.Members["title"].Value.ToString();
                                    //Console.WriteLine(tempAdd);
                                    if (tempAdd.Count() >= 100)
                                    {
                                        do
                                        {
                                            jobs.Add(tempAdd.Substring(0, 99) + "-");
                                            tempAdd = tempAdd.Substring(99);

                                        } while (tempAdd.Count() >= 100);

                                        jobs.Add(tempAdd);
                                    }
                                    else
                                    {
                                        jobs.Add(tempAdd);
                                    }
                                }
                            }
                            jobs.Add("\n");
                            jobs.Add("\n");
                        }
                    }
                    rs.Close();
                }
                catch(Exception e)
                {
                    jobs.Add(e.ToString());
                }
                try
                {
                    Runspace rs = RunspaceFactory.CreateRunspace();
                    rs.Open();
                    PowerShell ps = PowerShell.Create();
                    ps.Runspace = rs;
                    ps.AddScript("Add-PSSnapin VeeamPSSnapin");
                    ps.AddScript("Get-VBRJob | select name, {$_.Findlastsession().creationTime}, {$_.Findlastsession().EndTime}, {$_.Findlastsession().IsFullMode}");

                    jobs.Add("Job Name\tStart Time\t\tEnd Time\t\tIs Full mode");
                    jobs.Add("--------\t----------\t\t--------\t\t------------");
                   
                    foreach (PSObject result in ps.Invoke())
                    {
                        string addLine = result.ToString();
                        string name = "";
                        string sTime = "";
                        string eTime = "";
                        string isFull = "";

                        addLine = addLine.Remove(0, addLine.IndexOf("Name=") + 5);
                        name = addLine.Substring(0, addLine.IndexOf(";"));
                        addLine = addLine.Remove(0, addLine.IndexOf("creationTime=") + 13);
                        sTime = addLine.Substring(0, addLine.IndexOf(";"));
                        addLine = addLine.Remove(0, addLine.IndexOf("EndTime=") + 8);
                        eTime = addLine.Substring(0, addLine.IndexOf(";"));
                        addLine = addLine.Remove(0, addLine.IndexOf("IsFullMode=") + 11);
                        isFull = addLine.Substring(0, addLine.IndexOf("}"));

                        jobs.Add(name + "\t" + sTime + "\t" + eTime + "\t" + isFull);

                        if (eTime.Contains("1/1/1900") && Convert.ToDateTime(sTime) < DateTime.Now.AddHours(-6) && isFull == "False")
                        {
                            jobs.Add("&red This incremental job is running too long!!!");
                        }
                        else if (eTime.Contains("1/1/1900") && Convert.ToDateTime(sTime) < DateTime.Now.AddHours(-24) && isFull == "True")
                        {
                            jobs.Add("&red This full backup job is running too long!!!");
                        }

                        jobs.Add("\n");
                    }
                    rs.Close();
                }
                catch (Exception e)
                {
                    jobs.Add(e.ToString());
                }

                string fileName = bbWinDir + "\\" + dispName;
                if (File.Exists(fileName))
                    File.Delete(fileName);
                using (StreamWriter sw = new StreamWriter(fileName))
                {
                    sw.WriteLine(bbState);
                    foreach (string allJobs in jobs)
                    {
                        Console.WriteLine(allJobs);
                        sw.WriteLine(allJobs);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
   }
    }
}
dannordquist
Enthusiast
 
Posts: 27
Liked: 2 times
Joined: Thu Oct 23, 2014 3:39 pm
Full Name: Dan Nordquist

Re: Powershell. Report. Warnings. Failures.

Veeam Logoby v.Eremin » Wed Nov 19, 2014 2:27 pm

Hi, Dan, I've split the existing discussion and renamed the topic in order to increase its searchability. Thank you for taking your time and sharing the script with fellow community members; much appreciated. Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Powershell. Report. Warnings. Failures.

Veeam Logoby dannordquist » Wed Nov 19, 2014 2:37 pm

Excellent, thank you. I love it when someone takes the time to share with others and I'm happy to be able to return the favor.

The above code is written in C#
dannordquist
Enthusiast
 
Posts: 27
Liked: 2 times
Joined: Thu Oct 23, 2014 3:39 pm
Full Name: Dan Nordquist

Re: Powershell. Report. Warnings. Failures.

Veeam Logoby dannordquist » Wed Nov 26, 2014 4:00 pm

FYI - to add job level warnings and failures, a getLog command must be issued at the job level after this PS command:
ps2.AddScript("$LastSession = $Job.FindLastSession()");

Issue a $lastsession.logger.getlog().updatedrecords command here.

Then you can parse the result for the job level for finding things like disk space warnings, etc.
dannordquist
Enthusiast
 
Posts: 27
Liked: 2 times
Joined: Thu Oct 23, 2014 3:39 pm
Full Name: Dan Nordquist


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 3 guests