Hi
Recently I wanted to ensure a set of related scripts were able to access a set of values read in from a config file. My current method although works, does not seem reliable leading to my calling scripts bombing out with errors as $domain and others are empty.
In my getConfig file
# what script am I running Write-Host $MyInvocation.MyCommand.Path #... and the directory $directorypath = Split-Path $MyInvocation.MyCommand.Path # read the config file which is in the directory $configfile = $directorypath + '\config.xml' #read configuration file config.xml into variable $xmlfile $xmlfile = [xml](get-content "$configFile") # Determine the properties defined by xml elements we want to use cgoing forward $domain = $xmlfile.Configuration.Domain.Name Write-Host -fore Blue "Domain: $domain"
In each of my calling scripts I have
$scriptRoot = Split-Path $MyInvocation.MyCommand.Path Write-Host $scriptRoot # read the config file which is in the same directory obtain the variables we need for the functions below & "$scriptRoot\Get-SP2013Config.ps1"
# another attempt but this time with a module equally unreliable.
# Import-Module "$scriptRoot\Get-SP2013Config.psm1" -Global -Force Write-Host -ForegroundColor DarkGreen $domain
What I am seeing is the called script runs every time and displays the values I expect thereafter the values are lost and not in the scope of the calling script. I can fix the situation by running the called script before I invoke the calling script ( which subsequently calls the called script again) , thereafter my $domain etc are populated. What I want is a bomb proof method .