[英]Powershell Script that takes username and checks security groups
I want to create a powershell script that checks a users profile which security groups the user belongs to inside the active directory. 我想创建一个Powershell脚本,该脚本检查用户个人资料,该用户属于活动目录内的安全组。 Then I want to write it to a file. 然后,我想将其写入文件。 I'm having trouble solving this problem. 我无法解决此问题。
Example of the expected output. 预期输出的示例。
User1 用户1
Domain users Domain admins... 域用户域管理员...
User2 用户2
Domain users Enterprise admins 域用户企业管理员
New-Item "$home\Desktop\GroupsMembers-list $(get-date -f dd-MM-yyyy).txt"
$users = @("User1", "User2", "User3", "User4")
foreach ($user in $users){
Get-ADPrincipalGroupMembership $user | sort name | select name | Out-File "$home\Desktop\GroupsMembers-list $(get-date -f dd-MM-yyyy).txt"
}
I imagine you could get a bit more creative with it but this could definitely work for you: 我想您可以从中获得更多创意,但这绝对可以为您工作:
$users = @("user1","user2")
foreach ($user in $users){
$groups = Get-ADPrincipalGroupMembership $user | Where-Object {$_.GroupCategory -eq "Security"} | Sort-Object Name | Select-Object -ExpandProperty Name
Add-Content -Path C:\path\to\file.txt -Value @"
$user
Security Groups
----------------------------
"@
foreach ($group in $groups){
Add-Content -Path C:\path\to\file.txt -Value $group
}
}
This is what the output looks like: 输出如下所示:
user1
Security Groups
----------------------------
Administrators
Domain Admins
user2
Security Groups
----------------------------
Administrators
Domain Admins
I think it will be something like this: 我认为会是这样的:
$users = @("User1", "User2", "User3", "User4")
$file= "$home\Desktop\GroupsMembers-list $(get-date -f dd-MM-yyyy).txt"
""| Out-File $file; #Clear content of file
foreach ($user in $users){ ""| Out-File -Append $file; #Add a new line
"$User is in Security groups:"| Out-File -Append $file; # making a Title
"---------------------------------"| Out-File -Append $file; # making a Title
Get-ADPrincipalGroupMembership $user | sort Name | ft -HideTableHeaders Name | Out-File -Append $file #Writing data
}
The txt file will be replaced each time, when running script. 每次运行脚本时,都会替换txt文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.