为什么PowerShell创建一个Char数组而不是一个String数组? [英] Why is PowerShell creating a Char Array instead of a String Array?

查看:49
本文介绍了为什么PowerShell创建一个Char数组而不是一个String数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在数组的数组内输出元素时(例如:$ data_array [0] [0],我只会得到一个char.为什么会这样呢?我期望[0]的字符串为LAP-150 [0]该数组的位置.

When I output elements inside an array of an array (example: $data_array[0][0], I only get a char. Why is this? I was expecting a String of LAP-150 for the [0][0] position of this array.

import-module activedirectory
$domain_laptops = get-adcomputer -filter 'Name -like "LAP-150"' -properties operatingsystem, description | select name, description, operatingsystem
$data_array = @()

foreach ($laptop in $domain_laptops){
        $bde = manage-bde -computername $laptop.name -status
        $encryptionstatus=(manage-bde -status -computername $laptop.name | where {$_ -match 'Conversion Status'})
        if ($encryptionstatus){
            $encryptionStatus=$encryptionstatus.split(":")[1].trim()
        }
        else{
            $EncryptionStatus="Not Found..."
        }
        $data_array += ,($laptop.name,$laptop.description,$laptop.operatingsystem,$encryptionstatus)
    }


write-output $data_array[0][0]

上面脚本的输出只是字符"L",它是$ laptop.name变量中的第一个字符.我要去哪里错了?我认为这与我追加到数组的方式有关,但是我尝试了括号,逗号,无括号等的不同组合,但无济于事.

The output of the above script is just the character "L" which is the first character in the $laptop.name variable. Where am I going wrong? I assume it's something to do with how I'm appending to the array but I've tried different combinations of parenthesis, commas, no parenthesis, etc to no avail.

推荐答案

运行以下命令时,

$data_array += ($laptop.name,$laptop.description,$laptop.operatingsystem,$encryptionstatus)

+ = 符号后删除.

执行的测试,向您展示其工作原理

Tests performed to show you how it works

$array = @()
$array = 1, 2, 3, 4
$array.Length   //-> 4

$array2 = @()
$array2 = , 1, 2
$array2.Length  //-> 2

$array3 = @()
$array3 = , (1, 2)
$array3.Length  //-> 1

$array4 = @()
$array4= @(), (1, 2)
$array4.Length  //-> 2

使用时,必须在前后定义相同类型的元素.在迭代过程中,您正在使用 + =,(某物).的左侧,没有任何数据,因此它后面的所有文本都被视为用逗号分隔的字符串.

When you use ,, you have to define the same type of element before and after. During your iterations, you are using += , (something). Left of , doesnt have any data so all the text after it is considered a string seperated by commas.

对于2D数组,我建议在混合中使用哈希,

For 2D arrays, i would recommend using the hash in the mix,

$data_array += @{name=$laptop.name;description=$laptop.description;os=$laptop.operatingsystem;encryption=$encryptionstatus}

$data_array[0]["name"] // Prints the name of first laptop in array.

这篇关于为什么PowerShell创建一个Char数组而不是一个String数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆