Geo*_*ord 3 powershell user-management
我的职业是 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)
条件永远不匹配!我的脑袋疼!好的,这里可能有一些空白字符导致恶作剧,让我们修剪:
for ($i; $i -le $allusers.length; $i++) {
if ($allusers[$i].Trim() -eq "joeq") {
echo "joeq present"
}
}
Run Code Online (Sandbox Code Playgroud)
但是不,这会导致:
Method invocation failed because [Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData] doesn't contain a method named 'Trim'.
At C:\Users\dylanh\jirauserpurge.ps1:39 char:24
+ if ($allusers[$i].Trim <<<< () -eq "nickf") {
+ CategoryInfo : InvalidOperation: (Trim:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Run Code Online (Sandbox Code Playgroud)
显然,这是我缺少的某种类型转换问题。我需要做什么才能允许在“数组”中搜索特定值?
更新:在 fdibot 的回答和 HopelessN00b 的评论的帮助下,我有一个可行的解决方案:
$allusers= get-aduser -Filter {Enabled -eq $true}
$usernames = @()
for ($i=0; $i -le $allusers.length; $i++) {
$usernames += $allusers[$i].SamAccountName
}
$usernames -contains "joeq"
Run Code Online (Sandbox Code Playgroud)
额外的 for 循环似乎有点多余 TBH(或者至少它会在 bash 中!)啊好吧!感谢大家的意见。
小智 8
问题出在您的格式表上 :)
$allusers= get-aduser -Filter {Enabled -eq $true} | FT samAccountName
$allusers | gm
Run Code Online (Sandbox Code Playgroud)
类型名称:Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
名称 MemberType 定义 ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode
Method int GetHashCode() GetType
Method type GetType() ToString
方法字符串的ToString()autosizeInfo
物业Microsoft.PowerShell.Commands.Internal.Format.AutosizeInfo,SYSTE ... ClassId2e4f51ef21dd47e99d3c952918aff9cd属性字符串ClassId2e4f51ef21dd47e99d3c952918aff9cd {获得;} groupingEntry
物业
Microsoft.PowerShell.Commands.Internal.Format.GroupingEntry,SYST ... pageFooterEntry财产
Microsoft.PowerShell.Commands.Internal.Format.PageFooterEntry, Sy... pageHeaderEntry 属性 Microsoft.PowerShell.Commands.Internal.Format.PageHeaderEntry, Sy... shapeInfo
属性
Microsoft.PowerShell.Commands.Internal.Format.ShapeInfo, System.米...
这不是您要搜索的用户名。
如果你这样做
$allusers= get-aduser -Filter {Enabled -eq $true}
$allusers | gm
Run Code Online (Sandbox Code Playgroud)
您将获得一个包含所有属性的对象集合。在这里你可以用 if 那样检查
$allusers.SamAccountName -eq "joeq"
Run Code Online (Sandbox Code Playgroud)
现在你可以继续你的脚本,并循环所有用户列表,试试这个:
get-aduser -Filter {Enabled -eq $true} | Foreach {
if ($_.SamAccountName -eq "something") { "OK" }
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4230 次 |
最近记录: |