Windows Scripting Practices: VBS script code _VBS for SAP patches

Source: Internet
Author: User
Tags chr
Scripting main features include:

Registry Read and Modify
File content modifications such as hosts, services files
File property modification and file copying
System environment variable Settings
And so on, for reference only

Copy Code code as follows:

' SAP setup Script
' Writing: SCZ 2005.04.20
' Last Modification Date: 2005.04.22
' Directory must exist: BW (patch file) and login interface
'========================================================================
' Global variables, process
'========================================================================
WScript.Echo "This script can only run correctly under WIN2000/XP/2003 operating system administrator privileges, press ' OK ' to continue"
Set objFSO = CreateObject ("Scripting.FileSystemObject") ' File system object
Strwindir = Getwindir () ' Get Windows directory
Strsystem = Getsystempath () ' Get system directory
Strsappath = Getsappath () ' SAP frontend directory
Strsapguipath = Strsappath & "SAPgui" SAPgui Directory
Strsapbwpath = Strsappath & "BW" BW Directory
Strhostpath = Directory of Gethostfilepath () ' Host file
Strservicespath = Directory of Getservicespath () ' Services files

Call CopyFiles () ' Copy file
Call Modifyhost (Strhostpath) ' Modify host File
Call Modifyservices (Strservicespath) ' Modify Services file
Call SETEVN (strsapguipath) ' Set environment variable
Call Settcpip (Strservicespath) ' Modify TCPIP parameters
WScript.Echo "BW settings are processed, please install SAP system patches Manually"



'========================================================================
' Get the SAP Frontend directory by registering
'========================================================================
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 registration information failed to read, SAP is not installed or system is corrupted, installation terminated"
Err.Raise (507)
Err.Clear
End If
End Function


'========================================================================
' Get Windows directory
'========================================================================
Function Getwindir ()
Const Windowfolder = 0
Set Getwindir = Objfso.getspecialfolder (Windowfolder)
End Function


'========================================================================
' Get the system directory
'========================================================================
Function Getsystempath ()
Const Systemfolder = 1
Set Getsystempath = Objfso.getspecialfolder (Systemfolder)
End Function


'========================================================================
' Get the directory where the host file resides
'========================================================================
Function Gethostfilepath ()
Gethostfilepath = Strsystem & "Driversetc"
End Function


'========================================================================
' Get the directory where the services file resides
'========================================================================
Function Getservicespath ()
Getservicespath = Strsystem & "Driversetc"
End Function

'========================================================================
' Copy files
'========================================================================
Function CopyFiles ()
If not objfso.folderexists (Strsapbwpath) Then
WScript.Echo "BW component is not installed, please install the BW component of SAP before running the script"
Err.Raise (507)
Err.Clear
End If

Call Clearattribs (Strsapbwpath)

Objfso.copyfile "Landing Interface *.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

'========================================================================
' Remove file read-only property
'========================================================================
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

'========================================================================
' Remove file read-only property
'========================================================================
Function Clearfileattrib (strfile)
If objfso.fileexists (strfile) Then
Set f = objfso.getfile (strfile)
F.attributes = 0
End If
End Function

'========================================================================
' Modify the Host file
'========================================================================
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

'========================================================================
' Modify a Services file
'========================================================================
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

'========================================================================
' Set Environment variables
'------------------------------------------------------------------------
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

'========================================================================

'========================================================================
' Set TCP/IP parameters
'------------------------------------------------------------------------
Function Settcpip (Strservicespath)
Const HKEY_LOCAL_MACHINE = &AMP;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
'========================================================================

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.