Output questions

PowerShell script exchange

Output questions

Veeam Logoby tcollier » Wed Jan 09, 2013 8:21 pm

Hi,

We are using a monitoring tool which can read log files. So I want to add the Veeam backup.
So I created a Powershell script, I am fairly new to Powershell :

Code: Select all
$TranscriptDate = get-Date -f "yyyy-MM-dd---HH-mm"
$TranscriptFile = "c:\Scripts\VeeamLogs\$TranscriptDate.log"
Start-Transcript -Path $TranscriptFile
Add-PSSnapin -name "VeeamPSSnapIn"
Get-VBRJob
Stop-Transcript
exit


This give me the following output when runned from the powershell launched from B&R console :

    Job Name Type State Last Result Description
    -------- ---- ----- ----------- -----------
    Daily Backup ACTO-BOB VMware Backup Stopped Success Created by...
    Daily Backup ACTO-FTP VMware Backup Stopped Success Created by...

This is good for me as the status is displayed, and the script search for words ( eg Success, Failures )

But when I run the script from a command line ( PowerShell.exe -File VeeamScript.ps1 ), I get the following output :

    IsFree : False
    Id : 54c330ce-5dff-4d86-b1d0-1f8378f606b1
    Info : Veeam.Backup.Model.CDbBackupJobInfo
    JobType : Backup
    SourceType : VDDK
    JobTargetType : Backup
    TargetType : Other
    TypeToString : VMware Backup
    Description : Created by ACTO-MGMT\PcManager at 28/06/2012 15:26:13.
    Name : Daily Backup ACTO-BOB
    BackupPlatform : EVmware
    TargetHostId : 6745a759-2205-4cd2-b172-8ec8f7e60ef8
    TargetDir : F:\Backups
    TargetFile : Daily Backup ACTO-BOB
    Options : Veeam.Backup.Model.CJobOptions
    IsContinuous : False
    HvReplicaTargetOptions : Veeam.Backup.Model.CDomHvReplicaTargetOptions
    BackupStorageOptions : Veeam.Backup.Model.CDomBackupStorageOptions
    BackupTargetOptions : Veeam.Backup.Model.CDomBackupTargetOptions
    HvSourceOptions : Veeam.Backup.Model.CDomHvSourceOptions
    JobOptions : Veeam.Backup.Model.CDomJobOptions
    NotificationOptions : Veeam.Backup.Model.CDomNotificationOptions
    PostJobCommand : Veeam.Backup.Model.CDomPostJobCommand
    ViReplicaTargetOptions : Veeam.Backup.Model.CDomViReplicaTargetOptions
    ViSourceOptions : Veeam.Backup.Model.CDomViSourceOptions
    VssOptions : <CVssOptions><Enabled>True</Enabled><IgnoreErrors>Fals
    e</IgnoreErrors><GuestFSIndexingType>EveryFolders</Gue
    stFSIndexingType><Credentials><UserName>Administrator<
    /UserName><Password>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAin
    ........... and so on

Can someone tell me what I am doing wrong here?
tcollier
Novice
 
Posts: 3
Liked: never
Joined: Wed Jan 09, 2013 8:12 pm

Re: Output questions

Veeam Logoby v.Eremin » Thu Jan 10, 2013 10:26 am

In order to get in your log-file results that are similar to the ones you see in Veeam Snap-in, you can implement something like this:

Code: Select all
asnp VeeamPSSnapin

$TranscriptDate = get-Date -f "yyyy-MM-dd---HH-mm"
$TranscriptFile = "c:\Scripts\VeeamLogs\$TranscriptDate.log"
$jobs= Get-VBRJob
foreach($jobs in $jobs)
{
$jobs.name + " " + $jobs.Jobtype + " " + $jobs.GetLastState() + " " + $jobs.GetLastResult() >> $TranscriptFile


Furthermore, by the means of simple modification you can list in your log-file only jobs that have failed during last run or completed with warnings, etc.

Additionally, it might be worth taking a look at these topics regarding advanced PowerShell reporting:

viewtopic.php?f=26&t=13422
viewtopic.php?f=26&t=8925

Hope this helps.
Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Output questions

Veeam Logoby v.Eremin » Thu Jan 10, 2013 4:00 pm

Otherwise, you can slightly modify backup size report that recently has been recently discussed in this topic and redirect output data into the log-file.

Code: Select all
asnp VeeamPSSnapin

$TranscriptDate = get-Date -f "yyyy-MM-dd---HH-mm"
$TranscriptFile = "c:\Scripts\VeeamLogs\$TranscriptDate.log"
Get-VBRJob | Select-Object -Property @{N="Name";E={$_.Name}} ,@{N="JobType";E={$_.JobType}},@{N="Last State";E={$_.GetLastState()}},@{N="Last Result";E={$_.GetLastResult()}}| Sort Name -Descending | Format-Table > $TranscriptFile


Special thanks should go to Tom Sightler who’s provided original script in the aforesaid topic.
Hope this helps.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Output questions

Veeam Logoby tcollier » Thu Jan 10, 2013 8:44 pm

Both are correct, the second one has a better formatting.
Do you know why my output is different when running the command Get-VBRJob ?
tcollier
Novice
 
Posts: 3
Liked: never
Joined: Wed Jan 09, 2013 8:12 pm

Re: Output questions

Veeam Logoby tsightler » Thu Jan 10, 2013 9:35 pm

I think I can answer that, but one of the Powershell gurus may want to school me if I'm wrong.

You'll notice that the first output to the screen is formatted with a subset of variables to be displayed, while the "transcript" version basically list the entire object as it's returned, without any special formatting for display. When running the Get-VBRJob cmdlet interactively, the output is formatted due to a windows Powershell feature that allows you to define a "format" for an object when it is displayed by defining the format in an format.ps1xml file.

For the Veeam commands, these custom object display formats are stored in Veeam.Backup.PowerShell.format.ps1xml and if you open this file you will see the following view defined for the Veeam.Backup.Core.CBackupJob object type:

Code: Select all
  <View>
      <Name>Veeam.Backup.Core.CBackupJob</Name>
      <ViewSelectedBy>
        <TypeName>Veeam.Backup.Core.CBackupJob</TypeName>
        <TypeName>Deserialized.Veeam.Backup.Core.CBackupJob</TypeName>
      </ViewSelectedBy>
      <TableControl>
        <TableHeaders>
          <TableColumnHeader>
            <Label>Job Name</Label>
            <Width>25</Width>
            <Alignment>left</Alignment>
          </TableColumnHeader>
          <TableColumnHeader>
            <Label>Type</Label>
            <Width>15</Width>
            <Alignment>left</Alignment>
          </TableColumnHeader>
          <TableColumnHeader>
            <Label>State</Label>
            <Width>10</Width>
            <Alignment>left</Alignment>
          </TableColumnHeader>
          <TableColumnHeader>
            <Label>Last Result</Label>
            <Width>12</Width>
            <Alignment>left</Alignment>
          </TableColumnHeader>
          <TableColumnHeader>
            <Label>Description</Label>
            <Alignment>left</Alignment>
          </TableColumnHeader>
        </TableHeaders>
        <TableRowEntries>
          <TableRowEntry>
            <TableColumnItems>
              <TableColumnItem>
                <ScriptBlock>$_.Name</ScriptBlock>
              </TableColumnItem>
              <TableColumnItem>
                <ScriptBlock>$_.TypeToString</ScriptBlock>
              </TableColumnItem>
              <TableColumnItem>
                <ScriptBlock>$_.GetLastState()</ScriptBlock>
              </TableColumnItem>
              <TableColumnItem>
                <ScriptBlock>$_.GetLastResult()</ScriptBlock>
              </TableColumnItem>
              <TableColumnItem>
                <ScriptBlock>$_.Description</ScriptBlock>
              </TableColumnItem>
            </TableColumnItems>
          </TableRowEntry>
        </TableRowEntries>
      </TableControl>
    </View>


So, when running the Get-VBRJob cmdlet from the console, it returns the Veeam job object which is then automatically formatted by Powershell and output to the screen as defined by the XML format for this object so that it looks nice on the screen. However, when you run "Start-Transcript" this takes the raw output of the commands, without the XML defined display formatting, and redirects it to a file, thus you are getting a full dump of the entire object in the log file.

So, to get the result you desire you'll simply want to redirect standard output to the log file as shown in the other examples that have been provided.
tsightler
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Output questions

Veeam Logoby Sethbartlett » Fri Jan 11, 2013 1:01 am

Tom is exactly right. If you go into the Initialize-VeeamToolkit ps file in the Program Files\Veeam\Backup and Replication\ folder you will see that it does a reference to this xml data. It is used for formatting objects when looking at them at a glance. This is useful for the fact that you really only want to see a few pieces of data about a job, you dont need to see the scheduling everytime "at a glance"
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
Sethbartlett
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: Output questions

Veeam Logoby tsightler » Fri Jan 11, 2013 3:50 am 1 person likes this post

Well, to be fair, not exactly right! :(

After seeing your post I realized that the problem wasn't that "Start-Transcript" doesn't use the same output as the display (I was mislead by a different issue that I've run into with Start-Transcript), but that the problem in this case is that, when loading the program VeeamPSSnapin directly, the custom format data is not part of the profile. This is very trivial to work around as follows:

Code: Select all
Add-PSSnapin VeeamPSSnapIn
$snapIn = Get-PSSnapIn "VeeamPSSnapIn"
$xmlFilePath = [System.IO.Path]::Combine($snapIn.ApplicationBase, "Veeam.Backup.PowerShell.format.ps1xml")
update-formatdata -prependPath $xmlFilePath
$TranscriptDate = get-Date -f "yyyy-MM-dd---HH-mm"
$TranscriptFile = "c:\Scripts\VeeamLogs\$TranscriptDate.log"
Start-Transcript -Path $TranscriptFile
Get-VBRJob
Stop-Transcript
exit


So, while I was right about why the code was formatted when run from the console when started from within Veeam, I wasn't right about why it wasn't formatted when called externally. The above code should work fine and give the same formatting even when run as a "standalone" script. Sorry for the misinformation about that part in my first post! :oops:
tsightler
Veeam Software
 
Posts: 4768
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Output questions

Veeam Logoby tcollier » Fri Jan 11, 2013 9:37 am

If there where any points to be given, you'll get them all :)

Thank you for the explanation, now I know what to do with my script! Thank you for this!
tcollier
Novice
 
Posts: 3
Liked: never
Joined: Wed Jan 09, 2013 8:12 pm

[MERGED] Able to run commands from toolkit

Veeam Logoby mhaaksma » Mon Jul 03, 2017 5:54 pm

Hello,

I' am a powershell & veeam newbie.

I am trying to figure out how to send a simple plain text email notification for 1 backup job when it fails. Veeam sends email notifications with a bunch of html tags in it. I would like to use powershell to get around this.

I opened up the "Veeam Backup and Replication PowerShell Toolkit" from the GUI. I can run commands like Get-VBRCommand successfully:

Code: Select all
PS C:\Users\username> Get-VBRJob -Name Test_Backup_Job

Job Name                  Type            State      Last Result  Description
--------                  ----            -----      -----------  -----------
Test_Backup_Job VMware Backup   Working    None         Created by X\username at 6/26/2017 3:33 PM.


However, when I open up powershell outside of Veeam, I get this:

Code: Select all
PS C:\Windows\system32> Add-PSSnapin VeeamPSSnapin
PS C:\Windows\system32> Get-VBRJob -Name Test_Backup_Job


FreeBackupImpl               :
IsFree                       : False
UserCryptoKey                :
Id                           : 8ddc6161-ade8-48a9-afb0-f2fbf2ea0e63
Info                         : Veeam.Backup.Model.CDbBackupJobInfo
JobType                      : Backup
SourceType                   : VDDK
JobTargetType                : Backup
TargetType                   : Other
TypeToString                 : VMware Backup
Description                  : Created by PROS1\username at 6/26/2017 3:33 PM.
Name                         : Test_Backup_Job
BackupPlatform               : EVmware
TargetHostId                 : 00000000-0000-0000-0000-000000000000
TargetDir                    :
TargetFile                   : Test_Backup_Job
Options                      : Veeam.Backup.Model.CJobOptions
IsContinuous                 : False
NameWithDescription          : Test_Backup_Job (Created by PROS1\username at 6/26/2017 3:33 PM.)
SqlEnabled                   : False
OracleEnabled                : False
ParentJobId                  :
HvReplicaTargetOptions       : Veeam.Backup.Model.CDomHvReplicaTargetOptions
BackupStorageOptions         : Veeam.Backup.Model.CDomBackupStorageOptions
BackupTargetOptions          : Veeam.Backup.Model.CDomBackupTargetOptions
HvSourceOptions              : Veeam.Backup.Model.CDomHvSourceOptions
NotificationOptions          : Veeam.Backup.Model.CDomNotificationOptions
JobScriptCommand             : Veeam.Backup.Model.CDomJobScriptCommand
ViReplicaTargetOptions       : Veeam.Backup.Model.CDomViReplicaTargetOptions
CloudReplicaTargetOptions    : Veeam.Backup.Model.CDomCloudReplicaTargetOptions
ViSourceOptions              : Veeam.Backup.Model.CDomViSourceOptions
VssOptions                   : VssSnapshotOptions: [Enabled: [False], IgnoreErrors: [False], IsCopyOnly: [False]],
                               WinGuestFSIndexingOptions: [Type: [None]], LinGuestFSIndexingOptions: [Type: [None]],
                               SqlBackupOptions: [TransactionLogsProcessing: [TruncateOnlyOnSuccessJob],
                               BackupLogsFrequencyMin: [15], UseDbBackupRetention: [True], RetainDays: [15],
                               ProxyAutoSelect: [True]], ExchangeBackupOptions: [TransactionLogsProcessing:
                               [TruncateOnlyOnSuccessJob]], WinCredsId: [00000000-0000-0000-0000-000000000000],
                               LinCredsId: [00000000-0000-0000-0000-000000000000], Old Properties: [Enabled: [False],
                               IgnoreErrors: [False], GuestFSIndexingType: [None], IncludedIndexingFolders: [],
                               ExcludedIndexingFolders: [], LinGuestFSIndexingType: [None],
                               LinIncludedIndexingFolders: [], LinExcludedIndexingFolders: [], IsFirstUsage: [True]]
ScheduleOptions              : Start time: [7/4/2017 7:00:00 AM], Latest run time: [7/3/2017 7:00:12 AM], Timeout for
                               backup completion: [180min], Next run time: [07/04/2017 07:00:00], Retry times on
                               failure: [3], Retry timeout: [60 min], Daily options: [Enabled: True, DayNumberInMonth:
                               Everyday, Days: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
IsRunning                    : True
IsRequireRetry               : True
IsIdle                       : False
IsScheduleEnabled            : True
SourceProxyAutoDetect        : True
IsInitialReplica             : False
IsMappingReplica             : False
IsBackup                     : True
IsBackupSync                 : False
IsSnapshotReplica            : False
IsLegacyReplica              : False
IsReplica                    : False
IsVmCopy                     : False
IsTapeBackup                 : False
IsFileTapeBackup             : False
IsVmTapeBackup               : False
SourceWanAcceleratorName     :
TargetWanAcceleratorName     :
LinkedJobs                   : {}
LinkedJobIds                 : {}
LinkedRepositories           : {}
LinkedRepositoryIds          : {}
IsForeverIncremental         : False
IsBackupJob                  : True
IsEndpointJob                : False
SheduleEnabledTime           :
AutoScheduleOptions          : Veeam.Backup.Model.CAutoScheduleOptions
PreviousJobIdInScheduleChain :


Any idea on why the difference? Any help will be greatly appreciated. Thank you.
Thank you,
Monica
mhaaksma
Novice
 
Posts: 3
Liked: never
Joined: Thu Jun 29, 2017 2:39 pm
Full Name: Monica

Re: Output questions

Veeam Logoby v.Eremin » Mon Jul 03, 2017 6:25 pm 1 person likes this post

The console output is formatted differently. To get console-like output, kindly, leverage the workaround proposed above. Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Output questions

Veeam Logoby tdewin » Tue Jul 04, 2017 8:30 am

btw you can sometimes force output by using Format-List or Format-Table
tdewin
Veeam Software
 
Posts: 1043
Liked: 361 times
Joined: Fri Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 3 guests