Thursday, January 2, 2014

Read/Write Registry for multiple servers via Powershell – Part1

Hi Readers,
I am sharing a script that we have written to read the registry from multiple servers & provide back results in html table
via email.
example:-

extract the attached zip file from below link & then add the server names in servers.txt file.
edit the value that you want to read (top of the script)
$main = “Localmachine”
$Path = “System\CurrentControlSet\Services\Disk”
$key = “TimeOutValue”
Define smtp settings
$smtphost = “smtp.lab.com” 
$from = “Registry_value@lab.com” 
$to = “Vikas.sukhija@lab.com” 

Now run the batch file  & enjoy the output.
In the next part I will share writing to registry that I have already shared on MS site.
Script Code:-
##########################################################################
#       Author:Aishwarya Rawat
#       Reviewer: Vikas Sukhija
#       Date: 12/26/2013
#       Description: Check for Registry Value.
##########################################################################

##################Define variables########################################

$main = "Localmachine"
$Path = "System\CurrentControlSet\Services\Disk"
$key = "TimeOutValue"

####################Define email Variables#################################

$smtphost = "smtp.lab.com" 
$from = "Registry_value@lab.com" 
$to = "Vikas.sukhija@lab.com" 

############################################################################

$report = ".\report.htm" 
$servers = Get-content .\servers.txt

$checkrep = Test-Path $report 

If ($checkrep -like "True")

{

Remove-Item ".\report.htm"


}

New-Item ".\report.htm" -type file

################################ADD HTML Content#############################


Add-Content $report "" 
Add-Content $report "" 
Add-Content $report "" 
Add-Content $report 'Registry Value $Key Status
add-content $report '
Add-Content $report "
Add-Content $report "" 
add-content $report  "" 

add-content $report  " " 
add-content $report  "" 

#######################Title of table####################################################

add-content $report  "Registry Value $Key Status
add-content $report  "
add-content $report  "
add-content $report  "

######################Definae Columns###################################################
add-content $report  "" 

Add-Content $report " " 
Add-Content $report  "Server Name
Add-Content $report  "Value
Add-Content $report "


#####Get Registry Value ####

foreach ($Server in $servers) 
{

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($main, $Server)
$regKey= $reg.OpenSubKey($path)
$Value = $regkey.GetValue($key)
  
######################Add values inside Columns########################################  
         Add-Content $report " " 
Add-Content $report "   $Server
         Add-Content $report "$Value
         Add-Content $report "
             
                                                
   
}


#####################Close HTMl Tables###############################################


Add-content $report  "
Add-Content $report "
Add-Content $report "

#####Send Email#####


$subject = "Registry Value $Key Status" 
$body = Get-Content ".\report.htm" 
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost 
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body 
$msg.isBodyhtml = $true 
$smtp.send($msg) 

#############################################################################################


Reach me:- http://msexchange.me
Regards
Sukhija Vikas

No comments:

Post a Comment