简体   繁体   English

Powershell阵列无法正常使用命令

[英]Powershell array working abnormally with commands

Coming from Linux, I'm trying to automate the gathering of Veeam Backup jobs with a PowerShell script. 来自Linux,我正在尝试使用PowerShell脚本自动收集Veeam Backup作业。

I started to see how PowerShell arrays work, then I tried with a small test: 我开始了解PowerShell数组的工作方式,然后尝试了一个小测试:

$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: 但是当我修改我的2个数组时,添加命令而不是字符串:

$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! 我可能做错了!

PowerShell is working perfectly normally and doing exactly what you told it to do: PowerShell可以正常正常运行,并且完全按照您的要求执行操作:

$jobname = @(gwmi -Namespace "root/veeambs" -Class "job" | select typetostring, name | ft -HideTableHeaders | Out-String)
  • List jobs via WMI. 通过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 }

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 .. 由于我没有Veeam,因此我无法真正为您提供直接的工作样本,但..

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. 我确定您将能够使用相同的方法来解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM