Read/Write Registry for multiple servers via Powershell – Part2

Hi Readers,

I am sharing a script that we have written to create/modify the registry for multiple servers & provide back results in html table

via email.

example:-

 

extract the attached zip file below & then add the server names in servers.txt file.

http://gallery.technet.microsoft.com/Write-Create-Modify-2952426d

edit the value that you want to write (top of the script)

$main = “Localmachine”

$Path = “System\CurrentControlSet\Services\Disk”

$key = “TimeOutValue”

$tout = “60”  #####modified Value of dword ############

$nexist = “key doesn’t exist”

Define smtp settings

$smtphost = “smtp.lab.com”

$from = “Registry_value@lab.com”

$to = “Vikas.sukhija@lab.com”

Now run the batch file

Script Code:-

##########################################################################
# Author:Aishwarya Rawat (Read Registry)
# Modified: Vikas Sukhija (for writing & creating values)
# Date: 12/30/2013
# Description: Check for Registry Value & Modify a required value
##########################################################################

<#[Microsoft.Win32.RegistryValueKind]::Binary –> REG_BINARY
[Microsoft.Win32.RegistryValueKind]::Qword –> REG_QWORD
[Microsoft.Win32.RegistryValueKind]::MultiString –> REG_MULTI_SZ
[Microsoft.Win32.RegistryValueKind]::ExpandString –> REG_EXPAND_SZ
[Microsoft.Win32.RegistryValueKind]::String –> REG_SZ
#>
##################Define variables########################################

$main = “Localmachine”
$Path = “System\CurrentControlSet\Services\Disk”
$key = “TimeOutValue”
$tout = “60” #####modified Value of dword ############
$nexist = “key doesn’t exist”

####################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>ExistingValue</B></td>”
Add-Content $report “<td width=’10%’ align=’center’><B>ModifiedValue</B></td>”
Add-Content $report “</tr>”
#####Get Registry Value ####

foreach ($Server in $servers)
{

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

Add-Content $report “<tr>”
if ($value -eq $null)

{
Add-Content $report “<td bgcolor= ‘GainsBoro’ align=center> <B> $Server </B></td>”
Add-Content $report “<td bgcolor= ‘Aquamarine’ align=center><B>$nexist</B></td>”
$regkey.SetValue($key,$tout,[Microsoft.Win32.RegistryValueKind]::DWORD)
}
elseif ($value -ne $null)
{

Add-Content $report “<td bgcolor= ‘GainsBoro’ align=center> <B> $Server </B></td>”
Add-Content $report “<td bgcolor= ‘Aquamarine’ align=center><B>$value</B></td>”

if ($value -ne $tout)
{

$regkey.SetValue($key,$tout,[Microsoft.Win32.RegistryValueKind]::DWORD)
}
}

$Value1 = $regkey.GetValue($key)
Add-Content $report “<td bgcolor= ‘Aquamarine’ align=center><B>$Value1</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

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