VBScript基于WSH编程
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,大学时期也用过VBScript,不过都是基于ASP的,近期因工作需要,尝试在WSH(windows script host)下编程,实现列示oracle client下tnsnames.ora文件的主
千家信息网最后更新 2025年02月01日VBScript基于WSH编程
大学时期也用过VBScript,不过都是基于ASP的,近期因工作需要,尝试在WSH(windows script host)下编程,实现列示oracle client下tnsnames.ora文件的主要信息(TNSname、HOST、SID),大体思路是:判断当前系统下oracle路径,从系统变量中读取具体path,通过Wscript下的文件对象读取文件,分隔path,截取ora文件中的需要信息,下面是具体实现的脚本:
option explicit'on error resume nextcall MainSub Main dim homes homes = ReadOracleHome call ReadTNS(homes)End Sub'Date:2014-10-15 'Author:AlanFunction ReadTNS(homes) dim i,j,k,fs,ts,f,strAll,strTSNname(100),strHost(100),strServiceName(100),contents,content(10),strTotal,txt set fs =WScript.CreateObject("Scripting.FileSystemObject") For i=0 to UBound(homes) If homes(i) <> "" Then '判断tns文件是否存在 If fs.FileExists(Mid(homes(i),1,Len(homes(i))-3)&"network\admin\tnsnames.ora") = true Then '读取tnsnames.ora文件 set f=fs.getfile(Mid(homes(i),1,Len(homes(i))-3)&"network\admin\tnsnames.ora") set ts = f.OpenAsTextStream(1,0) do while ts.AtEndOfStream <> true For j=0 to 100 if ts.AtEndOfStream =true then exit for End if txt= ts.ReadLine If InStr(StrReverse(Trim(txt)),"=")=1 and InStr(Trim(txt),"(")=0 Then strTSNname(j)=StrReverse(Mid(Trim(StrReverse(txt)),InStr(Trim(StrReverse(txt)),"=")+1)) 'msgbox strTSNname(j) ElseIf InStr(txt,"(HOST =")>0 Then If InStr(txt,")(PORT =")>0 Then strHost(j)=Trim(SplitStr(txt,"(HOST =",")(PORT =")) Else strHost(j)=Trim(SplitStr(txt,"(HOST =",")")) End If 'msgbox strHost(j) ElseIf InStr(txt,"(SERVICE_NAME=")>0 or InStr(txt,"(SID =")>0 Then IF InStr(txt,"(SERVICE_NAME=")>0 Then strServiceName(j)=Trim(SplitStr(txt,"(SERVICE_NAME=",")")) ElseIf InStr(txt,"(SID =")>0 Then strServiceName(j)=Trim(SplitStr(txt,"(SID =",")")) End IF 'msgbox strServiceName(j) End If 'msgbox "TNSName="+strTSNname(j)+" HOST="+strHost(j)+" SERVUCE_NAME/SID="+strServiceName(j)+vbNewLine Next Loop '去除数组中的空值 dim a,b,c,nstrTSNname,nstrHost,nstrServiceName a=sumArrayNotNullValue(strTSNname) b=sumArrayNotNullValue(strHost) c=sumArrayNotNullValue(strServiceName) nstrTSNname= dropArrayNullValue(strTSNname,maxThree(a,b,c)) nstrHost= dropArrayNullValue(strHost,maxThree(a,b,c)) nstrServiceName= dropArrayNullValue(strServiceName,maxThree(a,b,c)) '组织文本显示效果 For k=0 to maxThree(a,b,c) IF nstrTSNname(k) <>"" and nstrHost(k)<>"" and nstrServiceName(k)<>"" Then If contents="" Then contents="TNSName="+addSpaceToString(getMaxLength(nstrTSNname),nstrTSNname(k))+"HOST="+addSpaceToString(getMaxLength(nstrHost),nstrHost(k))+" SERVUCE_NAME/SID="+nstrServiceName(k)+vbNewLine 'contents="TNSName="+nstrTSNname(k)+" "+"HOST="+nstrHost(k)+" "+"SERVUCE_NAME/SID="+nstrServiceName(k)+" "+vbNewLine Else contents=contents+"TNSName="+addSpaceToString(getMaxLength(nstrTSNname),nstrTSNname(k))+"HOST="+addSpaceToString(getMaxLength(nstrHost),nstrHost(k))+" SERVUCE_NAME/SID="+nstrServiceName(k)+vbNewLine 'contents=contents+"TNSName="+nstrTSNname(k)+" "+"HOST="+nstrHost(k)+" "+"SERVUCE_NAME/SID="+nstrServiceName(k) +" "+vbNewLine End If End If Next 'msgbox contents content(i)=Mid(homes(i),1,Len(homes(i))-3)&"network\admin\tnsnames.ora 中TNS信息如下:" & vbNewLine+contents '置空本次循环的字符串变量,便于下次循环重新赋值 contents = "" '关闭对象 set ts=nothing set f= nothing Else MsgBox "在"& Mid(homes(i),1,Len(homes(i))-3)& "network\admin\ 路径下未找到tnsnames.ora文件。" ,,"Information" End If End If Next set fs = nothing For i=0 to UBound(content) IF content(i) <>"" Then strTotal=strTotal+content(i)+vbNewLine End IF Next call PopupShowStr(strTotal)End Function'读取ORACLE_HOME,返回一个homes数组(因为可能有多个oracle_home)Function ReadOracleHome dim WshShell,WshSysEnv,path,paths,i,oracle_homes,counter Set WshShell = WScript.CreateObject("WScript.Shell") Set WshSysEnv=WshShell.Environment("SYSTEM") path= WshSysEnv("PATH") '从系统环境变量PATH中分离出ORACLE_HOME paths = Split(path,";",-1,1) counter=0 For i=0 to UBound(paths) If InStr(UCase(paths(i)),"\APP\")>0 Then If InStr(UCase(paths(i)),"\PRODUCT\")>0 Then 'MsgBox "Found valid path: " & paths(i) oracle_homes = paths(i) paths(i)="" paths(counter)=oracle_homes counter=counter+1 End If Else paths(i)="" End If Next '函数FilterOracleHome的返回值为paths数组 ReadOracleHome = pathsEnd Function'定义截取指定字符串之间字符串的函数Function SplitStr(mainStr,findStartStr,findEndStr) dim strSplit,intStart,intLength If InStr(mainStr,findStartStr) >0 Then intStart=InStr(mainStr,findStartStr)+Len(findStartStr) intLength= InStr(mainStr,findEndStr) - intStart strSplit = Mid(mainStr,intStart,intLength) End If SplitStr = strSplitEnd Function'定义一个弹出框显示文本内容的函数Function PopupShowStr(string) Dim WshShell Set WshShell = WScript.CreateObject("WScript.Shell") '将文本内容显示在弹出框中 call WshShell.Popup(string,0,"TNS Lists:")End Function'定义一个去除数组中空值的函数Function dropArrayNullValue(array,counter) dim i,j Redim arrNew(CInt(counter)) '如果array(i)为空,则后面的值往前移 For i=0 to UBound(array) IF array(i)<>"" Then arrNew(j)=array(i) j=j+1 End IF Next dropArrayNullValue=arrNewEnd Function'定义计算数组中非空值的个数函数Function sumArrayNotNullValue(array) dim i,j j=0 For i=0 to UBound(array) IF array(i)<>"" Then j=j+1 End IF Next sumArrayNotNullValue=jEnd Function'求三个数中的最大值Function maxThree(a,b,c) dim max max =a if a>b then if b>c then max=a else if a>c then max=a else max=c end if end if else if b>c then max=b else max=c end if end if maxThree=maxEnd Function'获取字符串数组中值的最大长度Function getMaxLength(array) dim i,max max=Len(Trim(array(0))) For i=0 to UBound(array) if Len(Trim(array(i))) > max then max=Len(Trim(array(i))) end if Next getMaxLength=maxEnd Function'统一字符串数组中各值的长度,长度不够补空格Function addSpaceToString(max,str) if max-Len(str)>0 then addSpaceToString=str +String(max-Len(str)," ") ' Space(max-Len(str)) else addSpaceToString=str end ifEnd Function
效果展示:
数组
文件
函数
字符
字符串
信息
变量
文本
系统
长度
最大
个数
内容
对象
效果
路径
循环
编程
不够
中值
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
云服务器的安全组与防火墙设置
微擎数据库主机填什么
盐城企业软件开发
服务器装什么安全系统
软件开发的研究生工资标准
服务器故障码130
java服务器端程序设计
服务器软件是什么
vpn连接数据库
山茶数据库
联通服务器怎么进入
mysql数据库支持中划线
兰州大学网络安全学院教师待遇
大数据对数据库的使用情况
ibm服务器选硬盘开机
网络安全学习app推荐
常见的数据库故障类型有哪些
传统软件开发把软件开发
数据库生成新表
电商环境的网络安全
服务器管理面板推荐
挑选网络技术开发源头好货
腾讯云开发服务器配置
计算机网络技术专升本吗
四川服务器电源厂商哪家好
软件开发计算机系统
北京it软件开发推荐
淘淘商城的数据库设计
acess数据库导出查询结果
数据库的收录范围