How To Run Single Script on Multiple Servers
Last weekend my production server rebooted automatically
which cause many processes killed. This cause unnecessary burden on DBAs to re-run all
jobs and monitoring them till they completed successfully.
After investigation we found this is caused due to windows
auto update setting. Here after windows update installation server rebooted
automatically. This was one of unexpected issue on production so to fix this
issue the major task was to check all servers and find those who have auto
status update.
There are couples of ways to do that
- Check server properties one by one on each server
- Check administrative tools one by one on each server
- Use Sql query and run on all server one by one via SSMS from one server
- Use power shell script and get output with one click.
/************ Sql script to read registry of machine ******************/
DECLARE @test int ,
@key varchar(100),@keyupdate varchar (100)
SET @key =
'SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU'
EXEC master..xp_regread
@rootkey='HKEY_LOCAL_MACHINE',@key=@key,@value_name='NoAutoUpdate',@value=@test
OUTPUT
If @test = 0set @keyupdate = 'Automatic Updates is enabled'
Else
set @keyupdate = 'Automatic Updates is disabled'
SELECT @@ServerName,convert(varchar(100),@keyupdate)as [Automatic Updates Status] , @test
/********************************************************************/
Using Power shell
- Create a file name server_list.txt. This file will keep list of all servers on which query need to be executed.
- Create a power shell script to execute desired code of reading registry on all servers
- Create a output file on same location
/**********************Power shell Script: *******************************/
$SearchValue = "0"
$RegPath = "SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
$Serverlist = gc "Drive:\Automation\serverlist.txt"
$output = "Drive:\Automation\Autoupdatestatus_Output.txt"
"Computer,Results" | out-file $output
ForEach ($Computername in $Serverlist)
{
write-host $Computername
$results = Get-WMIObject -query "select StatusCode from Win32_PingStatus where Address = '$Computername'"
if($results.statuscode -ne 1)
{
$BaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LOCALMACHINE", $ComputerName)
$OpenSubKey = $BaseKey.OpenSubKey($RegPath)
$Values = $OpenSubKey.GetValue('NoAutoUpdate')
if ($Values -match $SearchValue)
{
write-host $Computername "- Automatic Updates is enabled"
"$Computername,- Automatic Updates is enabled" | out-file -append $output
}
else
{
write-host $Computername "- Automatic Updates is disabled"
"$Computername,- Automatic Updates is disabled" | out-file -append $output
}
}
Else
{
write-host $Computername "- Server is not pingable"
"$Computername,- Server is not pingable" | out-file -append $output
}
}
/********************************************************************/
We have to run power shell script from a batch file
/*************************Batch files code: ******************************/
Powershell.exe Drive:\Automation\Windows_Auto_update_status.ps1 G:\Automation\Output.txt
/********************************************************************/
We have to just run batch file from command prompt and we
will get result in output file
No comments:
Post a Comment