Wednesday, September 14, 2011

Need to run an Oracle server in a VDI session?

 

Why, is a different questions, but if you do you will know about the listener.ora and the tnsnames.ora files that both reference the local computer name. You cant just set them to localhost.

But you can via a local GPO, startup script check them, replace them with some pre-formatted files and then pop in the local computer name and restart Oracle. Here is the VBS to do it.

PS: Yes I could use functions and subs but I didn’t so don't be a punisher. Long live the VBS batch file!

 

' Get domain, host name

Set WshNetwork = WScript.CreateObject("WScript.Network")

'WScript.Echo "Computer Name = " & WshNetwork.ComputerName

' "User Name = " & WshNetwork.UserName & vbCrLf & "Domain = " & WshNetwork.UserDomain

CompName=WshNetwork.ComputerName

DomName=WshNetwork.UserDomain

' WScript.Echo DomName &"\"& CompName

' check to see if the computer name is right

Const ForReading = 1

Set objRegEx = CreateObject("VBScript.RegExp")

objRegEx.Pattern = CompName

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("D:\oraclexe\app\oracle\product\10.2.0\server\NETWORK\ADMIN\tnsnames.ora", ForReading)

Do Until objFile.AtEndOfStream

strSearchString = objFile.ReadLine

Set colMatches = objRegEx.Execute(strSearchString)

If colMatches.Count > 0 Then

For Each strMatch in colMatches

' Wscript.Echo "found computer name: " &strSearchString & " Quiting."

' FTW quit.

Wscript.quit

Next

End If

Loop

objFile.Close

' Plan b

wscript.echo "put the files in place to update"

Set filesys=CreateObject("Scripting.FileSystemObject")

FolderLocation="D:\oraclexe\app\oracle\product\10.2.0\server\NETWORK\ADMIN\"

'wscript.echo FolderLocation & "*.prepped", FolderLocation & "*.ora"

filesys.CopyFile FolderLocation & "tnsnames.prepped", FolderLocation & "tnsnames.ora", true

filesys.CopyFile FolderLocation & "listener.prepped", FolderLocation & "listener.ora", true

set filesys=Nothing

'Stop Service

'wscript.echo "stoping"

strServiceName = "OracleServiceXE"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")

For Each objService in colListOfServices

objService.StopService()

Next

strServiceName = "OracleXETNSListener"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")

For Each objService in colListOfServices

objService.StopService()

Next

wscript.sleep 5000

' Update the text files with the computer name

Const ForWriting = 2

FileLocation1="D:\oraclexe\app\oracle\product\10.2.0\server\NETWORK\ADMIN\tnsnames.ora"

OldText="<servername>"

NewText=CompName

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(FileLocation1, ForReading)

strText = objFile.ReadAll

objFile.Close

strNewText = Replace(strText, OldText, NewText)

Set objFile = objFSO.OpenTextFile(FileLocation1, ForWriting)

objFile.WriteLine strNewText

objFile.Close

FileLocation2="D:\oraclexe\app\oracle\product\10.2.0\server\NETWORK\ADMIN\listener.ora"

'wscript.echo "changing" & Filelocation1 & Filelocation2 & "to " & NewText

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(FileLocation2, ForReading)

strText = objFile.ReadAll

objFile.Close

strNewText = Replace(strText, OldText, NewText)

Set objFile = objFSO.OpenTextFile(FileLocation2, ForWriting)

objFile.WriteLine strNewText

objFile.Close

' wait for the services to finish stoping from above

wscript.sleep 10000

' start them and done.

'Start Service

strServiceName = "OracleXETNSListener"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")

For Each objService in colListOfServices

objService.StartService()

Next

'Start Service

'wscript.echo "starting"

strServiceName = "OracleServiceXE"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")

For Each objService in colListOfServices

objService.StartService()

Next

wscript.quit

No comments:

Blog Archive