arrays - 空值 powershell 数组

标签 arrays powershell powershell-2.0 powershell-3.0

我有一个奇怪的问题,这是我的 CSV:

Serveur;Carte;Cordon;IP;Mac;Vmnic ;Vmnic mac;Connect;Port
Dexter;eth1;405;172.16.5.117;00:24:e8:36:36:df;Vmnic0;00:50:56:56:36:df;sw-front-1;A1
Dexter;eth2;14;192.168.140.17;00:24:e8:36:36:e1;Vmnic1;00:50:56:56:36:e1; sw_eq_ds_1;3
;;;;;;;;
Gordon;eth1;404;172.16.5.124;b8:ac:6f:8d:ac:b4;Vmnic0;00:50:56:5d:ac:b4;;
Gordon;eth2;35;192.168.140.114;b8:ac:6f:8d:ac:b6;Vmnic1;00:50:56:5d:ac:b6;;
Gordon;eth3;254;192.168.33.10;b8:ac:6f:8d:ac:b8;Vmnic2;00:50:56:5d:ac:b8;;

所以我用下面的代码将它导入到一个数组中:

$Serveur = @()

Import-Csv C:\Users\aasif\Desktop\myfile.csv -Delimiter ";" |`
    ForEach-Object {
        $Serveur += $_.Serveur
    }

为了删除重复值,我这样做了:

$Serveur = $Serveur | sort -uniq

所以当我显示我的数组时,我获得了这两个值:Dexter 和 Gordon 以及第三个空值

但我也得到一个空值

下面的代码返回3

$Serveur.count

为什么?

谢谢你的帮助

最佳答案

如果你想排除空值你可以这样做

$Serveur = $Serveur |  ? { $_ } | sort -uniq

关于arrays - 空值 powershell 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17704779/

相关文章:

python - 检查 2 列表中有多少值相等

c - 如何从文件中读取未知数量的 float ?

powershell - 如何使用命令提示符或Powershell关闭投影机?

powershell - 无法在 Powershell 中使用 Get-ChildItem -Exclude 参数排除目录

sorting - powershellv2 - 如何在拆分字符串后对列表进行排序

javascript - 排序函数在放入 for 循环时表现不同

java - 需要为用户生成每个学生的卷号吗?

powershell - 如何通过输入包含文件路径的变量在 powershell 中打开文件?

powershell - 使用 powershell 获取实例描述和标签

powershell - 使用 C# 开发 PowerShell Cmdlet : where to place the "main code"