Solved

execute a file containing variables to be initialized at the start of another script

Posted on 2012-03-14
4
285 Views
Last Modified: 2012-03-26
Hello,

Is it possible to execute a file containing variables to be initialized at the start of another script?

If yes, how?

Thanks

regards

bibi
0
Comment
Question by:bibi92
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 37720037
Hi
The easiest way would to be define global variables in the variables file and example of this is

$global:variable1 = " this is variable 1"
$global:variable2 = " this is another one"

in your main script call the variable file like this

.\myvariables.ps1
write-host $variable1

Then at the end of the main script delete the global variables using the following command

 Remove-Variable variable1 -scope global

There are many methods that  can be used , another is create an xml file as that contain the variables.

then import them.

to import the variables  you could use the following.
example xml file is
<?xml version="1.0" encoding="utf-8"?>
<Config>
  <SQL>
    <SOURCE>D:\SQL2008sp2</SOURCE>
    <PCUSOURCE>D:\SQL2008sp2\PCU</PCUSOURCE>
    <CUSOURCE>D:\SQLSPandCu\cu10</CUSOURCE>
    <SQLPort>1433</SQLPort>
    <DriveLetter>D</DriveLetter>
 </SQL>
</Config>

Open in new window


then in your main script add
$ConfigFile = (get-item "myvariablesxml").fullname
[xml] $xmlinput =  get-content $ConfigFile

you can then access the item as follows

$sqlport = $xmlinput.config.sql.sqlport



I hope you find this helpful

Joe
0
 

Author Comment

by:bibi92
ID: 37721894
Hello thanks and is it possible to créateur a global.env which contain variables and call it in a script. Thanks bibi
0
 
LVL 11

Accepted Solution

by:
Joe Klimis earned 500 total points
ID: 37722945
try the following , run as administrator
[Environment]::SetEnvironmentVariable("JBOSS_HOME","D:\Entwicklung\Programme\jboss-as", "Machine")
[Environment]::SetEnvironmentVariable("M2_HOME",D:\Entwicklung\Programme\apache-maven", "Machine")
[Environment]::SetEnvironmentVariable("TOMCAT_HOME","D:\Entwicklung\Programme\apache-tomcat", "Machine")
[Environment]::SetEnvironmentVariable("ANT_HOME",D:\Entwicklung\Programme\apache-ant", "Machine")


[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";D:\Entwicklung\Programme\mysql\bin;D:\Entwicklung\Programme\apache-maven\bin;D:\Entwicklung\Programme\apacheant\bin;D:\Entwicklung\Programme\eclipse\jre\bin", "Machine")
              

Open in new window

0
 

Author Closing Comment

by:bibi92
ID: 37766474
Thanks regards bibi
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will help you understand what HashTables are and how to use them in PowerShell.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question