arrayspowershellveeam

Powershell array working abnormally with commands


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!


Solution

  • 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)
    

    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 }