Delete Old Computer – Active Directory
Active Directory üzerinde kullanılmayan, atıl olarak bekleyen bilgisayar hesaplarını temizlemek için aşağıdaki script’i kullanabilirsiniz. Script, 60 günden eski bilgisayar hesaplarının lastlogondate’ini kontrol ederek siliyor ve bilgilendirme maili gonderiyor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#AD module etkinlestiriliyor... Import-module ActiveDirectory #Istenilen gun sayisi belirleniyor... $MK_olddate = (Get-Date).AddDays(-60) #Belirlenen bilgisayarlarin listesi export ediliyor... Get-ADComputer -SearchBase "OU=Bilgisayarlar,DC=domain,DC=local" -Filter {lastLogonDate -lt $MK_olddate} | FT Name | Out-File 'D:\ADCommands\OldComputer\DeletedOldComputerlist.txt' #Belirlenen bilgisayar hesaplari siliniyor... Get-ADComputer -SearchBase "OU=Bilgisayarlar,DC=domain,DC=local" -Filter {lastLogonDate -lt $MK_olddate} | Remove-ADComputer -confirm:$false ############################################################################### ###########Define Variables######## $fromaddress = "OldComputers@domain.com.tr" $toaddress = "YardimMasasi@domain.com.tr" $Subject = "Eski bilgisayarlar kayitlari silindi!!" $body = Get-Content -Path D:\ADCommands\OldComputer\Content\delete\content.htm | Out-String $attachment = "D:\ADCommands\OldComputer\DeletedOldComputerlist.txt" $smtpserver = "smtpserver" #################################### $message = new-object System.Net.Mail.MailMessage $message.From = $fromaddress $message.To.Add($toaddress) $message.CC.Add($CCaddress) $message.Bcc.Add($bccaddress) $message.IsBodyHtml = $True $message.Subject = $Subject $attach = new-object Net.Mail.Attachment($attachment) $message.Attachments.Add($attach) $message.body = $body $smtp = new-object Net.Mail.SmtpClient($smtpserver) $smtp.Send($message) ################################################################################# |