0
votes

Coming from Linux, I'm trying to automate the gathering of Veeam Backup jobs with a PowerShell script.

I started to see how PowerShell arrays work, then I tried with a small test:

$users = 'mark','josh','stephen'
$city = 'seattle','boston','chicago'

for ($i = 0; $i -lt $users.Count; $i++) {
    Write-Host  $users[$i] 'live in' $city[$i]
}

Output:

mark live in seattle
josh live in boston
stephen live in chicago

But when I modify my 2 arrays adding command instead of strings:

$jobname = @(gwmi -Namespace "root/veeambs" -Class "job" | select typetostring, name | ft -HideTableHeaders | Out-String)
$isenabled = @(gwmi -Namespace "root/veeambs" -Class "job" | select typetostring, scheduleenabled | ft -HideTableHeaders | Out-String)

for ($i = 0; $i -lt $jobname.Count; $i++) {
    echo $jobname[$i] ' --> ' $isenabled[$i]
}

Output is:

             Job1
             Job2
             Job3
             Job4
             Job5
             Job6
             Job7
             Job8
             Job9
             Job10
             Job11
             Job12
             Job13
             Job14
             JOb15
             Job16
             Job17
             Job18
             Job19
             Job20

 -->

                   False
                    True
                    True
                   False
                    True
                    True
                   False
                    True
                    True
                    True
                    True
                    True
                    True
                    True
                    True
                    True
                    True
                    True
                    True
                    True

instead of:

Job1 --> False
Job2 --> True
Job3 --> True
Job4 --> False
etc.

Running the entire command manually I get:

PS> gwmi -Namespace "root/veeambs" -Class "job" | select typetostring, name, scheduleenabled

typetostring name                            scheduleenabled
------------ ----                            ---------------
             Job1                                 False
             Job2                                 True
             Job3                                 True
             Job4                                 False
             Job5                                 True
             Job6                                 True
             Job7                                 False
             Job8                                 True
             Job9                                 True
             Job10                                True
             Job11                                True
             Job12                                True
             Job13                                True
             Job14                                True
             Job15                                True
             Job16                                True
             Job17                                True
             Job18                                True
             Job19                                True
             Job20                                True

I'm probably doing something wrong!

2
@Jonas Please don't answer in comments.vonPryz
soz. removed & moved.Jonas

2 Answers

2
votes

PowerShell is working perfectly normally and doing exactly what you told it to do:

$jobname = @(gwmi -Namespace "root/veeambs" -Class "job" | select typetostring, name | ft -HideTableHeaders | Out-String)
  • List jobs via WMI.
  • Select two properties.
  • Format the selected properties as a table.
  • Turn the entire table into a single string.
  • Put that string into an array.

The result is an array with one string element containing the entire table. Hence you're getting

<table1> --> <table2>

rather than

<table1_item1> --> <table2_item1>
<table1_item2> --> <table2_item2>
...

What you actually want is something like this:

Get-WmiObject -Namespace "root/veeambs" -Class "job" |
    Select-Object name, scheduleenabled |
    ForEach-Object { '{0} --> {1}' -f $_.name, $_.scheduleenabled }
0
votes

You do not need to fetch the information twice and combine it. If you get the object the information you need will be there. You'll also need to interate over the properties of your object and not over the object itself, even if you specify that the variable is an array it will be a single object in that array hence the first entry in the array is the full object.

Since i do not have Veeam I cannot really provide you with an direct working sample of that but ..

Ie.

$Computer = @(Get-WmiObject -Class Win32_ComputerSystem)

$Computer.PSObject.Properties | Where-Object {$_.Name -like "Is*"} | ForEach-Object {
    Write-Host $_.Name "-->" $_.Value

}

Will output ...

IsReadOnly --> False
IsFixedSize --> True
IsSynchronized --> False

Im sure you will be able to use the same method to solve your problem.