ddady
(Getting the hang of it)
2008-04-22 04:39 PM
A script that will update variables in another script

Hi all,

Is it possible to have a script with GET command [for example] in order to let a user to enter the data of variables in another script or even better in the same script?

Thanks in Advance


Les
(KiX Master)
2008-04-22 05:31 PM
Re: A script that will update variables in another script

Why don't you tell us what you are trying to achieve rather than how you are trying to achieve it.

ddady
(Getting the hang of it)
2008-04-22 06:43 PM
Re: A script that will update variables in another script

OK,

Well, i made a script that runs once a day on a server and checks if the free space on the local partition is less than what i have decided to be the minimum limit. If so, it sends a notification mail by BLAT.

Let say that this script's name is "CHECKDISK" and the script i'm looking for will be named "DATASCRIPT"

We are giving tech services to a large number of companies which their servers are different from company to company, i need to make some sort of script [DATASCRIPT] that will ask certain questions and will take the data such as [minimum disk size, email address to send from and to, mail relay name, etc] and that the DATASCRIPT will put it in the CHECKDISK script the values.

The DATASCRIPT will run only once in order to fill the necessary values in the CHECKDISK script

I hope i managed to explain myself


Glenn BarnasAdministrator
(KiX Supporter)
2008-04-22 06:51 PM
Re: A script that will update variables in another script

You might want to consider using an INI file or the system registry to define your parameters. The "process" script reads the ini file or registry values at startup. The "configure" script asks the questions and sets the values in the INI or registry.

We do something similar with our utilities- we use INI files stored in a standard path location. Since this "standard location" can vary between customers, this location is defined in a System environment var called S_CONFIG. We also use a standard registry path - "HKLM\SOFTWARE\ITCG\product" (where "ITCG" is our company initials).

Glenn


ddady
(Getting the hang of it)
2008-04-22 07:19 PM
Re: A script that will update variables in another script

Ok, so from what i understand, i should creat an INI file and than run the CONFIGURE script which will add the values to the INI file and then the PROCESS script will read the values from the INI?

If so, how do i tell the CONFIGURE script what to write and where ,and more important how the PROCESS script will know which data to take from the INI?

Can you please give an example?


Mart
(KiX Supporter)
2008-04-22 07:41 PM
Re: A script that will update variables in another script

If you create a default ini file and a so called configure script for each customer/task then you could fire up your configure script to set all or most variables in the ini file that are specific for that task or customer. The scripts used can then read stuff from the ini file using the ReadProfileString function and write back the result needed by an other script with the WriteProfileString function.
This way there is no need for sharing variables between two or more scripts and no scripting knowledge is necessary when setting up the task or changing things a bit one could just edit the ini file with a text editor.


Glenn BarnasAdministrator
(KiX Supporter)
2008-04-22 07:42 PM
Re: A script that will update variables in another script

Well, as an example from your CHECKDISK script..

you create a configure script - let's call it CDcfg.kix - it asks for the name of the email account to send mail to, and the name of the mail server.

Once it asks for and the user enters those values (and you potentially do some error checking, like verifying you can resolve the mail server name in DNS), you write the values to the config file (called CHECKDISK.ini):
 Code:
CLS
'Send mail to: ' Gets $MailUser ?
' Mail Server: ' Gets $MailServer ?
$ = WriteProfileString('%S_CONFIG%\checkdisk.ini', 'COMMON', 'MailUser', $MailUser)
$ = WriteProfileString('%S_CONFIG%\checkdisk.ini', 'COMMON', 'MailServer', $MailServer)

Of course, this assumes that you have properly defined the System environment var "S_CONFIG", and have created the path it refers to. This could be done by the process used to install this utility.

Your process script - CHECKDISK.KIX - would begin by loading its parameters:
 Code:
If Not Exist('%S_CONFIG%\checkdisk.ini')
  'ERROR: not configured!' ?
  Exit 2
EndIf
$MailUser = ReadProfileString('%S_CONFIG%\checkdisk.ini', 'COMMON', 'MailUser')
$MailServer = ReadProfileString('%S_CONFIG%\checkdisk.ini', 'COMMON', 'MailServer')

; rest of your script . . .

That's the basic idea - you will need to customize this to your specific needs.

Glenn


Les
(KiX Master)
2008-04-22 08:31 PM
Re: A script that will update variables in another script

You could GUIfy your config script with KiXforms or XLSInputBox() for a more professional look.

ddady
(Getting the hang of it)
2008-04-23 09:29 AM
Re: A script that will update variables in another script

Ok, I got the idea.

But i still don't understand the S_CONFIG thing and i will appreciate some explanation of how to work with it or configure it.

Regarding KiXforms, the commands look a lot like VB, is there any Drag&Drop GUI like VB?

Thanks in advance for all responses


Mart
(KiX Supporter)
2008-04-23 09:48 AM
Re: A script that will update variables in another script

 Originally Posted By: ddady
Ok, I got the idea.

But i still don't understand the S_CONFIG thing and i will appreciate some explanation of how to work with it or configure it.
....


S_CONFIG is set as an environment variable. You can see them by typing SET in a command window. Actually it is %S_ CONFIG%. It will hold the default path where the app or script is stored. The script will reference that to get the path it should use.
Setting it can be done from a command window or with a kix script using the commands SET or SETM

 Originally Posted By: ddady

....
Regarding KiXforms, the commands look a lot like VB, is there any Drag&Drop GUI like VB?
....


Yes there is. It’s called kixforms designer. It lets you create the form and place all kinds of controls (buttons, checkboxes listviews, etc…) on it and it will create the code to display the form and its controls. You will have to create the action for lets say a button in a script editor.


Richard H.Administrator
(KiX Supporter)
2008-04-23 11:48 AM
Re: A script that will update variables in another script

If you keep your CONFIG script, PROCESS script and INI file in the same folder you can make things a little more simple and use the built-in macro @SCRIPTDIR to determine the location:
 Code:
CLS
'Send mail to: ' Gets $MailUser ?
' Mail Server: ' Gets $MailServer ?
$ = WriteProfileString(@SCRIPTDIR+'\checkdisk.ini', 'COMMON', 'MailUser', $MailUser)
$ = WriteProfileString(@SCRIPTDIR+'\checkdisk.ini', 'COMMON', 'MailServer', $MailServer)

@SCRIPTDIR is populated automatically with the path to the directory that the script is run from, so saves you the effort of setting an environment variable.


Glenn BarnasAdministrator
(KiX Supporter)
2008-04-23 01:02 PM
Re: A script that will update variables in another script

Having scripts in multiple languages - Kix, Bat, & perl - the environment variable(s) allow me to place all scripts & utilities in one folder, and config files in another. Also allows me to apply different permissions to each folder, allowing authorized people to manipulate the config files but not the scripts.

On all servers and admin workstations at every client site, I create (via script) a /local and /local/bin folder structure. S_CONFIG defines the actual location of /local and S_BIN defines the /local/bin path. These are usually on C:. Kix32, some other utilities, and all kix/perl/bat scripts are in the /local/bin folder. Any config file is in /local. Scripts that need to call other scripts can reference $S_BIN%\scriptname, and config files are referenced by %S_CONFIG%\configfile.

A minor clarification of Mart's reply - the environment variable names are "S_BIN" and "S_CONFIG" - you can verify this by typing "set" at a command prompt. You reference or define the variable name by "VARNAME", but you reference its CONTENTS by "%VARNAME%".

Glenn

PS - the Customize script on my web site can be used to define these environment variables, create the folders, populate the folders with standard scripts, utilities, and script engines, and perform other basic configuration processes. It's all controlled by an ini file, so easily customized. I run it on all servers, and on the workstations of any system/network admin that needs access to the admin tools that it installs.


ddady
(Getting the hang of it)
2008-04-24 08:25 AM
Re: A script that will update variables in another script

OK, Just when i thought that i'm starting to understand all this S_CONFIG thing, you guys manage to confuse me

I'm sorry to be a BB, but when i type SET at the command prompt i see a big list and there is no S_CONFIG. Should i create it? If so, than how and where?

I'm sorry but those things are not so obvious to me, and if i wont ask i will bever learn and know. I will appreciate some more "step by step" explanation if it possible.

P.S - if i create a kix file with KIXFORM DESIGNER, will it run on any machine even if it doesnt have the KiXforms.NET Development Build installed on it?


Mart
(KiX Supporter)
2008-04-24 08:53 AM
Re: A script that will update variables in another script

You should create the environment variables. You can use the SET command from a command prompt or the SET, SETM or SETL build in commands in kix.

Type set /? at a command prompt to see all the options the Windows SET command has. The kix SET, SETM and SETL commands are all explained on page 46 and 50 of the 4.60 manual. There are differences. SETM for example requires access to HKEY_LOCAL_MACHINE that regular user do not have so it is choose your tool wisely.

As for the stuff created in KF designer. You need to install/register the Kixforms.NET or classic dll (depending on what version you are using) on each system you want to run it on.


Glenn BarnasAdministrator
(KiX Supporter)
2008-04-24 01:52 PM
Re: A script that will update variables in another script

S_CONFIG is what I use - it represents "System Config Folder" (which makes sense to me and my team). I also use S_LIB, which is where some external UDFs are kept - I don't use that too often now that I use KGen to generate my scripts. S_BIN is also defined, which represents the location for binaries - scripts, executables, and such that are not part of the core O/S.

This was meant to illustrate one method for defining a physical location for storage of config data, and a process for informing all scripts / programs of any language where their config files could be found. You can use any name that suits you, just be consistent.

When you install your utilities, you create the folder where they will be stored, along with the config subfolder, and then you define the SYSTEM environment variable(s) that identify the location(s). 99% of the time, it will be the same place (which might lead you to hard-code the path into your scripts - very bad!). The use of the environment var lets you easily handle the exceptions - Citrix servers come to mind, since they usually have no C: or D: drives themselves.

Glenn


ddady
(Getting the hang of it)
2008-04-27 12:03 AM
Re: A script that will update variables in another script

Sorry for the late response.

Ok, after looking in the command prompt and from your explanations.

If i write: SET [S_CONFIG=[c:\]] in the command prompt, It will configure the S_CONFIG folder in C directory?

Is Richard's way can save me the confusion of the SET thing?


Gargoyle
(MM club member)
2008-04-27 12:15 AM
Re: A script that will update variables in another script

Set is for enviromental variables ex: %systemdrive%, %path%, etc...
This does not configure a folder for you, but references one that is there already.

As an example:

At corporate headquarters
%PriServer% = SCorp
%AppServer% = SA_Corp

At a field office
%PriServer% = SFieldA
%AppServer% = SA_FieldA

At another field office
%PriServer% = SFieldB
%AppServer% = SA_FieldB

Now in my script I can just reference %PriServer% and use it to map everyones home drive to \\%PriServer%\Users$\Home\%UserName%

Or I need to launch a special application

Run \\%AppServer%\RunAtStart\WeAreWatching.EXE

So no matter where my servers are or what they are named I know that I have variable that I can use as a constant in my script.

Did that help any?


ddady
(Getting the hang of it)
2008-04-27 12:24 AM
Re: A script that will update variables in another script

So,you're saying that i should create a folder with the name S_CONFIG in C drive and put all the script inside [in my situation the INI file and scripts CONFIG and PROCESS] and than in the command prompt will type
SET %S_CONFIG% = c:\

That will solve my problem?


Gargoyle
(MM club member)
2008-04-27 02:48 AM
Re: A script that will update variables in another script

It really depends on your deployment method.

You mention that this is for multiple clients, I would assume that each enviroment is independent (unable to talk to each other or to a central site).

If you can explain what the deployment model would look like we can help you decide the best method for setting up variables and having constincy at the same time.


ddady
(Getting the hang of it)
2008-04-27 07:58 AM
Re: A script that will update variables in another script

OK!

A little background; I work in a company that gives IT services to about 50 companies. I made a script that should run only on those companies servers and that will notify us by mail [BLAT] if those servers free hard disks space is less than 10% from it's all capacity.

To make it easier let call this script "PROCESS", and the other script i'm looking to write will be "CONFIG".

Now, the script [PROCESS] run just fine, i have tested it on one company. Since every company has its own mail server, i was looking for an easier way to make the changes, such as: mail server name, mail sender address, etc. Instead of making those changes manually in the script [PROCESS],
The idea i had is to have another script [CONFIG] that will run only once in each company by our technicians and will ask them for the relevant information and will make those changes in the script [PROCESS] that will run by schedule one or twice a day.

Some of the solutions i have received here were by using an INI file and configure an enviroment variable with SET command that can be done from the script too.

Everything is clear to me [thanks to everybody here] but i can't realy understand how to do it. How to set this variable, how to write it, what the exact syntax? It doesnt matter that i will use this method on each and every company, i intend to make this SET thing by kix.

I hope that this will help you guys to help me :-)

p.s.

You guys doing a great job :-)


Gargoyle
(MM club member)
2008-04-27 10:22 AM
Re: A script that will update variables in another script

In this case I don't think that the SET is really going to work for you.

If I understand correctly, each site is wholly independent therefore you need the [CONFIG] script to write an .INI file where the [PROCESS] file can find it (reccomend a standardized location on all servers).

So on each site have the INI file located in a central location accesible by all servers at that site and just have the [PROCESS] file look there for it.

Make sense?


ddady
(Getting the hang of it)
2008-04-27 10:42 AM
Re: A script that will update variables in another script

Yes, it does.

So i guess that Richard's suggestion is the way form me. Just to put the 2 scripts and the INI file in one folder and that's it!?


 Originally Posted By: Richard H.
If you keep your CONFIG script, PROCESS script and INI file in the same folder you can make things a little more simple and use the built-in macro @SCRIPTDIR to determine the location:
 Code:
CLS
'Send mail to: ' Gets $MailUser ?
' Mail Server: ' Gets $MailServer ?
$ = WriteProfileString(@SCRIPTDIR+'\checkdisk.ini', 'COMMON', 'MailUser', $MailUser)
$ = WriteProfileString(@SCRIPTDIR+'\checkdisk.ini', 'COMMON', 'MailServer', $MailServer)

@SCRIPTDIR is populated automatically with the path to the directory that the script is run from, so saves you the effort of setting an environment variable.


Glenn BarnasAdministrator
(KiX Supporter)
2008-04-27 02:20 PM
Re: A script that will update variables in another script

You guys are missing the point...

If you work for one company and have total control, needing a variable place to put your scripts and config files is probably not necessary. However, if you write a script that it going to be used at many different companies, you need to build-in some flexibility to account for the control you don't have.

Step 1 - decide on a folder structure that you PREFER. I use:
C:\Local for my config files
C:\local\bin for my scripts and utilities (kix32.exe, *.kix, *.bat, blat.exe, etc)
Create this folder structure and populate it with the files necessary.

Step 2 - Create names that REPRESENT these physical locations. I use:
S_LOCAL to represent the path to the config files (C:\local)
S_BIN to represent the path to other scripts and utilities (C:\local\bin).
Define these names on your server as PERMANENT, SYSTEM environment variables. For testing, you can do this manually via My Computer/Properties/Advanced/Environment Variables.

Step 3 - Change your script to reference the config and utility file locations using the names defined above. For example, commands such as
$BlatCmd = 'C:\local\bin\blat.exe'
become
$BlatCmd = '%S_BIN%\blat.exe'

and config file references such as
$Data = ReadProfileString('c:\local\mydata.ini', 'this', 'that')
become
$Data = ReadProfileString('%S_CONFIG%\mydata.ini', 'this', 'that')

Now, when you create scripts, they reference the logical place where config files and utility programs are maintained. So - each time you go to install a new script on a customer's computer, your install script:
  • Prompts you for the location of the S_CONFIG root path, accepting your standard location as a default value
  • Checks for %S_CONIG% - if not defined, it creates S_CONFIG and S_BIN system variables and the folders they represent
  • Copies all the script's files to the appropriate locations

This is precisely what my Customize script does. I create a customize.ini before visiting a client, populate the source folders with the scripts and utilities I want to be able to use while serviceing their systems, and run the script on each server, and any admin workstation where administration tools will be needed. By taking a moment to define the customize.ini for that client (when necessary) I eliminate the prompt for the install directory for this and other utilities. Any install script for add-on utilities - such as my LogCleanup tool - checks for the presence of the S_CONFIG variable and exits if it wasn't defined, complaining that the system was not prepped.

Using this method, I accomplish two things that are important to ME - 1.) I don't have to modify scripts to use them at multiple clients, and 2.) I keep my config files separate from my script folders, making them easier to find.

Keep in mind that this still provides a great deal of flexibility. I have a backup script. The script is in the S_BIN folder, but it has several related configuration files, logs, and more. It references its config files in %S_CONFIG%\Backup\backup.ini, and logs as &S_CONFIG\Backup\Logs\sessionID.log. In fact, most of my scripts write their logs to %S_CONFIG%\logs\... - either directly to that folder, or to a subfolder if they write lots of logs.

The key point of this exercise is to be able to write a script ONCE and use it at HUNDREDS of different client configurations. You can handle the odd situation, too - like having an old server with too little space on C: by moving the C:\Local folder to another drive and simply redefining the two environment vars - no coding changes!

Glenn


Sealeopard
(KiX Master)
2008-04-27 04:28 PM
Re: A script that will update variables in another script

Couldn't you extract the names of the mail servers or other servers from AD? You could then have one central provisioning script that deploys the server monitoring scripts or alternatively use remote WMI access from a single central script to perform the monitoring? Otherwise, any changes to the servers in the company's domain would require corresponding changes in the configuration files or redeployment of the monitoring scripts.

Glenn BarnasAdministrator
(KiX Supporter)
2008-04-27 06:36 PM
Re: A script that will update variables in another script

Yeah, if every one of your clients used AD, or Exchange... and - in my current environment, the Exchange server won't accept mail from anything but the defined mail relay/spam filter server, which isn't in AD.

The key to this concept is flexibility - not adjusting or adapting your scripts (plural!) to each customer environment, but rather adjusting one set of parameters shared by al scripts. Could be an INI file, but again - where? How do the ALL scripts locate it? @SCRIPTDIR is one way, but not one I prefer. I guess it comes from my *ix background, where config files were generally centralized, and not maintained in the same folder as the commands that used them..

I can walk into any client environment, run my customize script (with any appropriate tweaks) and then install/run any other script in my library without any scripting change. That's an important and powerful capability when you're supporting dozens of different client networks.

Glenn


ddady
(Getting the hang of it)
2008-04-27 10:32 PM
Re: A script that will update variables in another script

Thanks Glenn, that's what i needed, your explanation was very detailed. Step 2 was what i've been missing or better say didn't understand.

Will try it tomorrow on the company server.

BTW, why doing it manually from My Computer/Properties/Advanced/Environment Variables is only for testing? Isn't suppose to be permanent?


Glenn BarnasAdministrator
(KiX Supporter)
2008-04-28 01:37 PM
Re: A script that will update variables in another script

Glad it helped..

As for your question about permanence - no, anything you set via the "DOS" SET command is temporary (and hence, good for testing). Once you exit the command prompt, the settings you defined in that session are gone.

You need to use the Kix Set or SetM commands, which permanently alter the specific user's environtment (Set) or that of the entire system (SetM). These do NOT change the active environment, so to see the effects, you need to open a new command prompt after you run the Set or SetM commands.

Again - look at the Customize package from my web site - you could use it by simply modifying the INI file and loading the tools and wstools folders with the utilities you want (in addition to or instead of the provided samples). Thus - one command would configure your systems and load them with the scripts and tools you want.

Glenn