Archive for November 2012

I had free time this evening , and I thought of using this time for PowerShell Scripting . So I came up with an IDEA to build a Powershell script which will query the list of servers and get the status of services(windows server) whose StartMode is set as Automatic.

As All of us know (Windows Administrators) , If the startmode of a service is set to automatic , that service has to be in “RUNNING” State ( Even If it screw’s your System 🙂 )

Some times this will not be the case , On Most of the instances if the server is patched and rebooted , some of the Automatic Services will not start.

To determine the servers and list of services , One can use Below PowerShell Code

Get-WmiObject -Class win32_Service -ComputerName (Get-Content C:\Names.txt) | ?{$_.StartMode -match “Auto*”} | Select-Object -Property * | %{ if($_.State -match “stopped”) {‘{0},{1},{2},{3}matic’ -f $_.SystemName,$_.DisplayName,$_.State,$_.StartMode | Out-File C:\AutoMaticServiceStatus.Csv -NoClobber -Encoding ascii -Append }}

Script will produce output C:\AutoMaticServiceStatus.csv in your C:\ Drive

Note – C:\Names.txt contains the Server Names against which we will run this powershell code.

Happy Scripting.




I work in a enviorment , where we patch our servers on Monthly basis . Post patching it is important for us to know about the server status , How many servers got patched , How many servers are still have the downloaded patches , How many serveers have failed to install the patches Etc.

I came up with an IDEA to write a PowerShell Script to get an overall Overview about this.

Below is the script which one can use to Accomplish the above task ,

Note – Before Running the script make sure you have created a C:\Names.txt file (List of servers goes here) against which you will query WSUS server to get the details.

$global:wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer(‘wsus ServerName Goes here’,$False,Port Number goes here of your WSUS)
$computerscope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope
$updatescope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
$s = Get-Content C:\Names.txt
$a = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope
$b = New-Object Microsoft.UpdateServices.Administration.UpdateScope
$c =  $wsus.getcomputertargets($a)
$id = @()
Foreach($Server in $s)
 $id += @($c | ? {$_.FulldomainName -like “$Server*”} | Select-Object -ExpandProperty ID)

$k = $wsus.GetSummariesPerComputerTarget($b,$a)
$k | Where-Object{$id -contains $_.ComputerTargetID} | ForEach {New-Object PSobject -Property @{
ComputerTarget = ($wsus.GetComputerTarget([guid]$_.ComputerTargetId)).FullDomainName
DownloadedCount = $_.DownloadedCount
FaliedCount = $_.FailedCount
}} | Export-Csv C:\PatchResult.csv -NoClobber -Force

Note –

$global:wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer(‘wsus ServerName Goes here’,$False,Port Number goes here of your WSUS)

You need to put WSUSServerName , If you are using SSL then Please put $TRUE , and If you are using any other port otherthan 80 , then put the appropriate port ,. If you are using Default port please let it blank

script will produce C:\PatchResult.csv which contains all the required information.

Hope this will help when it comes to patching 🙂

Happy Scripting.



Reference –

I was participating in Powershell TechNet forum and someone asked question about how to add the ‘A’ Records and ‘PTR’ Records in DNS server . I thought it will be a good excercise for me , ( As I still have sleepless night when I think of Powershell :))

After working for long hours , I have built below script.

Note- One should have C:\DNS.CSV with columns as Hostname and IP address which needs to assinged. 

PowerShell Script

#Script to add DNS ‘A’ Records ‘PTR’ Records to DNS Servers
$dns = “DC” # Your DNS Server Name Goes here
$Zone = “AD.COM” # Your Forward Lookup Zone Name Goes here
$ReverseZone = “” # Your ReverseLookup Zone Name Goes Here
$a = import-csv C:\DNS.csv

#Preparing the C:\Reverse.csv from C:\DNS.CSV for Adding PTR Records
$b = $a | Select-Object -expand IP
$c = $b | %{$_.Split(“.”) | Select-Object -Index 3}
$d = $a | Select-Object -Expand Name
$e = $d | %{$_.Insert($_.length,””)}
for($i=0;$i -le ($e.Length);$i++)
(‘”{0}”,”{1}”‘ -f $c[$i],$e[$i]) | Out-File C:\Reverse.csv -Append -Encoding ascii

$header = “IP”,”Name”
$f = Import-Csv C:\Reverse.csv  -Header $header

#Adding ‘A’ Record to DNS Forward Lookup Zone
$a | %{dnscmd $dns /recordadd $Zone $($_.Name)A $($_.IP)}

#Adding ‘PTR’ Record to DNS Reverse Lookup Zone
$f | %{dnscmd $dns /recordadd $ReverseZone $($_.IP)PTR $($_.Name)}

Script will produce one more CSV File C:\Reverse.CSV which contains IP and FQDN Name Columns for PTR Record creation

Hope this script help AD Administrators 🙂





If you are a windows server administrator then , one or the other day you will be asked to get the UPTime report of list of windows servers (Most of the times your Boss will ask this :))

Below Script will help you to get the UPtime report of listed servers

Before you run the script ,

  • Create a text file servers.txt in C:\ Drive and list the servers against which you need to run this query (The Script will list out the UPtime report of listed server)

Description –

This script will make use of WMI Query (Class – win32_Operatingsystem) and get the lastboottime of listed server. Script will produce 2 TXT Files namely , UPtimeReport.txt and Offline.txt in your C:\ Drive

Note – If all the servers are online then offline.txt will not be created and Vice Versa . i.e if all the servers are offline then UPTimeReport.txt will not be created.

PowerShell Script

#Title – Script for querying windows server UPTime/Lastboot

#Description – This script queries windows servers listed in Servers.txt file (WMI Query) and get their last bootuptime

$servers = Get-Content C:\Servers.txt
foreach ($s in $servers)
$a=Get-WmiObject -ComputerName $s -Class Win32_OperatingSystem -ErrorAction ‘Stop’
$b = $a.convertToDateTime($a.Lastbootuptime)
[TimeSpan]$LastBoot = New-TimeSpan $b $(Get-Date)
(‘{0} {1}Day(s),{2}Hour(s),{3}Min(s),{4}Seconds’ -f $s,$LastBoot.Days,$lastboot.Hours,$LastBoot.Minutes,
$LastBoot.Seconds) | out-file C:\UptimeReport.txt -append -Encoding ascii
(‘{0} Server is notreachable’ -f $s)| Out-File C:\offline.txt -Append -Encoding ascii

I Hope this script will help , and it will make your BOSS Happy as well 🙂



To Much of Garbage , We need to Clean it up . This applies to your Active Directory As well.

This week , I was having some time to play around with PowerShell(Power) .

( I Always call it as power , Because It has a capacity to accomplish each and everything task in Windows :))

Since I have made up my mind to list out the servers which are not used anymore , I thought I will create a script for this.

Below is my script .

$a = Get-ADComputer -filter * -SearchBase “DN of the Domain” -properties` Operatingsystem
$list= @()
Foreach ($os in $a)

 If($os.Operatingsystem -match “windows server *”)
 $list += $os | Select-Object @{ L=’ComputerName’;E={$_.Name} }, @{L=’OperatingSystem’;E={$_.Operatingsystem}}

$list | Export-Csv -Path C:\file.csv -notype

$r = Import-Csv C:\file.csv | Select-Object -ExpandProperty Servername
foreach ($Server in $r)
 if(!(Test-Connection $Server -Quiet))
 Write-Host “$server not reachable”


Note – One need to put DN of the domain Eg – “DC=AD,DC=COM” , In Place of “DN of the Domain”

The script will produce a Csv File C:\File.csv which contains List of windows server in specified domain.


The servers which are not reachable will be listed out in the Powershell Console.

I hope this information helps.

There will be some more Powershell Scripts which I will be building , Looks Like I am in Love with Powershell’s POWER