WINDOWS脚本实践:为SAP补丁制作的VBS脚本代码

所属分类: 脚本专栏 / vbs 阅读数: 2191
收藏 0 赞 0 分享
脚本主要功能包括:

注册表读取与修改 
文件内容修改如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 
'========================================================================
更多精彩内容其他人还在看

vbscript Split函数用法详解(字符串转数组函数)

本文详细介绍了vbscript中split函数的用法,有关split函数的一些实例,vbscript中split函数的语法介绍,有需要的朋友参考下
收藏 0 赞 0 分享

VBS实现将Excel表格保存为txt文本

希望能够找到个能给excel表另存为TXT的VBS代码,虽然另存为可以选择,但还是需要直接VBS执行这一步另存为TXT格式的,应该如何写代码呢
收藏 0 赞 0 分享

vbs解决处理TXT文本数据相关问题实现代码

使用vbs处理txt数据时,会遇到一些问题,本文将提供详细的解决方法,希望可以帮助你们
收藏 0 赞 0 分享

关于vbs 生成静态页面过程中出现的问题

访问某个aspx页面,然后把服务器返回的页面以filestream 的形式保存为html格式,接下来将介绍实现代码
收藏 0 赞 0 分享

dim函数第三个参数设置截取字符的长度问题

dim函数的第三个参数,也就是截取字符的长度,我在设置这个的时候,出了些问题,提示无效的过程调用或参数,本文详细介绍如何解决
收藏 0 赞 0 分享

邪恶的eval和new Function使用介绍

这些代码可以干什么?可以肯定的是可以干很多猥琐的事
收藏 0 赞 0 分享

WScript.Shell对象SpecialFolders属性未公开文档分享

WshShell对象的SpecialFolders属性返WshSpecialFolders 对象,该对象是一个特殊文件夹集合,其中包含整套Windows特殊文件夹
收藏 0 赞 0 分享

用VBS修改(设置)系统时间和日期的代码

用 VBS 把系统的时间修改到2038年1月19日3时14分07秒之后,这样某些依赖于 Unix 时间戳的程序就会出问题。那么怎样用 VBS 修改系统的时间呢
收藏 0 赞 0 分享

VBS For Next循环的一些细节

For Next 语句是 VBS 中常用的循环语句,你可能已经用过无数次 For Next 循环,但是你是否注意过它的一些细节呢
收藏 0 赞 0 分享

VBS For Next循环的陷阱分享

在《VBS For Next循环的一些细节》中说到了 For Next 循环的控制变量仅仅在循环开始前求值一次,但是其中存在一处陷阱
收藏 0 赞 0 分享
查看更多