您的位置:首页 > 运维架构 > Shell

使用powershell脚本批量更新客户端配置文件

2013-01-11 23:21 447 查看
2012-1-11

背景:

Oracle升级并且服务器和IP地址同时更新,系统架构是2层C/S架构的,面临的是客户端200+需要更新tnsnames.ora,且Oracle安装目录不一致,稍让安心的是Oracle客户端版本是统一的, OS都是windows 系统,并且都加入域。

解决方案:

使用powershell脚本在服务器端推送新版的tnsnames.ora

实施步骤:

结合.Net 框架编写远程读写注册表测试脚本

View Code

write-output "Replace TNSNames.ora now`nPlease Wait..."

#Create/format Excel

#$erroractionpreference = "SilentlyContinue"

$a = New-Object -comobject Excel.Application

$a.visible = $True

$b = $a.Workbooks.Add()

$b.workSheets.item(3).delete()

$b.WorkSheets.item(2).delete()

$b.WorkSheets.item(1).Name = "File_Delete"

$P = $b.WorkSheets.Item("File_Delete")

$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = "Machine Name"

$c.Cells.Item(1,2) = "PC Status"

$c.Cells.Item(1,3) = "Delete Status"

$d = $c.UsedRange

$d.Interior.ColorIndex = 40

$d.Font.ColorIndex = 9

$d.Font.Bold = $True

#define parameter

$intRow = 2

#get pc arrary

$strComputers = get-content .\pclist.txt

foreach ($strComputer in $strComputers)

{

$ping = new-object System.Net.NetworkInformation.Ping

$Reply = $ping.Send($strComputer)

if ($Reply.status -eq "success")

{

$reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $strComputer)

[System.String]$name="SOFTWARE\Oracle\KEY_OraClient10g_home1"

$regKey= $reg.OpenSubKey($name).GetValue("ORACLE_HOME")

$regKey=$regKey.Replace(":","$")

$tnsfilepath="\\"+$strComputer+"\"+$regKey+"\network\admin\"

$sourcetnsname=$tnsfilepath+"\tnsnames.ora"

$oldtnsfile=$tnsfilepath+"\tnsnames_Bak_"+[System.DateTime]::Now.ToString('yyyyMM')+".ora"

#backup

cp $sourcetnsname $oldtnsfile

$newtnsfile="\\"+"**.**.**.**\tnsnames.ora"

#upload the new version

cp $newtnsfile $sourcetnsname

$c.Cells.Item($intRow, 1) = $strComputer.ToUpper()

$c.Cells.Item($intRow, 2) = "Online"

$c.Cells.Item($intRow, 3) = "Replace Success"

$intRow = $intRow + 1

}

else

{

$c.Cells.Item($intRow, 1) = $strComputer.ToUpper()

$c.Cells.Item($intRow, 2) = "Offline"

$c.Cells.Item($intRow, 3) = "Delete failure"

$intRow = $intRow + 1

}

}

$d.EntireColumn.AutoFit()

write-output "TNSNames Modification finished`nComplete!"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: