Server Decommission Audit

Recently one of the IT team approached us to create a script where they want to audit if the decommissioned servers in the environment have entries in Active directory, DNS, WINS & also to check the ping status. Their approach was to find any discrepancies.

So this script has taken birth 🙂 , I am sharing it as it would be useful in other environments as well.

For checking  entry in AD , I have used Quest AD management Shell. Get-Qadcomputer

For checking in DNS , I have used Nslookup. nslookup -querytype=A $_ 2>&1

For checking in WINS , I have used Nblookup. .\nblookup $_

Nblookup can be downloaded from below link, although I have included it inside the script folder. (as it would be used from same path)

https://support.microsoft.com/en-us/kb/830578

Lastly I have used Test-Connection for checking Ping status.

Extract the zip file from below link, update the srv.txt file & variables smtp inside .ps1

https://gallery.technet.microsoft.com/scriptcenter/Server-Decommission-Audit-ad671c31

$smtpserver = “smtpserver”

$from = “DecomSrvAudit@labtest.com”

$to = “vsukhija@labtest.com

Run the batch file & report will be sent to email id defined inside the script.

 

####################################################################### 
#            Author: Vikas Sukhija (http://msexchange.me) 
#            Date: 04/17/2016 
#            Reviewer: 
#            Desc: Check server status in AD & Name lookup 
####################################################################### 
 
###############Add required modules ############ 
 
If ((Get-PSSnapin | where {$_.Name -match "Quest.ActiveRoles.ADManagement"}) -eq $null) 
{ 
    Add-PSSnapin Quest.ActiveRoles.ADManagement 
} 
 
########define variables######## 
$date1 = get-date -format d 
$date1 = $date1.ToString().Replace("/","-") 
 
$report = ".\" + "Decom_" + $date1 + "_.csv" 
 
$smtpserver = "smtpserver" 
 
$from = "DecomSrvAudit@labtest.com" 
 
$to = "vsukhija@labtest.com 
 
$coll = @() 
 
$srvs = gc .\srv.txt 
 
################################## 
 
 
$srvs | foreach-object{ 
write-host "processing server $_ ....." -foregroundcolor green 
$srv = get-qadcomputer $_$  -ea silentlycontinue 
$ping = Test-Connection $_ -count 2 -ea silentlycontinue 
 
 
if($ping) { 
$Pstatus = "Yes"Else { 
$Pstatus = "No" } 
 
if($srv) { 
$Adstatus = "Exists"Else { 
$Adstatus = "NotFound" } 
 
$nsl = nslookup -querytype=A $_ 2>&$find1 = "*can't find*" 
$find2 = "*timed-out*" 
 
if(($nsl -like $find1-or ($nsl -like $find2)){ 
$lookup = "False"Else$lookup = "True"} 
 
$wsl = .\nblookup $_ 
$find = "*does not exist*" 
 
if($Wsl -like $find){ 
$wlookup = "False"Else$wlookup = "True"} 
 
 
 
$dcomrep = "" | Select Name,Ping,AdStatus,Nslookup,Winslookup 
 
$dcomrep.Name = $_ 
$dcomrep.Ping = $Pstatus 
$dcomrep.AdStatus = $Adstatus 
$dcomrep.Nslookup = $lookup 
$dcomrep.Winslookup = $wlookup 
$coll +$dcomrep 
 
} 
 
#################################################### 
 
$coll | export-csv $report -notypeinfo 
 
$getpath = get-childitem $report 
$fullpath = $getpath.Fullname 
 
$message = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpserver$message.From = $from 
$message.To.Add($to$message.IsBodyHtml = $False 
$message.Subject = "Decom Audit Report" 
$attach = new-object Net.Mail.Attachment($fullpath$message.Attachments.Add($attach)  
$smtp.Send($message) 
 
#######################################################

 

Regards

Sukhija Vikas

http://msexchange.me

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s