|
IT是个团队,有时候难免会出现纰漏,比如说AD账号,有人就有可能未按标准来添加账号,比如规定必须把账号的office地址栏填上,但有时候某些人就忘了。为了解决这个问题,用下面脚本:
import-module activedirectory
$user_list = New-object System.Collections.ArrayList
#$telephone_a = New-object System.Collections.ArrayList
#$title_a = New-object System.Collections.ArrayList
#$department_a = New-object System.Collections.ArrayList
#$manager_a = New-object System.collections.ArrayList
$user_all = get-aduser -searchbase "ou=Users,ou=China,dc=script,dc=com" -filter * -Properties *
$offical_office = "Shanghai"
$offical_city = "Shanghai"
$offical_postalcode = "200233"
$offical_country = "CN"
$offical_company = "Script China"
set-content d:\ps-text\telephonenumber.txt ""
set-content d:\ps-text\jobtitle.txt ""
set-content d:\ps-text\department.txt ""
set-content d:\ps-text\manager.txt ""
set-content d:\ps-text\account_changed.txt ""
add-content d:\ps-text\telephonenumber.txt "Account | Description"
add-content d:\ps-text\telephonenumber.txt "--------------------------------------------"
add-content d:\ps-text\jobtitle.txt "Account | Description"
add-content d:\ps-text\jobtitle.txt "--------------------------------------------"
add-content d:\ps-text\department.txt "Account | Description"
add-content d:\ps-text\department.txt "--------------------------------------------"
add-content d:\ps-text\manager.txt "Account | Description"
add-content d:\ps-text\manager.txt "--------------------------------------------"
add-content d:\ps-text\account_changed.txt "Account | Description"
Function Direct_change($u)
{
$samaccountname = $u.SamAccountName
$office = $u.PhysicalDeliveryOfficeName
$street = $u.StreetAddress
$city = $u.City
$zipcode = $u.PostalCode
$country = $u.Country
$displayname = $u.DisplayName
if ([string]::isNullorEmpty($office))
{
set-aduser -Identity $samaccountname -PhysicalDeliveryOfficeName $offical_office
add-content d:\ps-text\account_changed.txt "The office name changed , account : $displayname"
}
if ([string]::isNullorEmpty($city))
{
set-aduser -Identity $samaccountname -City $offical_city
add-content d:\ps-text\account_changed.txt "The city name changed , account : $displayname"
}
if ([string]::isNullorEmpty($zipcode))
{
set-aduser -Identity $samaccountname -PostalCode $offical_postalcode
add-content d:\ps-text\account_changed.txt "The zipcode name changed, account : $displayname"
}
if ([string]::isNullorEmpty($country))
{
set-aduser -Identity $samaccountname -Country $offical_country
add-content d:\ps-text\account_changed.txt "The country name changed ,account : $displayname"
}
if ([string]::isNullorEmpty($company))
{
set-aduser -Identity $samaccountname -Company $offical_company
add-content d:\ps-text\account_changed.txt "The company name changed ,account : $displayname"
}
}
Function Record_need_change($u_r)
{
$telephonenumber = $u_r.TelephoneNumber
$accountexpire = $u_r.AccountExpirationDate
$jobtitle = $u_r.Title
$department = $u_r.Department
$manager = $u_r.Manager
$description = $u_r.Description
$displayname = $u_r.DisplayName
$sn = $u_r.SamAccountName
if ([string]::isNullorEmpty($telephonenumber))
{
#$telephone_a += @($sn)
add-content d:\ps-text\telephonenumber.txt "$displayname $description"
}
if ([string]::isNullorEmpty($jobtitle))
{
#$title_a += @($sn)
add-content d:\ps-text\jobtitle.txt "$displayname $description"
}
if ([string]::isNullorEmpty($department))
{
#$department_a += @($sn)
add-content d:\ps-text\department.txt "$displayname $description"
}
if ([string]::isNullorEmpty($manager))
{
#$manager_a += @($sn)
add-content d:\ps-text\manager.txt "$displayname $description"
}
}
foreach ($user in $user_all)
{
$GivenName = $user.GivenName
$sn = $user.sn
$Displayname = $user.DisplayName
Direct_change $user
Record_need_change $user
}
add-content d:\ps-text\telephonenumber.txt "--------------------------------------------"
add-content d:\ps-text\jobtitle.txt "--------------------------------------------"
add-content d:\ps-text\department.txt "--------------------------------------------"
add-content d:\ps-text\manager.txt "--------------------------------------------"
$EmailFrom = "j007@script.com"
[string]$EmailTo="j007@script.com,vg@script.com"
$Subject = "AD account information list, need complete!"
[string]$messagebody = ""
$telephone_s = get-content d:\ps-text\telephonenumber.txt
$telephone_0 = "1. Below is the account list of no telephonenumber, Please add it for them ASAP: "
$messagebody = $messagebody + $telephone_0 + "`r`n"
foreach ($line in $telephone_s)
{
$messagebody = $messagebody + $line + "`r`n"
}
$title_s = get-content d:\ps-text\jobtitle.txt
$title_0 = "2. Below account doesn't have job tilte,Please add it ASAP:"
$messagebody = $messagebody + "`r`n" + $title_0 + "`r`n"
foreach ($title_2 in $title_s)
{
$messagebody = $messagebody + $title_2 + "`r`n"
}
$dept_s = get-content d:\ps-text\department.txt
$dept_0 = "3. Please add Department value for below account in AD :"
$messagebody = $messagebody + "`r`n" + $dept_0 + "`r`n"
foreach ($dept in $dept_s)
{
$messagebody = $messagebody + $dept + "`r`n"
}
$menager_s = get-content d:\ps-text\manager.txt
$menager_0 = "4. Please add Manager name for below account:"
$messagebody = $messagebody + "`r`n" + $menager_0 + "`r`n"
foreach ($menager_1 in $menager_s)
{
$messagebody = $messagebody + $menager_1 + "`r`n"
}
$SMTPServer = "10.0.0.1"
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer)
#$SMTPClient.EnableSsl = $true
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("script\jc", "007");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $messagebody)
为什么会这么长? 主要是为了收到邮件的内容看起来顺眼一点。
先写这么多,后续再写...
|
|
|