WINDOWS指令碼實踐:為SAP補丁製作的VBS指令碼代碼_vbs

來源:互聯網
上載者:User
指令碼主要功能包括:

註冊表讀取與修改 
檔案內容修改如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 
'========================================================================
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.