通过powershell导出AD用户及其组的列表

tkclm6bt  于 2022-12-13  发布在  Shell
关注(0)|答案(1)|浏览(246)

我希望通过powershell导出Active Directory中的用户及其组,但由于某种原因,我似乎无法使管道工作,我现在使用的powershell脚本是

`
$groups = get-adgroup -filter *

foreach ($group in $groups) {
$naam = $group.name
$members = Get-ADGroupMember -identity $group
write-host “Group: $naam”
write-host “————————————-”
foreach ($member in $members) {
$memnaam = $member.samaccountname
write-host “$naammem”
}`

我就是想不通有什么建议吗?

`
$groups = get-adgroup -filter *

foreach ($group in $groups) {
$naam = $group.name
$members = Get-ADGroupMember -identity $group
write-host “Group: $naam”
write-host “————————————-”
foreach ($member in $members) {
$memnaam = $member.samaccountname
write-host “$naammem”
} | Export-CSV c:\FileName.csv`
9rygscc1

9rygscc11#

检查变量名中的拼写错误(你有一个变量名),检查你是否关闭了所有的括号和大括号。更好的是,不要在不需要的地方使用变量:

$groups = get-adgroup -Filter *

# If you save your search as an object you won't need to re-run it multiple times to use the data
Write-Host "Processing groups"
$SearchResult = $Groups | ForEach-Object {
    $GroupName = $_.Name
    # Some simple error trapping
    Try {
        # Writing to host for informational only
        Write-Host "$GroupName..." -NoNewline
        $Members = (Get-ADGroupMember -Identity $_ -ErrorAction Stop).SamAccountName
    }
    Catch {
        $Members = $_
    }
    # Output results as an object
    [pscustomobject]@{
        Group = $GroupName
        Members = $Members
    }
    Write-Host "Done"
}
Write-Host "Processing complete"
# If you want to display it in console
$SearchResult | Format-List
# Or a GridView
$SearchResult | Out-GridView

相关问题