powershell分享5--AD中常用的小語句

一、通過powershell批量創建郵箱

Set-Location C:\Users\fanyx_v\Desktop\

Import-Module ActiveDirectory
Add-PSSnapin Microsoft.Exchange*
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
$ADUser=Import-Csv -Path .\NewUser.csv -UseCulture -Encoding Default
foreach ($Users in $ADUser)
{
$UserPrincipalName=$Users.UserPrincipalName #每個用戶帳戶都有用戶主體名稱 (UPN),格式爲:<user>@<DNS-domain-name>。#
#$Company=$Users.Company #指定用戶所在的公司。#
$Department=$Users.Department #指定用戶所在的部門。#
$DisplayName=$Users.DisplayName #指定對象的顯示名稱。#
$Name=$Users.Name #指定對象的名稱。#
$SamAccountName=$Users.SamAccountName
$Description=$Users.Description #賬戶的描述信息#
$Manager=$Users.Manager #指定用戶的經理。#
#$AccountPassword=$Users.AccountPassword # 爲帳戶指定新密碼值。#
$MobilePhone=$Users.MobilePhone #指定用戶的移動電話號碼。
$AccountExpirationDate=$Users.AccountExpirationDate #指定用戶的過期時間

    #根據需要添加
New-ADUser -Name $Name -Manager  $Manager  -AccountExpirationDate $AccountExpirationDate  -UserPrincipalName  $UserPrincipalName -Department  $Department   -SamAccountName $SamAccountName   -DisplayName  $DisplayName  -Description $Description   -ChangePasswordAtLogon 0 -AccountPassword (ConvertTo-SecureString "2q3eNChZ" -AsPlainText -Force) -Enabled 1 -Path "OU=測試公司,DC=demo,DC=com"
sleep 20 #延遲數
Enable-Mailbox  $users.SamAccountName -Database "database"    

}

二、導出指定OU下面所有人的一些信息

Get-ADUser -Filter -SearchBase "OU=測試公司,DC=demo,DC=com" -Properties | select Name,whenCreated,PasswordLastSet,SamAccountName | Export-CSV creat_time.csv -NoTypeInformation -Encoding UTF8

三、批量創建聯繫人

import-csv C:\Users\fanyxv\Desktop\contact.csv | ForEach {new-mailcontact -Alias $.Alias -name $.displayname -ExternalEmailAddress $.targetaddress}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章