小编Geo*_*ord的帖子

Powershell 数组和比较

我的职业是 Linux 系统管理员,但我不得不做一些 powershelling。我的问题是这样的:

我使用以下命令从 AD 获取活动用户列表:

$allusers= get-aduser -Filter {Enabled -eq $true} | FT samAccountName 
Run Code Online (Sandbox Code Playgroud)

我的意图是 $allusers 变成一个数组,其中填充了 get-aduser 命令的输出行。在填充数组后立即弹出此块似乎确认确实如此。

for ($i; $i -le $allusers.length; $i++) {
    $allusers[$i]
}
Run Code Online (Sandbox Code Playgroud)

伟大的!所以现在我想检查给定的用户名是否存在于该数组中。我看到数组包含一个非常方便的“Contains()”函数,看起来它应该符合我的目的。

echo $allusers.Contains("joeq")
Run Code Online (Sandbox Code Playgroud)

可惜!我收到此错误:

Method invocation failed because [System.Object[]] doesn't contain a method named 'contains'.
Run Code Online (Sandbox Code Playgroud)

在 C:\Users\dylanh\jirauserpurge.ps1:33 char:24 + echo $allusers.contains <<<< ("joeq") + CategoryInfo : InvalidOperation: (contains:String) [], RuntimeException +fullyQualifiedErrorId : MethodNotFound

嗯...好吧,让我们去“老skool”

for ($i; $i -le $allusers.length; $i++) {
    if ($allusers[$i] -eq "joeq") {
        echo "joeq present"
    } …
Run Code Online (Sandbox Code Playgroud)

powershell user-management

3
推荐指数
1
解决办法
4230
查看次数

标签 统计

powershell ×1

user-management ×1