create config file with VB script

The following script will demonstrate how to create configuration file for an application via VB script. We are currently running it as a login script. It’s very simple but does the job 100%. It grabs environmental variables and then generates individual config file for each computer. In my example we care about hostname and system architecture.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'Name: settings.vbs
'Created by: Naz Snidanko | nsnidanko@wajax.com
'Date: 11/03/2013
'Dexcription: Script to generate AS400 profile file .ws 
'15/03/2013 - N.S. - fixed x64 -> displayed as AMD64
'17/03/2013 - N.S. - replaced %HOMEPATH% with %USERPROFILE%
 
'Function to parse computer name and generate Workstation ID
Function WorkstID (strHostName1, strHostBranch1)
Dim strHostNumb
'Get Hostname number
strHostNumb = Right(strHostName1, 3)
'generate Workstation ID if host number is higher than 099
If ( Left(strHostNumb, 1) = "0" ) Then
WorkstID = "CA" & strHostBranch1 & Right(strHostNumb, 2)
'generate Workstation ID if host number is lower than 100
Else
WorkstID = "CA" & strHostBranch1 & strHostNumb
End If
End Function
 
Dim wshShell, objFSO, objTextFile, sFileName, sArchitecture, sHostName, strSetting
Const ForWriting = 2
Set wshShell = CreateObject( "WScript.Shell" )
sArchitecture = wshShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
sHostName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objFSO = CreateObject("Scripting.FileSystemObject")
sFileName = wshShell.ExpandEnvironmentStrings("%USERPROFILE%\Desktop")
sFileName = sFileName & "\AS400.ws"
'This is to capture if file already created. Do not execute the rest...
If objFSO.FileExists(sFileName) Then
WScript.Quit
Else
 
 'Set path depending on computer architecture
 if (sArchitecture = "x86") Then
 strSetting = "C:\Program Files\IBM\Client Access\Emulator\Private\STDAS400."
 ElseIf sArchitecture = "AMD64" Then
 strSetting = "C:\Program Files (x86)\IBM\Client Access\Emulator\Private\STDAS400."
 end if
  'Toronto branch code 2
  If InStr(sHostName, "TOR") Then
  strLine = WorkstID (sHostName, "2")
  'Hamilton branch code 3
  ElseIf InStr(sHostName, "STO") Then
  strLine = WorkstID (sHostName, "3")
  'London branch code 4
  ElseIf InStr(sHostName, "LON") Then
  strLine = WorkstID (sHostName, "4")
  'Ottawa branch code 5
  ElseIf InStr(sHostName, "OTT") Then
  strLine = WorkstID (sHostName, "5")
  'Niagara branch code 6
  ElseIf InStr(sHostName, "NIA") Then 
  strLine = WorkstID (sHostName, "6")
  'Sudbury branch code 7
  ElseIf InStr(sHostName, "SUD") Then
  strLine = WorkstID (sHostName, "7")
  'Timmins branch code 8
  ElseIf InStr(sHostName, "TIM") Then
  strLine = WorkstID (sHostName, "8")
  'Thunder Bay branch code T 
  ElseIf InStr(sHostName, "THU") Then
  strLine = WorkstID (sHostName, "T")
  Else
    'catch error + CON, COR, PEM - stop script execution
    WScript.Quit
  End If
 
   ' Overwrite any existing file
   Set objTextFile = objFSO.OpenTextFile (sFileName, ForWriting, True)
 
   ' Write the configuration settings
   objTextFile.WriteLine("[Profile]")
   objTextFile.WriteLine("ID=WS")
   objTextFile.WriteLine("Version=8")
   objTextFile.WriteLine("[CT]")
   objTextFile.WriteLine("trace=Y")
   objTextFile.WriteLine("[Telnet5250]")
   objTextFile.WriteLine("HostName=WDDA400")
   objTextFile.WriteLine("AssociatedPrinterStartMinimized=N")
   objTextFile.WriteLine("AssociatedPrinterClose=N")
   objTextFile.WriteLine("AssociatedPrinterTimeout=0")
   objTextFile.WriteLine("Security=CA400")
   objTextFile.WriteLine("SSLClientAuthentication=Y")
   objTextFile.WriteLine("CertSelection=AUTOSELECT")
   objTextFile.WriteLine("AutoReconnect=Y")
   objTextFile.WriteLine("[Communication]")
   objTextFile.WriteLine("Link=telnet5250")
   objTextFile.WriteLine("Session=5250")
   objTextFile.WriteLine("[5250]")
   objTextFile.WriteLine("ScreenSize=27x132")
   objTextFile.WriteLine("HostCodePage=037-U")
   objTextFile.WriteLine("WorkStationID=") & strLine
   objTextFile.WriteLine("PrinterType=IBM3812")
   objTextFile.WriteLine("[Keyboard]")
   objTextFile.WriteLine("CuaKeyboard=2")
   objTextFile.WriteLine("Language=United-States")
   objTextFile.WriteLine("IBMDefaultKeyboard=N")
   objTextFile.WriteLine("DefaultKeyboard=") & strSetting & ("KMP")
   objTextFile.WriteLine("[Poppad]")
   objTextFile.WriteLine("IBMDefaultPoppad=N")
   objTextFile.WriteLine("DefaultPoppad=") & strSetting & ("PMP")
   objTextFile.WriteLine("[HotSpot]")
   objTextFile.WriteLine("Exists=Y")
   objTextFile.WriteLine("template1=PF PFnn")
   objTextFile.WriteLine("template2=PF FPnn")
   objTextFile.WriteLine("template3=PF Fnn")
   objTextFile.WriteLine("template4=PF nn")
   objTextFile.WriteLine("template5=URL")
   objTextFile.WriteLine("template6=cursor")
   objTextFile.WriteLine("[Colors]")
   objTextFile.WriteLine("OtherColumnSeparator=000000")
   objTextFile.WriteLine("[Window]")
   objTextFile.WriteLine("SessFlags=38C63")
   objTextFile.WriteLine("ViewFlags=CE00")
   objTextFile.WriteLine("RuleLinePos=0 0")
   objTextFile.WriteLine("[LastExitView]")
   objTextFile.WriteLine("A=3 150 150 853 682 3 11 20 400 0 IBM3270— 37")
   objTextFile.WriteLine("[printers]")
   objTextFile.WriteLine("printer=DEFAULT")
   objTextFile.WriteLine("Drawer1Orient=Portrait")
   objTextFile.WriteLine("Drawer2Orient=Portrait")
   objTextFile.WriteLine("VTPrintArea=Scroll")
   objTextFile.WriteLine("VTPrintChar=ASCII")
   objTextFile.WriteLine("VTTerminator=None")
   objTextFile.Close
End If
This entry was posted in Random stuff. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *