Sunday, January 5, 2014

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.




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

Reach me:- http://msexchange.me

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 ""
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 " "
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 ""
Add-Content $report ""
Add-Content $report ""
Add-Content $report " "
#####Get Registry Value ####
Server NameExistingValueModifiedValue
foreach ($Server in $servers)
{
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($main, $Server)
$regKey= $reg.OpenSubKey($path,$True)
$Value = $regkey.GetValue($key)
Add-Content $report " "
if ($value -eq $null)
{
Add-Content $report "
$Server
"
Add-Content $report "
$nexist
"
$regkey.SetValue($key,$tout,[Microsoft.Win32.RegistryValueKind]::DWORD)
}
elseif ($value -ne $null)
{
Add-Content $report "
$Server
"
Add-Content $report "
$value
"
if ($value -ne $tout)
{
$regkey.SetValue($key,$tout,[Microsoft.Win32.RegistryValueKind]::DWORD)
}
}
$Value1 = $regkey.GetValue($key)
Add-Content $report "
$Value1
"
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)
########################################################################


Regards
Sukhija Vikas

No comments:

Post a Comment