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.

http://gallery.technet.microsoft.com/Read-Registry-from-9d1a7ec4

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 “<html>”
Add-Content $report “<head>”
Add-Content $report “<meta http-equiv=’Content-Type’ content=’text/html; charset=iso-8859-1′>”
Add-Content $report ‘<title>Registry Value $Key Status</title>’
add-content $report ‘<STYLE TYPE=”text/css”>’
add-content $report “<!–”
add-content $report “td {”
add-content $report “font-family: Tahoma;”
add-content $report “font-size: 11px;”
add-content $report “border-top: 1px solid #999999;”
add-content $report “border-right: 1px solid #999999;”
add-content $report “border-bottom: 1px solid #999999;”
add-content $report “border-left: 1px solid #999999;”
add-content $report “padding-top: 0px;”
add-content $report “padding-right: 0px;”
add-content $report “padding-bottom: 0px;”
add-content $report “padding-left: 0px;”
add-content $report “}”
add-content $report “body {”
add-content $report “margin-left: 5px;”
add-content $report “margin-top: 5px;”
add-content $report “margin-right: 0px;”
add-content $report “margin-bottom: 10px;”
add-content $report “”
add-content $report “table {”
add-content $report “border: thin solid #000000;”
add-content $report “}”
add-content $report “–>”
add-content $report “</style>”
Add-Content $report “</head>”
Add-Content $report “<body>”
add-content $report “<table width=’50%’>”
add-content $report “<tr bgcolor=’Lavender’>”
add-content $report “<td colspan=’7′ height=’25’ align=’center’>”

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

add-content $report “<font face=’tahoma’ color=’#003399′ size=’4′><strong>Registry Value $Key Status</strong></font>”
add-content $report “</td>”
add-content $report “</tr>”
add-content $report “</table>”

######################Definae Columns###################################################
add-content $report “<table width=’50%’>”
Add-Content $report “<tr bgcolor=’Lavender’>”
Add-Content $report “<td width=’10%’ align=’center’><B>Server Name</B></td>”
Add-Content $report “<td width=’10%’ align=’center’><B>Value</B></td>”
Add-Content $report “</tr>”

#####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 “<tr>”
Add-Content $report “<td bgcolor= ‘GainsBoro’ align=center> <B> $Server </B></td>”
Add-Content $report “<td bgcolor= ‘Aquamarine’ align=center><B>$Value</B></td>”
Add-Content $report “</tr>”

}

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

Add-content $report “</table>”
Add-Content $report “</body>”
Add-Content $report “</html>”

#####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)

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

Regards

Sukhija Vikas

Advertisements

9 thoughts on “Read/Write Registry for multiple servers via Powershell – Part1

  1. I tested it with the servers that are offline, its not picking any value & that is the correct response.
    Registry Value TimeOutValue Status
    Server Name Value
    Server1
    Server2
    Server3
    Server4

  2. Thank you so much for your prompt reply. In my case, if an offline server is between two online servers, then the offline server is picking the values on the online servers.
    Server1 – Online
    Server2 – Offline
    Server3 – Online

    >>Value server2 = Value Server1,3

    If the Offline server is the last server, then it does not pick any value, just as expected.

  3. Replace the condition under for loop as(added if else)–> now servers that have error will not be printed in the report.

    foreach ($Server in $servers)
    {

    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($main, $Server)
    $regKey= $reg.OpenSubKey($path)
    $Value = $regkey.GetValue($key)

    if($error -ne $null)

    {

    Write-host “error found for server $Server” -foregroundcolor blue
    $error.clear()

    }

    else

    {

    ######################Add values inside Columns########################################
    Add-Content $report “


    Add-Content $report “

    $Server


    Add-Content $report “

    $Value


    Add-Content $report “

    }

    }

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