WINDOWS脚本实践:为SAP补丁制作的VBS脚本代码
程序员文章站
2022-06-16 20:02:59
脚本主要功能包括: 注册表读取与修改 文件内容修改如hosts、services文件 文件属性修改和文件复制 系统环境变量设置 ...
脚本主要功能包括:
注册表读取与修改
文件内容修改如hosts、services文件
文件属性修改和文件复制
系统环境变量设置
等,仅供参考
'sap设置脚本
'编写:scz 2005.04.20
'最后修改日期: 2005.04.22
'必须存在目录: bw(补丁文件) 和 登入界面
'========================================================================
'全局变量、处理过程
'========================================================================
wscript.echo "该脚本只能正常运行在win2000/xp/2003的操作系统管理员权限下,按'确定'继续"
set objfso = createobject("scripting.filesystemobject") '文件系统对象
strwindir = getwindir() '获取windows目录
strsystem = getsystempath() '获取system目录
strsappath = getsappath() 'sap frontend目录
strsapguipath = strsappath & "sapgui" 'sapgui目录
strsapbwpath = strsappath & "bw" 'bw目录
strhostpath = gethostfilepath() 'host 文件所在目录
strservicespath = getservicespath() 'services 文件所在目录
call copyfiles() '复制文件
call modifyhost(strhostpath) '修改host文件
call modifyservices(strservicespath) '修改services文件
call setevn(strsapguipath) '设置环境变量
call settcpip(strservicespath) '修改tcpip参数
wscript.echo "bw设置处理完毕,请手动安装sap系统补丁"
'========================================================================
'通过注册获取sap frontend目录
'========================================================================
function getsappath()
const hkey_local_machine = &h80000002
strcomputer = "."
set objreg=getobject("winmgmts:{impersonationlevel=impersonate}!\" & _
strcomputer & " ootdefault:stdregprov")
strkeypath = "softwaresapsap shared"
strentryname = "sapdestdir"
objreg.getstringvalue hkey_local_machine,strkeypath,strentryname,strvalue
getsappath = strvalue
if isnull(strvalue) then
wscript.echo "sap注册信息读取失败,sap未安装或系统已损坏,安装终止"
err.raise(507)
err.clear
end if
end function
'========================================================================
'获取windows目录
'========================================================================
function getwindir()
const windowfolder = 0
set getwindir = objfso.getspecialfolder(windowfolder)
end function
'========================================================================
'获取system目录
'========================================================================
function getsystempath()
const systemfolder = 1
set getsystempath = objfso.getspecialfolder(systemfolder)
end function
'========================================================================
'获取host文件所在目录
'========================================================================
function gethostfilepath()
gethostfilepath = strsystem & "driversetc"
end function
'========================================================================
'获取services文件所在目录
'========================================================================
function getservicespath()
getservicespath = strsystem & "driversetc"
end function
'========================================================================
'复制文件
'========================================================================
function copyfiles()
if not objfso.folderexists(strsapbwpath) then
wscript.echo "bw组件未安装,请先安装sap的bw组件,再运行该脚本"
err.raise(507)
err.clear
end if
call clearattribs(strsapbwpath)
objfso.copyfile "登陆界面*.ini" , strwindir
objfso.copyfile "bwgssntlm.dll" , strsapguipath & "gssntlm.dll"
objfso.copyfile "bwsncgss32.dll" , strsystem & "sncgss32.dll"
strbakfolder =strsapbwpath & "ak"
if not objfso.folderexists(strbakfolder) then
objfso.createfolder(strbakfolder)
else
call clearattribs(strbakfolder)
end if
objfso.copyfile strsapbwpath & "*.xla" , strbakfolder
objfso.copyfile "bw*.xla" , strsapbwpath
end function
'========================================================================
'去除文件只读属性
'========================================================================
function clearattribs(strfolder)
call clearfileattrib(strfolder & "sapbex.xla")
call clearfileattrib(strfolder & "sapbexc.xla")
call clearfileattrib(strfolder & "sapbexs.xla")
call clearfileattrib(strfolder & "sapbex0.xla")
call clearfileattrib(strsystem & "sncgss32.dll")
end function
'========================================================================
'去除文件只读属性
'========================================================================
function clearfileattrib(strfile)
if objfso.fileexists(strfile) then
set f = objfso.getfile(strfile)
f.attributes = 0
end if
end function
'========================================================================
'修改host文件
'========================================================================
function modifyhost(strhostpath)
strhostfile = strhostpath & "hosts"
strhostbak = strhostpath & "hosts.bak"
const forreading = 1, forwriting = 2, forappending = 8
objfso.copyfile strhostfile , strhostbak
set objfile = objfso.opentextfile(strhostfile, forreading, false)
strcontents = objfile.readall
objfile.close
set objfile = objfso.opentextfile(strhostfile, forappending, false)
objfile.writeblanklines 1
compresult = instr(strcontents,"192.168.0.136")
if compresult = 0 then objfile.writeline("192.168.0.136" & chr(9) & "bwprd")
compresult = instr(strcontents,"192.168.0.135")
if compresult = 0 then objfile.writeline("192.168.0.135" & chr(9) & "bwdev")
compresult = instr(strcontents,"192.168.0.171")
if compresult = 0 then objfile.writeline("192.168.0.171" & chr(9) & "bwqas")
objfile.close
end function
'========================================================================
'修改services文件
'========================================================================
function modifyservices(strservicespath)
strservicesfile = strservicespath & "services"
strservicesbak = strservicespath & "services.bak"
const forreading = 1, forwriting = 2, forappending = 8
objfso.copyfile strservicesfile , strservicesbak
set objfile = objfso.opentextfile(strservicesfile, forreading, false)
strcontents = objfile.readall
objfile.close
set objfile = objfso.opentextfile(strservicesfile, forappending, false)
objfile.writeblanklines 1
compresult = instr(strcontents, "sapmsp01")
if compresult = 0 then objfile.writeline("sapmsp01" & chr(9) & "3600/tcp")
objfile.close
end function
'========================================================================
'设置环境变量
'------------------------------------------------------------------------
function setevn(strsapguipath)
strcomputer = "."
set objwmiservice = getobject("winmgmts:\" & strcomputer & " ootcimv2")
set colitems = objwmiservice.execquery( "select * from win32_environment where name = 'snc_lib'")
found = false
for each objitem in colitems
if ucase(objitem.name) = "snc_lib" then
found = true
objitem.variablevalue = strsapguipath & "gssntlm.dll"
objitem.put_
end if
next
if (found = false) then
set oevn = objwmiservice.get("win32_environment").spawninstance_
oevn.name = "snc_lib"
oevn.variablevalue = strsapguipath & "gssntlm.dll"
oevn.systemvariable = true
oevn.username = "<system>"
oevn.status = "ok"
set opath = oevn.put_
end if
end function
'========================================================================
'========================================================================
'设置tcp/ip参数
'------------------------------------------------------------------------
function settcpip(strservicespath)
const hkey_local_machine = &h80000002
strcomputer = "."
set objreg=getobject("winmgmts:{impersonationlevel=impersonate}!\" & _
strcomputer & " ootdefault:stdregprov")
strkeypath = "systemcurrentcontrolsetservicestcpipparameters"
strentryname = "databasepath"
objreg.setstringvalue hkey_local_machine,strkeypath,strentryname,strservicespath
end function
'========================================================================
注册表读取与修改
文件内容修改如hosts、services文件
文件属性修改和文件复制
系统环境变量设置
等,仅供参考
复制代码 代码如下:
'sap设置脚本
'编写:scz 2005.04.20
'最后修改日期: 2005.04.22
'必须存在目录: bw(补丁文件) 和 登入界面
'========================================================================
'全局变量、处理过程
'========================================================================
wscript.echo "该脚本只能正常运行在win2000/xp/2003的操作系统管理员权限下,按'确定'继续"
set objfso = createobject("scripting.filesystemobject") '文件系统对象
strwindir = getwindir() '获取windows目录
strsystem = getsystempath() '获取system目录
strsappath = getsappath() 'sap frontend目录
strsapguipath = strsappath & "sapgui" 'sapgui目录
strsapbwpath = strsappath & "bw" 'bw目录
strhostpath = gethostfilepath() 'host 文件所在目录
strservicespath = getservicespath() 'services 文件所在目录
call copyfiles() '复制文件
call modifyhost(strhostpath) '修改host文件
call modifyservices(strservicespath) '修改services文件
call setevn(strsapguipath) '设置环境变量
call settcpip(strservicespath) '修改tcpip参数
wscript.echo "bw设置处理完毕,请手动安装sap系统补丁"
'========================================================================
'通过注册获取sap frontend目录
'========================================================================
function getsappath()
const hkey_local_machine = &h80000002
strcomputer = "."
set objreg=getobject("winmgmts:{impersonationlevel=impersonate}!\" & _
strcomputer & " ootdefault:stdregprov")
strkeypath = "softwaresapsap shared"
strentryname = "sapdestdir"
objreg.getstringvalue hkey_local_machine,strkeypath,strentryname,strvalue
getsappath = strvalue
if isnull(strvalue) then
wscript.echo "sap注册信息读取失败,sap未安装或系统已损坏,安装终止"
err.raise(507)
err.clear
end if
end function
'========================================================================
'获取windows目录
'========================================================================
function getwindir()
const windowfolder = 0
set getwindir = objfso.getspecialfolder(windowfolder)
end function
'========================================================================
'获取system目录
'========================================================================
function getsystempath()
const systemfolder = 1
set getsystempath = objfso.getspecialfolder(systemfolder)
end function
'========================================================================
'获取host文件所在目录
'========================================================================
function gethostfilepath()
gethostfilepath = strsystem & "driversetc"
end function
'========================================================================
'获取services文件所在目录
'========================================================================
function getservicespath()
getservicespath = strsystem & "driversetc"
end function
'========================================================================
'复制文件
'========================================================================
function copyfiles()
if not objfso.folderexists(strsapbwpath) then
wscript.echo "bw组件未安装,请先安装sap的bw组件,再运行该脚本"
err.raise(507)
err.clear
end if
call clearattribs(strsapbwpath)
objfso.copyfile "登陆界面*.ini" , strwindir
objfso.copyfile "bwgssntlm.dll" , strsapguipath & "gssntlm.dll"
objfso.copyfile "bwsncgss32.dll" , strsystem & "sncgss32.dll"
strbakfolder =strsapbwpath & "ak"
if not objfso.folderexists(strbakfolder) then
objfso.createfolder(strbakfolder)
else
call clearattribs(strbakfolder)
end if
objfso.copyfile strsapbwpath & "*.xla" , strbakfolder
objfso.copyfile "bw*.xla" , strsapbwpath
end function
'========================================================================
'去除文件只读属性
'========================================================================
function clearattribs(strfolder)
call clearfileattrib(strfolder & "sapbex.xla")
call clearfileattrib(strfolder & "sapbexc.xla")
call clearfileattrib(strfolder & "sapbexs.xla")
call clearfileattrib(strfolder & "sapbex0.xla")
call clearfileattrib(strsystem & "sncgss32.dll")
end function
'========================================================================
'去除文件只读属性
'========================================================================
function clearfileattrib(strfile)
if objfso.fileexists(strfile) then
set f = objfso.getfile(strfile)
f.attributes = 0
end if
end function
'========================================================================
'修改host文件
'========================================================================
function modifyhost(strhostpath)
strhostfile = strhostpath & "hosts"
strhostbak = strhostpath & "hosts.bak"
const forreading = 1, forwriting = 2, forappending = 8
objfso.copyfile strhostfile , strhostbak
set objfile = objfso.opentextfile(strhostfile, forreading, false)
strcontents = objfile.readall
objfile.close
set objfile = objfso.opentextfile(strhostfile, forappending, false)
objfile.writeblanklines 1
compresult = instr(strcontents,"192.168.0.136")
if compresult = 0 then objfile.writeline("192.168.0.136" & chr(9) & "bwprd")
compresult = instr(strcontents,"192.168.0.135")
if compresult = 0 then objfile.writeline("192.168.0.135" & chr(9) & "bwdev")
compresult = instr(strcontents,"192.168.0.171")
if compresult = 0 then objfile.writeline("192.168.0.171" & chr(9) & "bwqas")
objfile.close
end function
'========================================================================
'修改services文件
'========================================================================
function modifyservices(strservicespath)
strservicesfile = strservicespath & "services"
strservicesbak = strservicespath & "services.bak"
const forreading = 1, forwriting = 2, forappending = 8
objfso.copyfile strservicesfile , strservicesbak
set objfile = objfso.opentextfile(strservicesfile, forreading, false)
strcontents = objfile.readall
objfile.close
set objfile = objfso.opentextfile(strservicesfile, forappending, false)
objfile.writeblanklines 1
compresult = instr(strcontents, "sapmsp01")
if compresult = 0 then objfile.writeline("sapmsp01" & chr(9) & "3600/tcp")
objfile.close
end function
'========================================================================
'设置环境变量
'------------------------------------------------------------------------
function setevn(strsapguipath)
strcomputer = "."
set objwmiservice = getobject("winmgmts:\" & strcomputer & " ootcimv2")
set colitems = objwmiservice.execquery( "select * from win32_environment where name = 'snc_lib'")
found = false
for each objitem in colitems
if ucase(objitem.name) = "snc_lib" then
found = true
objitem.variablevalue = strsapguipath & "gssntlm.dll"
objitem.put_
end if
next
if (found = false) then
set oevn = objwmiservice.get("win32_environment").spawninstance_
oevn.name = "snc_lib"
oevn.variablevalue = strsapguipath & "gssntlm.dll"
oevn.systemvariable = true
oevn.username = "<system>"
oevn.status = "ok"
set opath = oevn.put_
end if
end function
'========================================================================
'========================================================================
'设置tcp/ip参数
'------------------------------------------------------------------------
function settcpip(strservicespath)
const hkey_local_machine = &h80000002
strcomputer = "."
set objreg=getobject("winmgmts:{impersonationlevel=impersonate}!\" & _
strcomputer & " ootdefault:stdregprov")
strkeypath = "systemcurrentcontrolsetservicestcpipparameters"
strentryname = "databasepath"
objreg.setstringvalue hkey_local_machine,strkeypath,strentryname,strservicespath
end function
'========================================================================