简体   繁体   English

如何将这些 Active Directory 帐户操作记录到 CSV 文件中?

[英]How can I log these Active Directory account operations to a CSV file?

I want to create a .csv file for each AD account that is disabled and moved that has the $account.Distingushed name of the object move and the $OU.Distinguished name that the object is moved from.我想为每个被禁用和移动的 AD 帐户创建一个.csv文件,该文件具有对象移动的$account.Distingushed名称和对象$OU.Distinguished移动的$OU.Distinguished名称。

What would be the best way to handle this?处理这个问题的最佳方法是什么?

$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???

            }
        }
    }

You can try something like below code (untested).您可以尝试类似下面的代码(未经测试)。

Set the variables for the csvPath, ouDistinguishedName and accountDistinguishedName.为 csvPath、ouDistinguishedName 和 accountDistinguishedName 设置变量。

You can add those vars to an object and export to csv.您可以将这些变量添加到对象并导出到 csv。 I used $account.Name as the csv name, but you can use something else.我使用 $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
        }
    }
}

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

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

I figured it out!我想到了!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将以下 CSV 文件转换为 Active Directory 用户 - How can I turn the following CSV file into Active Directory Users 如何使用Powershell从csv文件更新Active Directory中的更多数据行? - How can I update more data rows in Active Directory from a csv file using Powershell? 我如何在Active Directory中修改此powershell代码,以便在从CSV文件中清空单元格时能够更改属性? - How can I modify this powershell code in Active Directory, to be able to change attributes when emply cells from csv file? 如何将Microsoft帐户添加到Azure Active Directory? - How do I add a Microsoft account to Azure Active Directory? 如何从CSV列中读取用户名并将Active Directory中的相关数据添加到他们的行中? - How can I read in user names from a CSV column and add relevant data from Active Directory to their row? 如何将活动目录数据导入数组并将其导出到单个CSV文件中? - How to import active directory data into an array and export that into a single CSV file? 如何替换所有Active Directory帐户照片? - How to replace all Active Directory account photos? 我如何从附加的 CSV 文件中的 DistinguishedName 列中过滤“OU = 服务帐户”:powershell - How can i Filter “OU=Service account” from column DistinguishedName from a CSV file attached : powershell 如何获取日志目录? - How can i get the Log directory? 如何从 CSV 文件中的 DistinguishedName 过滤“OU=Service account” - How can i Filter “OU=Service account” from DistinguishedName in CSV file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM