Guest User

Untitled

a guest
Dec 21st, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. cls
  2. $cred = Get-Credential
  3. $containers = Get-ADComputer -SearchBase "DC=bank,DC=XXbank,DC=ru" -Filter * -Properties IPv4Address, LastLogonDate, Description |
  4. Sort-Object Name # компьютеры в домене
  5. #Where-Object Name -eq PC2071 # один компьютер в домене
  6. Get-ADUser -SearchBase "DC=bank,DC=XXbank,DC=ru" -Filter * |
  7. Foreach { $h = @{} } { $h.Add($_.SamAccountName, $_.Name) } # берем пользователей, в хэш-таблицу добавляем логины юзеров
  8. $d = Get-Date -Format "dd.MM"
  9. $i = 0
  10.  
  11. #$test = $true
  12. $test = $false
  13.  
  14. $log = [Environment]::GetFolderPath("MyDocuments") + "\lister$d.txt"
  15. if (!$test) { Set-Content -Path $log -Value $null }
  16.  
  17. foreach ($comp in $containers) {
  18. $i++
  19.  
  20. try {
  21. $ip = $comp.IPv4Address
  22. $desc = $comp.Description
  23. $name = $comp.DNSHostName
  24.  
  25. $description = "{0} {1}" -f $d, $ip
  26. $cname = $comp.Name
  27.  
  28. if (Test-Connection -ComputerName $name -quiet) { # проверка хостов на доступность
  29. $INFO=Get-WmiObject -Class Win32_ComputerSystem -ComputerName $name -Ea Stop
  30. $login = $INFO.UserName
  31. if ($login -ne $null) {
  32. $login = $INFO.UserName.split("\")[-1] # оставляем только логин
  33. $N = $h[$login] # из хэша делаем выборку по логину, забираем ФИО юзера
  34. $description += " {0} ({1})" -f $login, $N
  35. } elseif ($desc -match '( \w+ \([^\)]*\))') { $description += $Matches[1] }
  36.  
  37. $INFO2=Get-WmiObject -Class Win32_OperatingSystem -ComputerName $name -Ea Stop
  38. $inst = $INFO2.InstallDate.Substring(2, 4)
  39. $boot = $INFO2.LastBootUpTime.Substring(2, 4)
  40. $ver = $INFO2.Version
  41. if ($INFO2.Caption -like "*Server*") {
  42. switch -regex ($ver) {
  43. "^5.2." { $os = "2003" }
  44. "^6.0.6002" { $os = "2008" }
  45. "^6.1.7601" { $os = "2008R2" }
  46. "^6.3.9200" { $os = "2012" }
  47. "^6.3.9600" { $os = "2012R2" }
  48. "^10.0." { $os = "2016" }
  49. }
  50. } else {
  51. switch -regex ($ver) {
  52. "^5.1.2600" { $os = "XP" }
  53. "^6.1.7601" { $os = "W7" }
  54. "^6.2.9200" { $os = "W8" }
  55. "^6.3.9600" { $os = "W81" }
  56. "^10.0." { $os = "W10." + $ver.Split(".")[2] }
  57. }
  58. }
  59. $bit = $INFO2.OSArchitecture
  60. if ($bit -ne $null) { $bit = $bit.Substring(0, 2) } else { $bit = 32 }
  61. $description += " {0} {1} {2}/{3}" -f $os, $bit, $inst, $boot
  62.  
  63. "$i. $cname - $description"
  64.  
  65. #Invoke-Command -ComputerName $name -ScriptBlock {$OSWMI=Get-WmiObject -class Win32_OperatingSystem;$OSWMI.Description=$args[0];$OSWMI.put() } -ArgumentList($description)
  66. $INFO.Description = $description
  67. if (!$test) {
  68. "$i. $cname - $description" | Out-File -FilePath $log -Append -Encoding Default
  69.  
  70. try { $null = $INFO.put() } catch { }
  71.  
  72. Set-ADComputer $comp -Description $description -Cred $cred
  73. }
  74. } else {
  75. #$desc = $comp.Item("Description")
  76. if ($desc) {
  77. "$i. $cname - $desc - выключен?"
  78. if (!$test) { "$i. $cname - $desc - выключен?" | Out-File -FilePath $log -Append -Encoding Default }
  79. } else {
  80. "$i. $cname - выключен? пишем"
  81. if (!$test) { Set-ADComputer $comp -Description $description -Cred $cred }
  82. }
  83. }
  84. }
  85.  
  86. catch {
  87. $cname = $comp.Name
  88. "$i. $cname - выдает ошибку связи!"
  89. if (!$test) { "$i. $cname - ?" | Out-File -FilePath $log -Append -Encoding Default }
  90. }
  91. }
  92.  
  93. if (!$test) {
  94. $user = "sender@XXbank.ru"
  95. $pass = "****" | ConvertTo-SecureString -AsPlainText -Force
  96.  
  97. $crml = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $pass
  98.  
  99. Send-MailMessage -From $user -To admin@XXbank.ru -SmtpServer 192.168.1.25 -Port 25 -Credential $crml -Subject "Check AD $d" -Attachments $log
  100. }
Add Comment
Please, Sign In to add comment