Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # - config ---------------------------------------------------------------------
  2. $filepath = 'C:\Users\tolstoy_d\Documents\Tesli Projects\ad-ps-mobile\formulas4.csv'
  3. $version = "2015.01.23"
  4.  
  5. # - exclude list ---------------------------------------------------------------
  6. # not in list, but must be kept
  7. $exclude = (
  8. ('User name',   '+73242342354'),
  9. ('User name2',      '+7(234214234'),
  10. ('User name 3',     '+7(123123123')
  11. )
  12. # - consts ---------------------------------------------------------------------
  13. clear
  14. $err  = @()
  15. $start = Get-Date
  16. # ------------------------------------------------------------------------------
  17. function __de-yo {
  18.     # replace all 'yo' to 'ye'
  19.     return $args.Replace('ั‘','ะต').Replace('ะ','ะ•')
  20. }
  21.  
  22. function _parse_csv {
  23.     # read csv and clear from 'yo'
  24.     [array]$csv = @()
  25.     # Note:
  26.     #   csv-file format:
  27.     #   Second-name First-name,Mobile-number
  28.     $csv_raw = Import-Csv -Path $filepath -Header cname,cmobile
  29.     # clear from empty values in csv file
  30.     foreach ( $i in $csv_raw ) {
  31.         # if string not empty (possible import bug)
  32.         if ( $i.cname.Length ) {
  33.             $i.cname = __de-yo $i.cname
  34.             $csv += $i
  35.         }
  36.     }
  37.     return $csv, $csv.Count
  38. }
  39.  
  40. function _ad_parse {
  41.     # parse AD to find users with mobile
  42.     [array]$list_to_remove = @()
  43.     [array]$list_to_update = @()
  44.  
  45.     $adlist = Get-ADUser -Filter {Enabled -eq $true} -Properties name,mobile,ObjectGUID | Select-Object -Property name,mobile,ObjectGUID
  46.  
  47.     foreach ($i in $adlist) {
  48.         $i.name = __de-yo $i.name
  49.         # check name in csv list
  50.         if ( $csv.cname -contains $i.name ) {
  51.             $list_to_update += $i
  52.         }
  53.         elseif ($exclude | Where-Object {$_ -contains $i.name}) {
  54.             $list_to_update += $i
  55.         }
  56.         elseif ($i.mobile) {
  57.             $list_to_remove += $i
  58.         }
  59.     }
  60.     return $list_to_remove, $list_to_update
  61. }
  62.  
  63. function _ad_remove_mobile {
  64.     # remove mobiles not in list
  65.     [int]$num_removed = 0
  66.     Write-Host "Users not in list. Mobile numbers will be removed:"
  67.     Write-Host ($list_to_remove | FT name,mobile | Out-String)
  68.    
  69.     $list_to_remove | ForEach-Object {
  70.         try {
  71.             Set-ADUser $_.ObjectGUID -Clear mobile
  72.             $num_removed ++
  73.         }
  74.         catch {
  75.             $script:err += , $_
  76.         }
  77.     }
  78.     return $num_removed
  79. }
  80.  
  81. function _ad_update_mobile {
  82.     # update mobiles in list for AD users
  83.     [int]$num_updated = 0
  84.    
  85.     function __ad_replace {
  86.         Param (
  87.         [Object]$update_item,
  88.         [String]$my_name,
  89.         [String]$my_mobile
  90.         )
  91.         if ( $my_name -like $update_item.name ) {
  92.             if ( $my_mobile -notlike $update_item.mobile ) {
  93.                 try {
  94.                     Set-ADUser  $update_item.ObjectGUID -Replace @{mobile=$my_mobile}
  95.                     # return update success
  96.                     return 1
  97.                 }
  98.                 catch {
  99.                     $script:err += , $_
  100.                     return 0
  101.                 }
  102.             }
  103.         }
  104.     }
  105.    
  106.     foreach ($list_to_update_item in $list_to_update) {
  107.    
  108.         foreach ( $csv_item in $csv ) {
  109.             $num_updated += __ad_replace -update_item $list_to_update_item -my_name $csv_item.cname -my_mobile $csv_item.cmobile
  110.         }
  111.         foreach ( $exclude_item in $exclude ) {
  112.             $num_updated += __ad_replace -update_item $list_to_update_item -my_name $exclude_item[0] -my_mobile $exclude_item[1]
  113.         }
  114.     }
  115.     return $num_updated
  116. }
  117.  
  118. # - main -----------------------------------------------------------------------
  119. try {
  120.     $csv, $num_imported = _parse_csv
  121.     $list_to_remove, $list_to_update = _ad_parse
  122.     $num_removed = _ad_remove_mobile
  123.     $num_updated = _ad_update_mobile
  124. }
  125. catch {
  126.     # if unexpected error: write event to console and windows event log, and Exit
  127.     Write-Host "Critical Error:" -BackgroundColor Red
  128.     Write-Host $error[0]
  129. #   Write-EventLog -LogName "Windows Powershell" -Source "Powershell" -EventID 3001 -EntryType Warning -Message $error[0]
  130.     Exit
  131. }
  132. finally {
  133.     # print results
  134.     if ($err) {
  135.         Write-Host "================================================================================"
  136.         Write-Host "Errors detected:" -BackgroundColor Red
  137.         $err | FT
  138.     # TODO: Event log notification, to detect not critical errors
  139.     }
  140.     Write-Host "================================================================================"
  141.     Write-Host "Mobile numbers imported: " $num_imported
  142.     Write-Host "Mobile updated: " $num_updated
  143.     Write-Host "Mobile removed: " $num_removed
  144.     Write-Host "time:" ((Get-Date) - $start)
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement