PowerShell script exchange
Post Reply
marius roma
Veteran
Posts: 459
Liked: 5 times
Joined: Feb 01, 2012 12:04 pm
Full Name: Mario
Contact:

Looping around jobs and VM

Post by marius roma »

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
veremin
Product Manager
Posts: 20389
Liked: 2297 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Looping around jobs and VM

Post by veremin » 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)?
marius roma
Veteran
Posts: 459
Liked: 5 times
Joined: Feb 01, 2012 12:04 pm
Full Name: Mario
Contact:

Re: Looping around jobs and VM

Post by marius roma »

Thank you for your answer. In the meantime I corrected the code...
veremin
Product Manager
Posts: 20389
Liked: 2297 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Looping around jobs and VM

Post by veremin » 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.
marius roma
Veteran
Posts: 459
Liked: 5 times
Joined: Feb 01, 2012 12:04 pm
Full Name: Mario
Contact:

Re: Looping around jobs and VM

Post by marius roma »

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
veremin
Product Manager
Posts: 20389
Liked: 2297 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Looping around jobs and VM

Post by veremin » 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.
marius roma
Veteran
Posts: 459
Liked: 5 times
Joined: Feb 01, 2012 12:04 pm
Full Name: Mario
Contact:

Re: Looping around jobs and VM

Post by marius roma »

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
veremin
Product Manager
Posts: 20389
Liked: 2297 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Looping around jobs and VM

Post by veremin » 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.
marius roma
Veteran
Posts: 459
Liked: 5 times
Joined: Feb 01, 2012 12:04 pm
Full Name: Mario
Contact:

Re: Looping around jobs and VM

Post by marius roma »

Thanks again.
Marius
veremin
Product Manager
Posts: 20389
Liked: 2297 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Looping around jobs and VM

Post by veremin » 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.
marius roma
Veteran
Posts: 459
Liked: 5 times
Joined: Feb 01, 2012 12:04 pm
Full Name: Mario
Contact:

Re: Looping around jobs and VM

Post by marius roma »

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
Veteran
Posts: 459
Liked: 5 times
Joined: Feb 01, 2012 12:04 pm
Full Name: Mario
Contact:

Re: Looping around jobs and VM

Post by marius roma »

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
veremin
Product Manager
Posts: 20389
Liked: 2297 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Looping around jobs and VM

Post by veremin » 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.
marius roma
Veteran
Posts: 459
Liked: 5 times
Joined: Feb 01, 2012 12:04 pm
Full Name: Mario
Contact:

Re: Looping around jobs and VM

Post by marius roma »

It works fine, many, many thanks!
marius
veremin
Product Manager
Posts: 20389
Liked: 2297 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Looping around jobs and VM

Post by veremin »

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.
naveennkumar
Novice
Posts: 4
Liked: never
Joined: Aug 21, 2018 8:00 am
Full Name: Naveen Kumar
Contact:

[MERGED] VEEAM POWERSHELL script

Post by naveennkumar »

Hi All, I am New to VEEAM and POWERSHELL. Is there a way/script to get list of VM's of which read and transferred size is 0.0 B from all backup schedules.
PTide
Product Manager
Posts: 6551
Liked: 765 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Looping around jobs and VM

Post by PTide »

Hi,

First of all, you need to get the list of sessions:

Code: Select all

$sessions = get-vbrbackupsession
After that you can query every of them so it prints out the 'transferred' values for every session:

Code: Select all

foreach ($session in $sessions) {
$transfer = $session.progress.Transferred/1Gb
$transfer
}
You need to modify the code so that it checks if the value is equal to zero.

Thanks
naveennkumar
Novice
Posts: 4
Liked: never
Joined: Aug 21, 2018 8:00 am
Full Name: Naveen Kumar
Contact:

Re: Looping around jobs and VM

Post by naveennkumar »

Hello PTide,

Thanks for the reply, above command doesnt provide the VM list with 0.0 B backup, just through's list of 0's.
PTide
Product Manager
Posts: 6551
Liked: 765 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Looping around jobs and VM

Post by PTide »

That's werid. The following code should return you all sessions with zero transfered size + job names and sessions start times:

Code: Select all

$sessions = get-vbrbackupsession
foreach ($session in $sessions)
{
 if ($session.progress.TransferedSize -eq 0)
 {
   $session.JobName + ' session ' + $session.Info.CreationTime + ' TransferSize is zero'
 }
}
Would you try to run it, please?

Thanks
naveennkumar
Novice
Posts: 4
Liked: never
Joined: Aug 21, 2018 8:00 am
Full Name: Naveen Kumar
Contact:

Re: Looping around jobs and VM

Post by naveennkumar »

Thanks again, below is the output i got from the command.. I need to get VM names in place of JOB name.

DLY_SCHED_VMBKP_ABFRL_CRITICAL_MSSQL_JOB20 session 02/21/2018 02:24:15 TransferSize is zero
DLY_SCHED_VMBKP_ABFRL_CRITICAL_MSSQL_JOB21 session 02/26/2018 02:16:42 TransferSize is zero
DLY_SCHED_VMBKP_ABFRL_CRITICAL_MSSQL_JOB23 session 02/21/2018 04:19:53 TransferSize is zero
DLY_SCHED_VMBKP_ABFRL_CRITICAL_VMTOOLS_JOB03 session 06/17/2018 20:05:46 TransferSize is zero
DLY_SCHED_VMBKP_ABFRL_CRITICAL_LINUX_JOB12 session 05/15/2018 00:07:07 TransferSize is zero
DLY_SCHED_VMBKP_ABFRL_CRITICAL_VMTOOLS_JOB04 session 03/05/2018 04:38:54 TransferSize is zero
DLY_SCHED_VMBKP_ABFRL_CRITICAL_VMTOOLS_JOB04 session 02/27/2018 20:52:18 TransferSize is zero
DLY_SCHED_VMBKP_ABFRL_CRITICAL_VMTOOLS_JOB03 session 05/14/2018 20:35:09 TransferSize is zero
DLY_SCHED_VMBKP_ABFRL_CRITICAL_VMTOOLS_JOB04 session 01/15/2018 21:19:06 TransferSize is zero
PTide
Product Manager
Posts: 6551
Liked: 765 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Looping around jobs and VM

Post by PTide »

I'm afraid it might be not possible to get those stats per-VM. Would you tell us why do you need that info? Are you trying to teck down those servers that do not generate any changes on their drives?

Thanks
naveennkumar
Novice
Posts: 4
Liked: never
Joined: Aug 21, 2018 8:00 am
Full Name: Naveen Kumar
Contact:

Re: Looping around jobs and VM

Post by naveennkumar »

Yes, VM team wants to identify those VMs and check if those VMs need backups.
veremin
Product Manager
Posts: 20389
Liked: 2297 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Looping around jobs and VM

Post by veremin »

You can query task sessions (Get-VBRTaskSession) for job session which has transferred size equal to zero, and VM names from them. For example,

Code: Select all

Get-VBRTaskSession -Session $Session | select name
Thanks.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests