Advertisement
Guest User

Untitled

a guest
Mar 7th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.32 KB | Source Code | 0 0
  1. $prog   =  'C:\Program Files\Softerra\LDAP Browser 4\laimex.exe'
  2. $cred   =  'C:\Scripts\XXXX.cred'
  3. $key    =  'C:\Scripts\XXXX.key'
  4. $server =  'ldap.yourserver.to:636'
  5. $rootDN =  'DC=XXXX'
  6. $path   =  '\\nas1.mydomain.to\XXXX'
  7. $file   =  'XXXX.csv'
  8. $date   =   Get-Date -f 'yyyy-MM-dd_HH-mm'
  9. $log    =  'XXXX_' + $date + '.log'
  10.  
  11. $exclude = @(
  12.     'OU=YYYY,DC=XXXX',
  13.     'OU=Serviceuser,DC=XXXX'
  14.     )
  15.  
  16.  
  17. # get credentials from file
  18. $credentials = Import-Csv $cred
  19. $user = $credentials.User
  20. $AESKey = Get-Content $key
  21. $secPW = $credentials.secPW | ConvertTo-SecureString -Key $AESKey
  22. $pwd = [System.Net.NetworkCredential]::new("", $secPW).Password
  23.  
  24. # export OUs for loop with laimex
  25. Start-Transcript $path\Log\$log
  26. & $prog /s $server /mech SIMPLE /user $user /pwd $pwd /r $rootDN /p ONE /t '(objectClass=organizationalUnit)' /d CSV /f $path\$file
  27. Stop-Transcript
  28.  
  29. # check log
  30. $logcheck = Get-Content $path\Log\$log
  31. if ($logcheck -contains "Export abgeschlossen.")
  32.     {
  33.         # import OUs (skip first 3 lines)
  34.         $OUs = (Get-Content $path\$file | select -skip 3 | ConvertFrom-Csv).DN
  35.  
  36.         # loop all not excluded
  37.         $OUs | where { $_ -notin $exclude } | foreach {
  38.  
  39.             # get OU name
  40.             $name = ($_ -split ",")[0].Replace("OU=","")
  41.  
  42.             $logsub = ($log).Replace("XXXX_","XXXX_" + $name + "_")
  43.  
  44.             # export user and groups in loop with laimex
  45.             Start-Transcript $path\Log\$logsub
  46.             Write-Host Start
  47.             & $prog /s $server /mech SIMPLE /user $user /pwd $pwd /r $_ /p SUB /page 100 /t '(|(objectClass=user)(objectClass=group))' /d CSV /f ($path + "\" + ($file).Replace(".csv","_" + $name + ".csv"))
  48.             Write-Host End
  49.             Start-Sleep -Seconds 2
  50.             Stop-Transcript
  51.  
  52.             # remove LF if not CRLF
  53.             (Get-Content -Raw ($path + "\" + ($file).Replace(".csv","_" + $name + ".csv")) -Encoding UTF8) -replace "(?<!\r)\n"," " | Set-Content ($path + "\" + ($file).Replace(".csv","_" + $name + "_tmp.csv")) -Encoding UTF8 -Force
  54.            
  55.             # import CSV
  56.             $csvData = Get-Content ($path + "\" + ($file).Replace(".csv","_" + $name + "_tmp.csv")) -Encoding UTF8 | Select-Object -Skip 3 | ConvertFrom-Csv
  57.  
  58.             # delete tmp file
  59.             Remove-Item ($path + "\" + ($file).Replace(".csv","_" + $name + "_tmp.csv")) -Force
  60.  
  61.             # export CSV (for UTF-8-BOM)
  62. if (!(Test-Path ($path + "\UTF-8-BOM\") -PathType Container)) {New-Item -ItemType Directory -Force -Path ($path + "\UTF-8-BOM\")}
  63.             $csvData | Export-Csv ($path + "\UTF-8-BOM\" + ($file).Replace(".csv","_" + $name + "_BOM.csv")) -Encoding UTF8 -NoTypeInformation -Force
  64.  
  65.             # check logsub
  66.             $logsubcheck = Get-Content $path\Log\$logsub
  67.             if ($logsubcheck -notcontains "Export abgeschlossen.")
  68.                 {
  69.                     Send-MailMessage -Encoding UTF8 -To "Postkorb IT Monitoring <monitoring@mydomain.to>" -From "XXXX <server@mydomain.to>" -SmtpServer smtp.mydomain.to  -Priority High `
  70.                     -Subject "Export $name nicht erfolgreich" `
  71.                     -Body "Es wurden vom Skript keine weiteren Maßnahmen getroffen.`nSkript wird ausgeführt auf: $($env:computername)`nPfad: $($PSScriptRoot+"\"+$MyInvocation.MyCommand.Name)"
  72.                 }
  73.         }
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement