如何导出指定OU中的所有AD用户账号
How to export all AD user accounts in a specified OU
我正在尝试将一个 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
}
那个样本似乎有点极端。这应该有效:
$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"
我正在尝试将一个 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
}
那个样本似乎有点极端。这应该有效:
$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"