19 Ocak 2015 Pazartesi

Active Directory: Export Users Information to a CSV file

Aktif Dizin kullanıcılarının tamamını .csv formatında kaydetmek için aşağıdaki kod satırlarından bir tanesi yeterlidir.






Get-ADUser -Filter * -Properties * | Export-Csv -delimiter ";" -path "C:\AllUsers.csv"

veya






Get-ADUser -Filter * -SearchBase "ou=Users,dc=SiberBlog,dc=org" -Properties * | Export-Csv -delimiter ";" -path "C:\AllUsers.csv"

veya






DsQuery * -filter "&(objectClass=User)(objectCategory=Person)" –attr samAccountName Name >>AllUsers.csv

Kodu biraz geliştirebiliriz. Kullanıcıya ait bilgilerden hangilerine ihtiyaç duyuluyor ise onları seçebiliriz.


 

Script Versiyon 1 (AllUsersExpExcel_V1.ps1) Kaynak Kod:






# ===========================================================
# NAME: AllUsersExpExcel_V1.ps1
# AUTHOR: Bekir Yalçın
# DATE: 19/01/2015
# COMMENT: Tüm Aktif Dizin kullanıcılarına ait bilgilerin .txt ve .csv formatında dışa aktarım.
# VERSION: 1
# LINK: http://siberblog.org/index.php/active-directory-export-users-information-to-a-csv-file
# ===========================================================Import-Module ActiveDirectory
Clear#Rapor dosya adina tarihi ekliyoruz.
$ReportFile1 = ( "AllUsers" + (Get-Date –f "_yyyyMMdd_HHmmss").tostring() + ".txt" )
$ReportFile2 = ( "AllUsers" + (Get-Date –f "_yyyyMMdd_HHmmss").tostring() + ".csv" )
$Kayitlar=@()# Administrator kullanıcısı hariç
$UserList = Get-ADUser -Filter * -Properties *

# Sadece belirlenen bir OU altındaki kullanıcılar için aşağıdaki satırları kullanabilirsiniz.
# $OrganizationUnit="OU=KULLANICILAR,DC=SIBERBLOG,DC=ORG"
# $UserList = Get-ADUser -SearchBase $OrganizationUnit -Properties *

Foreach($User in $UserList)
{
Write-Host $User.SamAccountName

$Dosya=New-Object PSObject -Property @{
SamAccountName = $User.SamAccountName
DisplayName = $User.DisplayName
Description = $User.Description
Enabled = $User.Enabled
ObjectCategory = $User.ObjectCategory
Department = $User.department
Mail = $User.mail
OfficePhone = $User.OfficePhone
PasswordLastSet = $User.PasswordLastSet
LastLogonDate = $User.LastLogonDate
}
$Kayitlar += $Dosya
}

# =======================

#Kullanıcı bilgilerini sabit genişlikli olarak .txt dosyaya aktarıyoruz.
$Kayitlar | Format-Table SamAccountName,DisplayName,Description,Enabled,ObjectCategory,
Department,Mail,OfficePhone,PasswordLastSet,LastLogonDate -AutoSize |
Out-File -Width 1500 -Append -FilePath $ReportFile1 -Encoding Unicode

Notepad.exe $ReportFile1

# =======================

#Kullanıcı bilgilerini Microsoft Excel için .csv dosyaya aktarıyoruz.
Foreach( $Kayit in $Kayitlar )
{
[String]$SatirEkle = "$($Kayit.SamAccountName);$($Kayit.DisplayName); $($Kayit.Description);$($Kayit.Enabled);$($Kayit.ObjectCategory); $($Kayit.Department);$($Kayit.Mail);$($Kayit.OfficePhone); $($Kayit.PasswordLastSet);$($Kayit.LastLogonDate)"
Add-Content -Path $ReportFile2 -Value $SatirEkle
}

# =======================

Download Script Versiyon 1: AllUsersExpExcel_V1.rar

Hiç yorum yok:

Yorum Gönder