Thursday, January 3, 2013

Powershell Script to check if any server is pending for reboot


After the security patch, we need to validate if any of our critical server is pending for reboot and take necessary actions.PowerShell script and use it to identify servers that are pending for reboot.This will avoid the scenarios of unexpected server reboot.
 
# Filename:  CheckForPendingReboot.ps1
# Description: Imports a list of computers from a text file and then checks each of the
#    computers for the RebootPending registry key. If the key is present,
#    the script restarts the computer upon confirmation from the user.
# Assumes the existence of a file called 'Servers.txt' in the same directory, populated with
# the NETBIOS names of a list of computers, each on a new line.
 
 $computernames = gc Servers.txt

 foreach ($computername in $computernames)
 {
 $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$computername)
 $key = $baseKey.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\")
 $subkeys = $key.GetSubKeyNames()
 $key.Close()
 $baseKey.Close()
 If ($subkeys | Where {$_ -eq "RebootPending"})
 {
  Write-Host "There is a pending reboot for" $computername
  Restart-Computer -ComputerName $computername -confirm
 }
 Else
 {
  Write-Host "No reboot is pending for" $computername
 }
}

 

Wednesday, January 2, 2013

Steps to open powershell Ise

Following are the ways to open powershell ISE:

Method 1 : If you see powershell icon on your task bar. you can right click and choose 'windows powershell ISE'.







 

Method 2 : In the Run box, type, powershell_ise.exe.

Method 3 : In the Windows PowerShell console type powershell_ise.exe.

Powershell script to filter error from event viewer system log

This is first post of 2013 :)  so wanted to post something which I learnt recently.

A powershell script for filtering errors in the event viewer log. This script you need to execute in powershell ISE.

$startTime=(Get-Date).AddDays(-1)
$endtime=Get-Date
$b = get-eventlog -log system -after $starttime -before $endtime -EntryType Error
$b = $b | sort eventid
$b | Export-CSV -delimiter " "  e:\outtab.txt
This script will filter out last 24hours system log and pull all the errors from the log and dump it to txt file. If you want to tweek this script and if you like to export the data in csv file you can use

$b | Export-Csv e:\out1.csv

Output would look like :

 
 
 
if