【问题标题】:PowerShell turning an if else statement to a Foreach LoopPowerShell 将 if else 语句转换为 Foreach 循环
【发布时间】:2016-07-12 08:37:25
【问题描述】:

我需要删除一些不再存在于 AD 中但仍安装到应用程序中的用户。

我有一个脚本,它提供了一个用户列表并将它们分配给一个变量。目前我想要一个不存在的用户列表。这适用于单个用户:

#One user in an Application 

$Users = $location.Users.FindAll() | where {$_.username -like "*johnsmith*"} | select username 

Username                                                                                                                                                                        
--------                                                                                                                                                                        

JohnSmith


#See if user exists in AD (Match Username with AD UserPrincipalName)

$users = $repository.Users.FindAll() |where {$_.username -like "*johnp*"} | select username 

$users2 = Get-ADUser -Filter "UserPrincipalName -eq '$($users.username)'" -Properties  LastLogonDate

if (!$users2 )
{
Write-Host "$users doesnt exist"
}

else
{
Write-Host "$users does exist"
}


> @{Username=JohnSmith} doesnt exist

我遇到的问题是将其提供给变量中多个用户的 foreach 循环。我怎样才能让这个查看每个用户的对象并输出不存在的对象?

#List of all users in Appication

$Users = $location.Users.FindAll() | select username 

Username                                                                                                                                                                        
--------                                                                                                                                                                        

JohnSmith
JohnSmith1
JohnSmith2

试过这个,但不起作用,显示所有用户都存在,即使上面不存在 JohnSmith:

$users = $location.Users.FindAll() |select username 

$users2 = Get-ADUser -Filter "UserPrincipalName -eq '$($users.username)'"

foreach ($users2 in $users){
if (!$users2 )
 {
 write-host "$users2 doesnt exist"
}

else
{
write-host "$users2 does exist"
}
}

PS C:\>
@{Username=JohnSmith} does exist
@{Username=JohnSmith1} does exist
@{Username=JohnSmith2} does exist

【问题讨论】:

    标签: powershell if-statement foreach scripting


    【解决方案1】:

    未测试,但这应该会返回不在 AD 中的所有应用程序用户的列表:

    $applicationUsers = $location.Users.FindAll() | select username 
    $adUsers = Get-ADUser -filter * | select -ExpandProperty UserPrincipalName
    $users = $applicationUsers | Where username -NotIn $adUsers
    

    您可以使用以下方法迭代它们:

    $users | Foreach-Object { $_ }
    

    【讨论】:

    • Get-ADUser -filter * 是一种非常糟糕的做法。无法想象这需要多长时间才能在我的 AD 中完成超过九千(严重)用户类型的对象。您必须指定一些条件,否则检索所有用户对象可能需要很长时间。我刚刚检查过:2 分 15 秒和大约 500mb 的 RAM。
    • @n01d 这本身并不是一个坏习惯。这取决于他的环境,我们不知道,所以如果需要,我无法给他正确的标准来提高整体表现。
    • @Martin Brandl,在我看来,搜索 AD 中的每个用户比检索整个 AD 更好。任何状况之下。除非您在目录中有约 100 个用户对象。但由于我们不了解环境,所以没有理由给出这样的建议。只是根据我的个人实践得出的看法。
    • @n01d 不过,这取决于他的环境,并且由于他的回答,这个解决方案似乎非常适合他,因此我不明白反对意见。我还确定使用此 cmdlet 的人知道-filter 参数,并且能够在需要时采用它。在我的公司(18 个用户,36 个 AD 对象),这比搜索每个用户更有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2019-12-12
    • 2021-12-13
    相关资源
    最近更新 更多