2

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?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

0

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

3 Comments

Do you want the output from each job to be added to the same object? What is the actual expected result, using the values in this answer? My understanding is that C1 to C6 are stored as a single item within $vmnames, based on this output.
Yes i want to add all the output from $jobs to each single item in $vmnames. So that i have list where I can see VM1 is in Job1, VM2 is in Job1 and for every VM is created a new line.
Hmmm, try explicitly casting $vmnames to an array: [array]$vmnames = ($job.GetObjectsInJob()).name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.