by psnovice at 2013-01-17 10:09:36
script1.ps1:by nohandle at 2013-01-17 11:04:51
-------------
Set-StrictMode -Version Latest
$global:var1=0
$global:var11=4
if ($var11 -eq 0)
{
$var1=3
}
else
{
$var1=4
}
Write-Host ("Value 1: " + $var1)
return $var1
script2.ps1:
----------------
Set-StrictMode -Version Latest
$var1main=""
$var1=""
$var1main=$var1
{.\script1.ps1}
Write-Host ("Value 1 main: " + $var1main)
Value 1 main: displays nothing.
What is wrong here?
$var1main=$global]
You have to address the var1 in the global scope.
But the global scope should not be used unless it is totally necessary. Passing the values by parameters is better approach.
{.\script1.ps1}
Or you try to dot source the script and use its variables?
by psnovice at 2013-01-17 11:47:51Thanks Nohandle.by nohandle at 2013-01-17 12:51:18
Still not working
Set-StrictMode -Version Latest
$global:var1main=""
$global:clearvar1=""
$var1main=$var1
{…\script1.ps1}
Write-Host ("Value 1 main: " + $var1main)
I am just trying to pass variable value from one script to another. Where can I find any example?
[quote="psnovice"]I am just trying to pass variable value from one script to another. Where can I find any example?[/quote]Can you describe what you are trying to do a bit more? Global variables are most likely not the way to go.by psnovice at 2013-01-22 07:24:19
In your script1 you define global variable. In the second script you run the first one (or you dot source it? I am not sure).
Let me go through the script2:
You do not ask for global var1 in the $var1main=$var1, becuse you do not refer to any scope you refer to local var1.
You assign the value from the var1 to the var1main->The var1main is empty.
You run the script1 and assign the global:var1
You output the empty var1main.
Hi,by DexterPOSH at 2013-01-29 17:24:58
This is working from running PS1 but not working when running PS1 from batch file:
sprint2.ps1:
-----------
Set-StrictMode -Version Latest
$var1main=""
$var1=""
if ($var1main -eq $var1) {.\script1.ps1}
$var1main=$var1
Write-Host ("Value 1 main: " + $var1main)
sprint1.ps1:
-----------
Set-StrictMode -Version Latest
$global:var1=0
$global:var11=0
if ($var11 -eq 0)
{
$var1=3
}
else
{
$var1=4
}
Write-Host ("Value 1: " + $var1)
$global:var1=$var1
Result of script2.ps1:
---------------------
Value 1 main: 3
test1.bat:
----------
powershell.exe " &""<Scriptpath>\script2.ps1"""
Result of test1.bat: Value 1 main: null
When running script2.ps1 from test1.bat batch file, "Value 1 main" is null but when running script2.ps1, the "Value 1 main" is 3.
Please help. what could be the issue.
Hi ,by nohandle at 2013-01-30 03:05:28
I feel that Jakub aka NoHandle explained it already. But I will give it another shot.
From what I can understand you need to access the global variable defined in Script1, in Script2.
For this you need to dot source the script1 in script2.
For example to dot source any script:. <path>\Scrip1.ps1
Then after you dot source the Script1 the variables defined in it e.g $var1 are available in Script2.
Currently your Script2 is executing the Script1.ps1…the following code from your script2 is not dot sourcing the script instead executing itif ($var1main -eq $var1) {.\script1.ps1}
Correct way to do above would beif ($var1main -eq $var1) {. .\script1.ps1}
Notice that two dots (.) separated by space. First dot will dot source it and the second one merely tells that the script is present in current directory.
Hope this makes sense.
Regards,
Dexter
[quote="DexterPOSH"]From what I can understand you need to access the global variable defined in Script1, in Script2.[/quote]by DexterPOSH at 2013-03-09 03:05:05
We discussed the issue on another forum and the purpose of this is to make two or more scripts share data. One script does the calculations, the second process the data and the third one probably logs it.
[quote="DexterPOSH"]you need to access the global variable defined in Script1, in Script2.
For this you need to dot source the script1 in script2.[/quote]
You do not need to dot source a script to access a variable defined in the global scope, but I see you meant something like this: Instead of sharing data by global variables use for example scoping which imports all the variables of the dot sourced script into the current scope.
Thanks Jakub,
I understand it more clearly now.
P.S. - I am an admirer of your work. Your blog is awesome