使用powershell脚本批量更新客户端配置文件
2012-1-11
背景:
Oracle升级并且服务器和IP地址同时更新,系统架构是2层C/S架构的,面临的是客户端200+需要更新tnsnames.ora,且Oracle安装目录不一致,稍让安心的是Oracle客户端版本是统一的, OS都是windows 系统,并且都加入域。
解决方案:
使用powershell脚本在服务器端推送新版的tnsnames.ora
实施步骤:
结合.Net 框架编写远程读写注册表测试脚本
View Code
1 $MachineName = '.' 2 3 $reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $MachineName) 4 5 [System.String]$name="SOFTWARE\Oracle\KEY_OraClient11g_home1" 6 7 $regKey= $reg.OpenSubKey($name).GetValue("ORACLE_HOME")
Powershell脚本读取PC列表,这部份没有编写代码,主要是这部分机器都属于CSR座席,在CIC系统中直接可以导出,稍处理后保存在pclist.txt即可使用。
最终脚本(主要一次性的操作,没有规划成函数调用方式):
View Code
1 write-output "Replace TNSNames.ora now`nPlease Wait..." 2 3 #Create/format Excel 4 5 #$erroractionpreference = "SilentlyContinue" 6 7 $a = New-Object -comobject Excel.Application 8 9 $a.visible = $True 10 11 12 13 $b = $a.Workbooks.Add() 14 15 $b.workSheets.item(3).delete() 16 17 $b.WorkSheets.item(2).delete() 18 19 $b.WorkSheets.item(1).Name = "File_Delete" 20 21 $P = $b.WorkSheets.Item("File_Delete") 22 23 24 25 $c = $b.Worksheets.Item(1) 26 27 28 29 $c.Cells.Item(1,1) = "Machine Name" 30 31 $c.Cells.Item(1,2) = "PC Status" 32 33 $c.Cells.Item(1,3) = "Delete Status" 34 35 36 37 $d = $c.UsedRange 38 39 $d.Interior.ColorIndex = 40 40 41 $d.Font.ColorIndex = 9 42 43 $d.Font.Bold = $True 44 45 46 47 #define parameter 48 49 $intRow = 2 50 51 52 53 #get pc arrary 54 55 $strComputers = get-content .\pclist.txt 56 57 foreach ($strComputer in $strComputers) 58 59 { 60 61 $ping = new-object System.Net.NetworkInformation.Ping 62 63 $Reply = $ping.Send($strComputer) 64 65 if ($Reply.status -eq "success") 66 67 { 68 69 $reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $strComputer) 70 71 [System.String]$name="SOFTWARE\Oracle\KEY_OraClient10g_home1" 72 73 $regKey= $reg.OpenSubKey($name).GetValue("ORACLE_HOME") 74 75 $regKey=$regKey.Replace(":","$") 76 77 $tnsfilepath="\\"+$strComputer+"\"+$regKey+"\network\admin\" 78 79 $sourcetnsname=$tnsfilepath+"\tnsnames.ora" 80 81 $oldtnsfile=$tnsfilepath+"\tnsnames_Bak_"+[System.DateTime]::Now.ToString('yyyyMM')+".ora" 82 83 #backup 84 85 cp $sourcetnsname $oldtnsfile 86 87 88 89 $newtnsfile="\\"+"**.**.**.**\tnsnames.ora" 90 91 #upload the new version 92 93 cp $newtnsfile $sourcetnsname 94 95 96 97 $c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 98 99 $c.Cells.Item($intRow, 2) = "Online" 100 101 $c.Cells.Item($intRow, 3) = "Replace Success" 102 103 104 105 $intRow = $intRow + 1 106 107 } 108 109 else 110 111 { 112 113 $c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 114 115 $c.Cells.Item($intRow, 2) = "Offline" 116 117 $c.Cells.Item($intRow, 3) = "Delete failure" 118 119 $intRow = $intRow + 1 120 121 } 122 123 } 124 125 126 127 $d.EntireColumn.AutoFit() 128 129 130 131 write-output "TNSNames Modification finished`nComplete!"