Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cls
- $cred = Get-Credential
- $containers = Get-ADComputer -SearchBase "DC=bank,DC=XXbank,DC=ru" -Filter * -Properties IPv4Address, LastLogonDate, Description |
- Sort-Object Name # компьютеры в домене
- #Where-Object Name -eq PC2071 # один компьютер в домене
- Get-ADUser -SearchBase "DC=bank,DC=XXbank,DC=ru" -Filter * |
- Foreach { $h = @{} } { $h.Add($_.SamAccountName, $_.Name) } # берем пользователей, в хэш-таблицу добавляем логины юзеров
- $d = Get-Date -Format "dd.MM"
- $i = 0
- #$test = $true
- $test = $false
- $log = [Environment]::GetFolderPath("MyDocuments") + "\lister$d.txt"
- if (!$test) { Set-Content -Path $log -Value $null }
- foreach ($comp in $containers) {
- $i++
- try {
- $ip = $comp.IPv4Address
- $desc = $comp.Description
- $name = $comp.DNSHostName
- $description = "{0} {1}" -f $d, $ip
- $cname = $comp.Name
- if (Test-Connection -ComputerName $name -quiet) { # проверка хостов на доступность
- $INFO=Get-WmiObject -Class Win32_ComputerSystem -ComputerName $name -Ea Stop
- $login = $INFO.UserName
- if ($login -ne $null) {
- $login = $INFO.UserName.split("\")[-1] # оставляем только логин
- $N = $h[$login] # из хэша делаем выборку по логину, забираем ФИО юзера
- $description += " {0} ({1})" -f $login, $N
- } elseif ($desc -match '( \w+ \([^\)]*\))') { $description += $Matches[1] }
- $INFO2=Get-WmiObject -Class Win32_OperatingSystem -ComputerName $name -Ea Stop
- $inst = $INFO2.InstallDate.Substring(2, 4)
- $boot = $INFO2.LastBootUpTime.Substring(2, 4)
- $ver = $INFO2.Version
- if ($INFO2.Caption -like "*Server*") {
- switch -regex ($ver) {
- "^5.2." { $os = "2003" }
- "^6.0.6002" { $os = "2008" }
- "^6.1.7601" { $os = "2008R2" }
- "^6.3.9200" { $os = "2012" }
- "^6.3.9600" { $os = "2012R2" }
- "^10.0." { $os = "2016" }
- }
- } else {
- switch -regex ($ver) {
- "^5.1.2600" { $os = "XP" }
- "^6.1.7601" { $os = "W7" }
- "^6.2.9200" { $os = "W8" }
- "^6.3.9600" { $os = "W81" }
- "^10.0." { $os = "W10." + $ver.Split(".")[2] }
- }
- }
- $bit = $INFO2.OSArchitecture
- if ($bit -ne $null) { $bit = $bit.Substring(0, 2) } else { $bit = 32 }
- $description += " {0} {1} {2}/{3}" -f $os, $bit, $inst, $boot
- "$i. $cname - $description"
- #Invoke-Command -ComputerName $name -ScriptBlock {$OSWMI=Get-WmiObject -class Win32_OperatingSystem;$OSWMI.Description=$args[0];$OSWMI.put() } -ArgumentList($description)
- $INFO.Description = $description
- if (!$test) {
- "$i. $cname - $description" | Out-File -FilePath $log -Append -Encoding Default
- try { $null = $INFO.put() } catch { }
- Set-ADComputer $comp -Description $description -Cred $cred
- }
- } else {
- #$desc = $comp.Item("Description")
- if ($desc) {
- "$i. $cname - $desc - выключен?"
- if (!$test) { "$i. $cname - $desc - выключен?" | Out-File -FilePath $log -Append -Encoding Default }
- } else {
- "$i. $cname - выключен? пишем"
- if (!$test) { Set-ADComputer $comp -Description $description -Cred $cred }
- }
- }
- }
- catch {
- $cname = $comp.Name
- "$i. $cname - выдает ошибку связи!"
- if (!$test) { "$i. $cname - ?" | Out-File -FilePath $log -Append -Encoding Default }
- }
- }
- if (!$test) {
- $user = "sender@XXbank.ru"
- $pass = "****" | ConvertTo-SecureString -AsPlainText -Force
- $crml = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $pass
- Send-MailMessage -From $user -To admin@XXbank.ru -SmtpServer 192.168.1.25 -Port 25 -Credential $crml -Subject "Check AD $d" -Attachments $log
- }
Add Comment
Please, Sign In to add comment