Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # - config ---------------------------------------------------------------------
- $filepath = 'C:\Users\tolstoy_d\Documents\Tesli Projects\ad-ps-mobile\formulas4.csv'
- $version = "2015.01.23"
- # - exclude list ---------------------------------------------------------------
- # not in list, but must be kept
- $exclude = (
- ('User name', '+73242342354'),
- ('User name2', '+7(234214234'),
- ('User name 3', '+7(123123123')
- )
- # - consts ---------------------------------------------------------------------
- clear
- $err = @()
- $start = Get-Date
- # ------------------------------------------------------------------------------
- function __de-yo {
- # replace all 'yo' to 'ye'
- return $args.Replace('ั','ะต').Replace('ะ','ะ')
- }
- function _parse_csv {
- # read csv and clear from 'yo'
- [array]$csv = @()
- # Note:
- # csv-file format:
- # Second-name First-name,Mobile-number
- $csv_raw = Import-Csv -Path $filepath -Header cname,cmobile
- # clear from empty values in csv file
- foreach ( $i in $csv_raw ) {
- # if string not empty (possible import bug)
- if ( $i.cname.Length ) {
- $i.cname = __de-yo $i.cname
- $csv += $i
- }
- }
- return $csv, $csv.Count
- }
- function _ad_parse {
- # parse AD to find users with mobile
- [array]$list_to_remove = @()
- [array]$list_to_update = @()
- $adlist = Get-ADUser -Filter {Enabled -eq $true} -Properties name,mobile,ObjectGUID | Select-Object -Property name,mobile,ObjectGUID
- foreach ($i in $adlist) {
- $i.name = __de-yo $i.name
- # check name in csv list
- if ( $csv.cname -contains $i.name ) {
- $list_to_update += $i
- }
- elseif ($exclude | Where-Object {$_ -contains $i.name}) {
- $list_to_update += $i
- }
- elseif ($i.mobile) {
- $list_to_remove += $i
- }
- }
- return $list_to_remove, $list_to_update
- }
- function _ad_remove_mobile {
- # remove mobiles not in list
- [int]$num_removed = 0
- Write-Host "Users not in list. Mobile numbers will be removed:"
- Write-Host ($list_to_remove | FT name,mobile | Out-String)
- $list_to_remove | ForEach-Object {
- try {
- Set-ADUser $_.ObjectGUID -Clear mobile
- $num_removed ++
- }
- catch {
- $script:err += , $_
- }
- }
- return $num_removed
- }
- function _ad_update_mobile {
- # update mobiles in list for AD users
- [int]$num_updated = 0
- function __ad_replace {
- Param (
- [Object]$update_item,
- [String]$my_name,
- [String]$my_mobile
- )
- if ( $my_name -like $update_item.name ) {
- if ( $my_mobile -notlike $update_item.mobile ) {
- try {
- Set-ADUser $update_item.ObjectGUID -Replace @{mobile=$my_mobile}
- # return update success
- return 1
- }
- catch {
- $script:err += , $_
- return 0
- }
- }
- }
- }
- foreach ($list_to_update_item in $list_to_update) {
- foreach ( $csv_item in $csv ) {
- $num_updated += __ad_replace -update_item $list_to_update_item -my_name $csv_item.cname -my_mobile $csv_item.cmobile
- }
- foreach ( $exclude_item in $exclude ) {
- $num_updated += __ad_replace -update_item $list_to_update_item -my_name $exclude_item[0] -my_mobile $exclude_item[1]
- }
- }
- return $num_updated
- }
- # - main -----------------------------------------------------------------------
- try {
- $csv, $num_imported = _parse_csv
- $list_to_remove, $list_to_update = _ad_parse
- $num_removed = _ad_remove_mobile
- $num_updated = _ad_update_mobile
- }
- catch {
- # if unexpected error: write event to console and windows event log, and Exit
- Write-Host "Critical Error:" -BackgroundColor Red
- Write-Host $error[0]
- # Write-EventLog -LogName "Windows Powershell" -Source "Powershell" -EventID 3001 -EntryType Warning -Message $error[0]
- Exit
- }
- finally {
- # print results
- if ($err) {
- Write-Host "================================================================================"
- Write-Host "Errors detected:" -BackgroundColor Red
- $err | FT
- # TODO: Event log notification, to detect not critical errors
- }
- Write-Host "================================================================================"
- Write-Host "Mobile numbers imported: " $num_imported
- Write-Host "Mobile updated: " $num_updated
- Write-Host "Mobile removed: " $num_removed
- Write-Host "time:" ((Get-Date) - $start)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement