Skip to main content

Hello,

It’s possible to get job status (warning, failed or success) using powershell script?

hi @hs08 

i uses MyVeeamReport.ps1

MyVeeamReport.ps1 · GitHub


I use Get-VBRJob to get the output

 

but when I try to filter only to show Type=”Vmware Backup” there is no result. Anyone know why?

 


Try this code:

Get-VBRJob | Where-Object { $_.JobType -eq "Backup" }

The column headers of the output of an array of objects in powershell do not always align to the objects’ property names. To get the correct properties for filtering, pipe a single object to “format-list” (or “fl”).


Matt's suggestion above should work to give you what you want.  The other one Link State mentioned is also very good.


@hs08,

 

If you’re ever not sure on what values are accepted, check the type for a given property by calling the GetType() method. If it’s a System.Enum, you can just call the enum names with an additional method:

 

PS C:\Users\david.LAB> $job.JobType.GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True EDbJobType System.Enum


PS C:\Users\david.LAB> $job.JobType.GetType().GetEnumNames()
Backup
Replica
Copy
...

I honestly just keep a copy of this output of enums around for quick reference when making filters with Where-Object. I usually just print a list every major release to make sure it’s current and use that as a reference.

 

If you aren’t sure which is which, make a job with a known type, then just get that job by name with Get-VBRJob -Name or use the -Id (you can always get the ID from the job log for a given job -- look near the beginning of a log (search ‘new log’ and then look for JobID or Job ID)


Try this code:

Get-VBRJob | Where-Object { $_.JobType -eq "Backup" }

The column headers of the output of an array of objects in powershell do not always align to the objects’ property names. To get the correct properties for filtering, pipe a single object to “format-list” (or “fl”).

Another option to list a cmdlet’s Properties is to Pipe output with the Get-Member cmdlet. I believe this lists all Properties for a given cmdlet as well.


Hello, i want to get the ‘last Result’ and wnt to know what the header name?

I try many header parameters but not get the value.

 

 


@hs08,

There unfortunately a little trick Veeam does with Powershell where the output of Get-VBRJob has a custom view applied to the Powershell session, and some of that data is an amalgamate from a few other options.

The last result is on CBackupSession objects returned by Get-VBRBackupSession. Get-VBRBackupSession is a bit slow unless you pass Job Ids to the -Id parameter, so I’m actually going to recommend a different way:

Get-VBRSession -Type “ type of job without square brackets ]t“ | Where-Object {$_.JobId -eq $job.id} | Sort -Property CreationTime -Descending

This will return a list of job sessions with more limited information based on the JobId you pass to it. From there you can make a PSCustomObject to reflect what you want.

There are ways to get this from the CBackupJob object returned by Get-VBRJob, but you need to use unsupported methods as far as I know, so I recommend do not go that route and use supported methods.

 


Similarly, the reason that GetLastResult didn’t work for you is because that is a .NET internal method. These are unsupported methods, and I do not advise using them. Methods that are read-only like this one are typically safe to use, but it’s best to avoid them. To call them in a a Select-Object statement like you’re trying, you need to follow the example here:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-7.4#example-14-expandproperty-alters-the-original-object

 

Get-VBRJob | Where-Object {$_.JobType -eq “Backup”} | Select Id, Name, JobType, @{n=”Last Result”;e={$_.GetLastResult()}}

PS C:\Users\david.LAB> $job | Select Id, Name, JobType, @{n="Last Result";e={$_.GetLastResult()}}

Id Name JobType Last Result
-- ---- ------- -----------
3713a1dd-8ff5-4a6d-ac24-4e89d348fffb dd-malware-box-backup Backup Success


 


@ddomask 

Super…., it’s work by execute

Get-VBRJob | Where-Object {$_.JobType -eq “Backup”} | Select Id, Name, JobType, @{n=”Last Result”;e={$_.GetLastResult()}}


You’re welcome hs08, but do remember, this is an unsupported method, so if there is _any_ weirdness with it, as per Support Policy (5.8) no support for such unsupported methods.

It will work and probably will not change, but I cannot guarantee that it won’t, so I advise build your script with the PSCustomObject and Get-VBRBackupSession + Get-VBRJob as noted.


Great to see you figured out the correct syntax for the PS command and it now works.  👍


I’m working on a Veeam PS script based on a Job’s “state”. If I run Get-VBRJob -Name <job-name> it displays a few properties..one of which is the State (i.e. Stopped or Running). But, if I place that cmd in a variable and run $Variable.State, or pipe that cmd with Select State..the State column doesn’t show the Job state..the column is blank. Any idea why ​@ddomask or others? 🤔

Thanks!


I’m working on a Veeam PS script based on a Job’s “state”. If I run Get-VBRJob -Name <job-name> it displays a few properties..one of which is the State (i.e. Stopped or Running). But, if I place that cmd in a variable and run $Variable.State, or pipe that cmd with Select State..the State column doesn’t show the Job state..the column is blank. Any idea why ​@ddomask or others? 🤔

Thanks!

 You can see all properties by using Format-List

PS C:\Users\jocolon> $Job = Get-VBRJob -Name HyperV-Backup-Job
PS C:\Users\jocolon> $Job

Job Name Type State Last Result Description
-------- ---- ----- ----------- -----------
HyperV-Backup-Job Hyper-V Backup Stopped None Created by

PS C:\Users\jocolon> $Job | Format-list


LogNameMainPart : HyperV-Backup-Job
FreeBackupImpl :
IsFree : False
UserCryptoKey : Veeam.Backup.Core.CCryptoKey
Uid : 89887443-aebf-482e-921b-18e34f835c71
IsPseudoContinuousJob : False
Id : 89887443-aebf-482e-921b-18e34f835c71
Info : Veeam.Backup.Model.CDbBackupJobInfo
JobType : Backup
SourceType : HyperV
JobTargetType : Backup
TargetType : Other
TypeToString : Hyper-V Backup
Description : Created by PHARMAX\administrator at 6/30/2021 10:02 PM.
Name : HyperV-Backup-Job
BackupPlatform : EHyperV
TargetHostId : f551fdee-3479-4631-86d2-863c27440b22
TargetDir : E:\Backups
TargetFile : HyperV-Backup-Job
Options : Veeam.Backup.Model.CJobOptions
Extensions : Veeam.Backup.Core.CJobExtensions
IsContinuous : False


Additionally, the `Get-Member` command displays the properties and methods of the object.

PS C:\Users\jocolon> $Job | Get-Member


TypeName: Veeam.Backup.Core.CBackupJob

Name MemberType Definition
---- ---------- ----------
CanRunByScheduler Method bool ISchedulableJob.CanRunByScheduler()
CheckDeleteAllowed Method void CheckDeleteAllowed()
CreateJobDeletionMutex Method Veeam.Backup.Core.CMutex CreateJobDeletionMutex()
Delete Method void Delete(Veeam.Backup.Model.CModifiedUserInfo initiator)
DeleteOrMarkForDeletion Method void DeleteOrMarkForDeletion(Veeam.Backup.Model.CModifiedUserInfo modifiedBy)
DetachFromParentPolicy Method void DetachFromParentPolicy()
DisableScheduler Method void DisableScheduler(Veeam.Backup.Model.CModifiedUserInfo initiator)
EnableScheduler Method void EnableScheduler(Veeam.Backup.Model.CModifiedUserInfo initiator)
Equals

For example, to access the content of the `State` variable, you can use the `GetLastState()` method. When the `Get-VBRJob -Name HyperV-Backup-Job` cmdlet is called, the `State` property obtains its value from the `GetLastState()` method, which is accessed through a calculated property.

PS C:\Users\jocolon> $Job.GetLastState()
Stopped
PS C:\Users\jocolon>

I came across those questions and many more when I developed the AsBuiltReport.Veeam.VBR report.

 

PD: Exercism offers an excellent PowerShell learning track: Exercism


I’m working on a Veeam PS script based on a Job’s “state”. If I run Get-VBRJob -Name <job-name> it displays a few properties..one of which is the State (i.e. Stopped or Running). But, if I place that cmd in a variable and run $Variable.State, or pipe that cmd with Select State..the State column doesn’t show the Job state..the column is blank. Any idea why ​@ddomask or others? 🤔

Thanks!

@coolsport00, Get-VBRJob returns CJob objects which gives you information about the job settings, so you will only find information from CJob objects like retention settings, active full settings, etc.

Your confusion I think is coming because the Veeam Powershell Module has a custom formatting file that pretties up the console output on some cmdlets:

C:\Program Files\Veeam\Backup and Replication\Console\Veeam.Backup.PowerShell\Veeam.Backup.PowerShell.format.ps1xml

You can check this on any server where you have the remote console installed, and the relevant section for the State part is at line 228:

 

     <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>

You can see that it’s doing a mapping to make the display all pretty, and ​@jcolonfzenpr is correct, this wrapper simply calls $_.GetLastState() which actually resides on CBackupSession objects returned by Get-VBRBackupSession.

So your confusion is understandable, this usually will bite you at least once as you’re working with Powershell and Veeam.

Like jcolonfzenpr tells, you can use that method if you like to check the state of the job, it’s reliable.

 

P.S. ​@jcolonfzenpr love the Veeam AsBuilt report script, monumental work!


@jcolonfzenpr ​@ddomask - thanks guys. I ping’d some V100 folks in our Signal channel and ​@TylerJurgens gave me a different approach to look at → the IsRunning property. Using the “gm” cmdlet, I ended up finding out there just isn’t a State Property to call with the Get-VBRJob cmdlet as you guys shared. I did look at the VBRBackupSession cmdlet but still couldn’t quite get there. The task I was wanting to do was pretty simple - if a certain Job is not running at a given time (I’m going to call my script in Task Scheduler), start it. But to do so, I simply wanted to check if the BU Job was “Stopped” or not; or, with the Property I ended up using...checking to see if it was “Running” or not works too. Thanks for the explanation on this. Appreciate it!


Comment