2017-07-06 69 views
0

嘗試在OU中的所有AD用戶導出爲CSV文件。但是,我不知道如何填寫我從網站中提取的示例腳本中的前兩個給定變量。我如何所有AD用戶帳戶導出指定的OU

鑑於我的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

那樣品似乎有點極端。這應該工作:

$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 

爲了解釋$路徑和$ Pathexist變量,它只是檢查是否存在導出的CSV文件的路徑。如果它不存在,它會創建它。您將需要申報$ CSVReportPath作爲一個變量,如下所示:

$CSVReportPath = "C:\Temp" 
相關問題