【问题标题】:How can I log these Active Directory account operations to a CSV file?如何将这些 Active Directory 帐户操作记录到 CSV 文件?
【发布时间】:2020-06-06 10:20:00
【问题描述】:

我想为每个被禁用和移动的 AD 帐户创建一个 .csv 文件,其中包含移动对象的 $account.Distingushed 名称和移动对象的 $OU.Distinguished 名称。

处理这个问题的最佳方法是什么?

$OUs | Where-Object{$excludeOUS -notcontains $_.DistinguishedName  } | Foreach-Object {
        $params = @{}
        $params = @{
            SearchBase = [String]$_.DistinguishedName
            SearchScope = [String]"OneLevel"
            AccountInactive = $true
            TimeSpan = ([timespan]$days)
            Verbose = $true
        }   
        If($users) { 
            $params.Add("UsersOnly",$true)
        }
        ElseIf($computers) { 
            $params.Add("ComputersOnly",$true)
        }
        $accounts = Search-ADAccount @params
        foreach ($account in $accounts) {
            $params = @{}
            $params = @{
                Identity = [string]$account.DistinguishedName
                Verbose = $true
            }
            If ($noDisable -notcontains $account.Name -and $account.ObjectClass -eq "User" ) {
                Disable-ADAccount @params @whatIf

                $params.Add("Description",$description)

                Set-ADUser @params @WhatIf

                $params.Remove('Description')
                $params.Add("TargetPath", 'OU=Disabled Users,DC=test,DC=local')

                Move-ADObject @params @WhatIf
                # Somehow Export $account.DistinghuisedName and $OU.Distinguished name to .csv???
            }
            ElseIf ($noDisable -notcontains $account.Name -and $account.ObjectClass -eq "Computer") {

                Disable-ADAccount @params @whatIf

                $params.Add("Description",$description)

                Set-ADComputer @params @WhatIf

                $params.Remove('Description')
                $params.Add("TargetPath", 'OU=Disabled Computers,DC=test,DC=local')

                Move-ADObject @params @WhatIf
                # Somehow Export $account.DistinghuisedName and $OU.Distinguished name to .csv???

            }
        }
    }

【问题讨论】:

    标签: powershell csv logging active-directory export-csv


    【解决方案1】:

    您可以尝试以下代码(未经测试)。

    设置 csvPath、ouDistinguishedName 和 accountDistinguishedName 的变量。

    您可以将这些变量添加到对象并导出到 csv。我使用 $account.Name 作为 csv 名称,但您可以使用其他名称。

    $csvPath = "c:\temp"
    $OUs | Where-Object { $excludeOUS -notcontains $_.DistinguishedName } | Foreach-Object {
        $ouDistinguishedName = $_.DistinguishedName
        $params = @{ }
        $params = @{
            SearchBase      = [String]$_.DistinguishedName
            SearchScope     = [String]"OneLevel"
            AccountInactive = $true
            TimeSpan        = ([timespan]$days)
            Verbose         = $true
        }   
        If ($users) { 
            $params.Add("UsersOnly", $true)
        }
        ElseIf ($computers) { 
            $params.Add("ComputersOnly", $true)
        }
        $accounts = Search-ADAccount @params
        foreach ($account in $accounts) {
            $accountDistinguishedName = $account.DistinguishedName
            $accountName = $account.Name
            $params = @{ }
            $params = @{
                Identity = [string]$account.DistinguishedName
                Verbose  = $true
            }
            If ($noDisable -notcontains $account.Name -and $account.ObjectClass -eq "User" ) {
                Disable-ADAccount @params @whatIf
    
                $params.Add("Description", $description)
    
                Set-ADUser @params @WhatIf
    
                $params.Remove('Description')
                $params.Add("TargetPath", 'OU=Disabled Users,DC=test,DC=local')
    
                Move-ADObject @params @WhatIf
                # Somehow Export $account.DistinghuisedName and $OU.Distinguished name to .csv???
    
                $objectProperty = @{}
                $objectProperty.Add('Account',$accountDistinguishedName)
                $objectProperty.Add('OU',$ouDistinguishedName)
                $object = New-Object -TypeName psobject -Property $objectProperty
                $object | Export-Csv "$csvPath\$accountName.csv" -NoTypeInformation
            }
            ElseIf ($noDisable -notcontains $account.Name -and $account.ObjectClass -eq "Computer") {
    
                Disable-ADAccount @params @whatIf
    
                $params.Add("Description", $description)
    
                Set-ADComputer @params @WhatIf
    
                $params.Remove('Description')
                $params.Add("TargetPath", 'OU=Disabled Computers,DC=test,DC=local')
    
                Move-ADObject @params @WhatIf
                # Somehow Export $account.DistinghuisedName and $OU.Distinguished name to .csv???
    
                $objectProperty = @{}
                $objectProperty.Add('Account',$accountDistinguishedName)
                $objectProperty.Add('OU',$ouDistinguishedName)
                $object = New-Object -TypeName psobject -Property $objectProperty
                $object | Export-Csv "$csvPath\$accountName.csv" -NoTypeInformation
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您甚至可以将其直接导出为哈希表:

      @{"Account" = $accountDistinguishedName; "OU" = $ouDistinguishedName}.GetEnumerator() | Export-Csv "$($csvpath)\$($accountname)" -NoTypeInformation
      

      【讨论】:

        【解决方案3】:

        我想通了!

        $acctsCSV = @(
            [pscustomobject]@{
                Account = [string]$account.Name
                OU = [string]$OU.DistinguishedName
            }
        )
        $acctsCSV | Export-Csv -Path $filePath -NoTypeInformation
        

        【讨论】:

          猜你喜欢
          • 2011-05-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-02
          相关资源
          最近更新 更多