Stefan
Stefan

Reputation: 23

Looping to a script

I have a problem using loops to add somthing to an object.

This part works:

# Get Backup Jobs
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}
foreach ($job in $jobs) {
$jobOptions = New-Object PSObject
$jobOptions | Add-Member -MemberType NoteProperty -Name "JobName" -value $job.name
$jobOptions | Add-Member -MemberType NoteProperty -Name "Enabled" -value $job.isscheduleenabled

Output:

Jobname,Enabled,...    
Job1,True,...

What I want is:

VMName,Jobname,Enabled

VM1,Job1,True,...

VM2,Job1,True,...

VM3,Job2,True,...

I tried:

# Get Backup Jobs
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}
foreach ($job in $jobs) {

$vmnames = ($job.GetObjectsInJob()).name
$jobOptions = New-Object PSObject
$jobOptions | Add-Member -MemberType NoteProperty -Name "VMName" -value 
$vmnames

$jobOptions | Add-Member -MemberType NoteProperty -Name "JobName" -value 
$job.name
$jobOptions | Add-Member -MemberType NoteProperty -Name "Enabled" -value   
$job.isscheduleenabled

This creates:

VMName,Jobname,Enabled

VM1 VM2,Job1,True,...

What do I have to change?

Upvotes: 2

Views: 254

Answers (4)

Walter Mitty
Walter Mitty

Reputation: 18940

In the inner loop, I would try looping through the objects instead of looping through the names. Something along these lines:

# Get Backup Jobs
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}

foreach ($job in $jobs) {
    # change starts here; additional loop over VMs
    foreach ($vm in $job.GetObjectsInJob()){

        [array]$jobOptions += New-Object PSObject -Property @{
            "VMName"  = $vm.name
            "JobName" = $job.name
            "Enabled" = $job.isscheduleenabled
        }

    }
}

I borrowed most of the code from gms0ulman's response.

Upvotes: 0

Stefan
Stefan

Reputation: 23

Martins answer produce this output.

    VMName                                        JobName               Enabled
    ------                                        -------               -------
S01                                           Backup Job Server1       True
S02                                           Backup Job Server2       True
{C1, C2, C3, C4, C5, C6}                     Backup Job Clients       True

gms0ulman answer produce this output.

VMName                                   JobName               Enabled
------                                   -------               -------
S01                                 Backup Job Server1       True
S02                                 Backup Job Server2       True
C1 C2 C3 C4 C5 C6                   Backup Job Clients       True

Upvotes: 0

G42
G42

Reputation: 10019

I would loop over the VMs like so, and use += to add to $jobOptions:

# Get Backup Jobs
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}

foreach ($job in $jobs) {

    $vmnames = ($job.GetObjectsInJob()).name

    # change starts here; additional loop over VMs
    foreach ($vm in $vmnames){

        [array]$jobOptions += New-Object PSObject -Property @{
            "VMName"  = $vm
            "JobName" = $job.name
            "Enabled" = $job.isscheduleenabled
        }

    }
}

Disclaimer: I have not used jobs in PowerShell, but don't see how this would affect the loop logic.

Upvotes: 0

Martin Brandl
Martin Brandl

Reputation: 58931

You could use calculated properties:

$jobs = Get-VBRJob | 
    Where-Object JobType -eq "Backup" |
    Select-Object @{l="VMName"; e={($_.GetObjectsInJob()).name}}, 
    @{l="JobName"; e={$_.name}}, 
    @{l="Enabled"; e={$_.isscheduleenabled}}

Note: I assume that $job.GetObjectsInJob().name returns the VMName.

Upvotes: 2

Related Questions