如何导出指定OU中的所有AD用户帐户

时间:2017-07-06 00:56:31

标签: powershell active-directory powergui

我正在尝试将OU中的所有AD用户导出到csv文件。但是,我不知道如何填写我从网站上提取的示例脚本中的前两个给定变量。

鉴于我的LDAP连接字符串为LDAP://OU=Admin Accounts, OU=Test Accounts,dc=sample,dc=domain,dc=com,我如何填写$path$pathexist的变量?

以下是示例脚本:

PROCESS #This is where the script executes 
{ 
$path = Split-Path -parent "$CSVReportPath\*.*" 
$pathexist = Test-Path -Path $path 
If ($pathexist -eq $false) 
{New-Item -type directory -Path $path} 

$reportdate = Get-Date -Format ssddmmyyyy 

$csvreportfile = $path + "\ALLADUsers_$reportdate.csv" 

#import the ActiveDirectory Module 
Import-Module ActiveDirectory 

#Perform AD search. The quotes "" used in $SearchLoc is essential 
#Without it, Export-ADUsers returuned error 
              Get-ADUser -server $ADServer -searchbase "$SearchLoc" -Properties * -Filter * |  
              Select-Object @{Label = "First Name";Expression = {$_.GivenName}},  
              @{Label = "Last Name";Expression = {$_.Surname}}, 
              @{Label = "Display Name";Expression = {$_.DisplayName}}, 
              @{Label = "Logon Name";Expression = {$_.sAMAccountName}}, 
              @{Label = "Full address";Expression = {$_.StreetAddress}}, 
              @{Label = "City";Expression = {$_.City}}, 
              @{Label = "State";Expression = {$_.st}}, 
              @{Label = "Post Code";Expression = {$_.PostalCode}}, 
              @{Label = "Country/Region";Expression = {if (($_.Country -eq 'GB')  ) {'United Kingdom'} Else {''}}}, 
              @{Label = "Job Title";Expression = {$_.Title}}, 
              @{Label = "Company";Expression = {$_.Company}}, 
              @{Label = "Description";Expression = {$_.Description}}, 
              @{Label = "Department";Expression = {$_.Department}}, 
              @{Label = "Office";Expression = {$_.OfficeName}}, 
              @{Label = "Phone";Expression = {$_.telephoneNumber}}, 
              @{Label = "Email";Expression = {$_.Mail}}, 
              @{Label = "Manager";Expression = {%{(Get-AdUser $_.Manager -server $ADServer -Properties DisplayName).DisplayName}}}, 
              @{Label = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}}, # the 'if statement# replaces $_.Enabled 
              @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}} |  

              #Export CSV report 
              Export-Csv -Path $csvreportfile -NoTypeInformation     
}

1 个答案:

答案 0 :(得分:1)

那个样本看起来有点极端。这应该有效:

$csvreportfile = "C:\Temp\Output.csv"
$ADServer = "DC1.domain.com"
Get-ADUser -server $ADServer -searchbase "OU=Admin Accounts, OU=Test Accounts,dc=sample,dc=domain,dc=com" -Properties * -Filter * |  
Select-Object @{Label = "First Name";Expression = {$_.GivenName}},  
@{Label = "Last Name";Expression = {$_.Surname}}, 
@{Label = "Display Name";Expression = {$_.DisplayName}}, 
@{Label = "Logon Name";Expression = {$_.sAMAccountName}}, 
@{Label = "Full address";Expression = {$_.StreetAddress}}, 
@{Label = "City";Expression = {$_.City}}, 
@{Label = "State";Expression = {$_.st}}, 
@{Label = "Post Code";Expression = {$_.PostalCode}}, 
@{Label = "Country/Region";Expression = {if (($_.Country -eq 'GB')  ) {'United Kingdom'} Else {''}}}, 
@{Label = "Job Title";Expression = {$_.Title}}, 
@{Label = "Company";Expression = {$_.Company}}, 
@{Label = "Description";Expression = {$_.Description}}, 
@{Label = "Department";Expression = {$_.Department}}, 
@{Label = "Office";Expression = {$_.OfficeName}}, 
@{Label = "Phone";Expression = {$_.telephoneNumber}}, 
@{Label = "Email";Expression = {$_.Mail}}, 
@{Label = "Manager";Expression = {%{(Get-AdUser $_.Manager -server $ADServer -Properties DisplayName).DisplayName}}}, 
@{Label = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}}, # the 'if statement# replaces $_.Enabled 
@{Label = "Last LogOn Date";Expression = {$_.lastlogondate}} |  

#Export CSV report 
Export-Csv -Path $csvreportfile -NoTypeInformation  

要解释$ Path和$ Pathexist变量,只需检查CSV导出的路径是否存在。如果它不存在,它就会创建它。您需要将$ CSVReportPath声明为变量,如:

$CSVReportPath = "C:\Temp"