我想创建一个Powershell脚本,该脚本检查用户个人资料,该用户属于活动目录内的安全组。然后,我想将其写入文件。 我无法解决此问题。
预期输出示例。
User1
域用户 域管理员...
User2
域用户 企业管理员
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"
}
答案 0 :(得分:1)
我想您可以从中获得更多创意,但这绝对可以为您工作:
$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
}
}
输出结果如下:
user1
Security Groups
----------------------------
Administrators
Domain Admins
user2
Security Groups
----------------------------
Administrators
Domain Admins
答案 1 :(得分:0)
我认为将会是这样:
from urllib.request urlopen as uReq
每次运行脚本时,都会替换txt文件。