Looping around jobs and VM

PowerShell script exchange

Looping around jobs and VM

Veeam Logoby marius roma » Wed Nov 25, 2015 12:58 pm

I need to extract statistical information about "each time each VM was replicated during each execution of each job".
I implemented a loop like:
Code: Select all
#load Veeam Powershell Snapin
Add-PSSnapin -Name VeeamPSSnapIn # -ErrorAction SilentlyContinue
$jobs = get-vbrjob | ?{$_.jobtype -eq "Replica"}
foreach ($job in $jobs) {
$Sessions = Get-VBRBackupSession | ?{$_.jobid -eq $job.id}
foreach ($session in $sessions) {
$tasks = Get-VBRTaskSession $session
foreach ($task in $tasks)  {
# Print the name of each VM each time it was replicated
$VM_name = $task.name
$VM_name
# $task should contain the information about each time a VM was replicated
}
}
}

I get strange data, so I am not sure I am implementing the right loop.
Can anybody please suggest the best loop in terms of accuracy and efficiency?
Regards
Marius
marius roma
Expert
 
Posts: 317
Liked: 3 times
Joined: Wed Feb 01, 2012 12:04 pm
Full Name: Mario

Re: Looping around jobs and VM

Veeam Logoby v.Eremin » Wed Nov 25, 2015 1:11 pm 1 person likes this post

You mean you want to understand how many times a VM was replicated successfully? Is it your real code or just an example with some omissions (meaning $Sessions and $Tasks assignment is not included in the provided example for the purpose of simplicity)?
v.Eremin
Veeam Software
 
Posts: 13291
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Looping around jobs and VM

Veeam Logoby marius roma » Wed Nov 25, 2015 1:13 pm

Thank you for your answer. In the meantime I corrected the code...
marius roma
Expert
 
Posts: 317
Liked: 3 times
Joined: Wed Feb 01, 2012 12:04 pm
Full Name: Mario

Re: Looping around jobs and VM

Veeam Logoby v.Eremin » Wed Nov 25, 2015 1:41 pm 1 person likes this post

What about my original question? Are you looking for getting some counter, suggesting how many time VM was replicated, or some different information regarding replication tasks should be provided? Thanks.
v.Eremin
Veeam Software
 
Posts: 13291
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Looping around jobs and VM

Veeam Logoby marius roma » Wed Nov 25, 2015 3:20 pm

Thank you for your question.
Once I get $task for every time each VM was replicated by each job I can get other information, i.e. the start and stop time, the replica status, and so on.
I removed such information from my scipt sample.
In my real script the information I get look incomplete or inaccurate, so I suspect there is some logic error in the loop...
That's the reason because I look for advice...
Regards
marius
marius roma
Expert
 
Posts: 317
Liked: 3 times
Joined: Wed Feb 01, 2012 12:04 pm
Full Name: Mario

Re: Looping around jobs and VM

Veeam Logoby v.Eremin » Wed Nov 25, 2015 4:10 pm 1 person likes this post

If I were you, I would write something like the following. Currently the table contains only job name, VM name (session name), and the session status. Feel free to add whatever session-specific information you want to:

Code: Select all
Asnp VeeamPSSnapin

$table = New-Object system.Data.DataTable “Replica info”
$col1 = New-Object system.Data.DataColumn JobName,([string])
$col2 = New-Object system.Data.DataColumn VMName,([string])
$col3 = New-Object system.Data.DataColumn SessionStatus,([string])

$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)


foreach ($Job in (Get-VBRJob | where {$_.JobType -eq "Replica"}))
    {
    foreach ($Session in (Get-VBRBackupSession | Where {$_.Jobname -eq $Job.name}))
        {
        foreach ($TaskSession in $Session.GetTaskSessions())
            {
                $row = $table.NewRow()         
                $row.JobName = $Job.Name
                $row.VmName = $TaskSession.name
                $row.SessionStatus = $TaskSession.Status
               
                $table.Rows.Add($row)
            }
        }
    }
$table


Thanks.
v.Eremin
Veeam Software
 
Posts: 13291
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Looping around jobs and VM

Veeam Logoby marius roma » Fri Nov 27, 2015 1:28 pm

Thank you very much.
It looks it works fine, as expected.
May I ask for am additional hint?
While:
Code: Select all
 $row.SessionStatus = $TaskSession.Status

returns the status of the replica (the VM was replicated or not or there was a warning), how can I get the amount of data that were transferred for a specific VM (in the report it is the "Transfered" column)?
Regards
Marius
marius roma
Expert
 
Posts: 317
Liked: 3 times
Joined: Wed Feb 01, 2012 12:04 pm
Full Name: Mario

Re: Looping around jobs and VM

Veeam Logoby v.Eremin » Fri Nov 27, 2015 1:46 pm 1 person likes this post

You will need to query .progress.TransferedSize property of Task Session:

Code: Select all
    Asnp VeeamPSSnapin

    $table = New-Object system.Data.DataTable “Replica info”
    $col1 = New-Object system.Data.DataColumn JobName,([string])
    $col2 = New-Object system.Data.DataColumn VMName,([string])
    $col3 = New-Object system.Data.DataColumn SessionStatus,([string])
    $col4 = New-Object system.Data.DataColumn Transferred,([string])

    $table.columns.add($col1)
    $table.columns.add($col2)
    $table.columns.add($col3)
    $table.columns.add($col4)


    foreach ($Job in (Get-VBRJob | where {$_.JobType -eq "Replica"}))
        {
        foreach ($Session in (Get-VBRBackupSession | Where {$_.Jobname -eq $Job.name}))
            {
            foreach ($TaskSession in $Session.GetTaskSessions())
                {
                    $row = $table.NewRow()         
                    $row.JobName = $Job.Name
                    $row.VmName = $TaskSession.name
                    $row.SessionStatus = $TaskSession.Status
                    $row.Transferred = $Session.progress.TransferedSize/1MB
                   
                    $table.Rows.Add($row)
                }
            }
        }
    $table


Thanks.
v.Eremin
Veeam Software
 
Posts: 13291
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Looping around jobs and VM

Veeam Logoby marius roma » Sat Nov 28, 2015 3:14 pm

Thanks again.
Marius
marius roma
Expert
 
Posts: 317
Liked: 3 times
Joined: Wed Feb 01, 2012 12:04 pm
Full Name: Mario

Re: Looping around jobs and VM

Veeam Logoby v.Eremin » Mon Nov 30, 2015 9:33 am 1 person likes this post

You're welcome. If you need any other task session specific property to be added to the table, just check task session entity, see what particular members it has, and then add the required property as an additional column (the same way the other four have been already added). Thanks.
v.Eremin
Veeam Software
 
Posts: 13291
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Looping around jobs and VM

Veeam Logoby marius roma » Mon Nov 30, 2015 10:38 am

Let me ask for some additional information:
The value I get using:
Code: Select all
$Session.progress.TransferedSize/1MB

is out of scale.
I made an attempt using:
Code: Select all
$TaskSession.progress.TransferedSize/1MB

but the results I get are not correct as well...
Am I missing any relevant point?
Regards
Marius
marius roma
Expert
 
Posts: 317
Liked: 3 times
Joined: Wed Feb 01, 2012 12:04 pm
Full Name: Mario

Re: Looping around jobs and VM

Veeam Logoby marius roma » Mon Nov 30, 2015 11:51 am

Let me provide an additional clarification: the data I need to get is the one displayied in the column "Transferred" for each VM in the report and is expressed in GB.
Regards
Marius
marius roma
Expert
 
Posts: 317
Liked: 3 times
Joined: Wed Feb 01, 2012 12:04 pm
Full Name: Mario

Re: Looping around jobs and VM

Veeam Logoby v.Eremin » Mon Nov 30, 2015 12:20 pm 1 person likes this post

Replace 1MB with 1Gb and provide values that are reported for the specific VM in both GUI and PowerShell (screenshots would be helpful):

Code: Select all
$Session.progress.TransferedSize/1Gb


Thanks.
v.Eremin
Veeam Software
 
Posts: 13291
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Looping around jobs and VM

Veeam Logoby marius roma » Mon Nov 30, 2015 8:49 pm

It works fine, many, many thanks!
marius
marius roma
Expert
 
Posts: 317
Liked: 3 times
Joined: Wed Feb 01, 2012 12:04 pm
Full Name: Mario

Re: Looping around jobs and VM

Veeam Logoby v.Eremin » Tue Dec 01, 2015 9:26 am

Glad to hear that. It's all about PowerShell byte conversion, in PowerShell you can divide the resulting number by 1KB, 1MB, 1GB, etc. whatever value you prefer. Thanks.
v.Eremin
Veeam Software
 
Posts: 13291
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 3 guests