ARS scheduled task script cannot use a script scope variables

I need to SET a script scope variable value within a function but it appears that the ARS scheduled task environment command shell does not have these available. Here's a simple script to demonstrate the issue ( if you run this in a powershell or even the ARS command shell it works as expected but as a scheduled task the script scope variable is not updated. 

Returning the value from the function is not an option as the actual task is dynamic, so I don't know until run time what the variable name is and I'm using invoke-expression to actually set the value which is where I thought the original issue lay, but invoke-expression does work, I've tested it and the issue is 100% with the variable not being available.  I just added proof of that in the test function below - the output inside the function shows blank values for the script scope variables when run in an ARS scheduled task.  

Function Set-MyVariableValues {

"Variable 1 in function : '$script:myvariable1'" | Out-file c:\temp\MyVariableValueFunction.txt -append
"Variable 2 in function : '$script:myvariable2'" | Out-file c:\temp\MyVariableValueFunction.txt -append
$script:Myvariable1 = 2
Invoke-Expression '$script:Myvariable2 = 3'


}

$myvariable1 = 1
$Myvariable2 = 2

"Variable 1 value was : '$myvariable1'" | Out-file c:\temp\MyVariableValue.txt
"Variable 2 value is now : '$myvariable2'" | Out-file c:\temp\MyVariableValue.txt -Append

Set-MyVariableValues

"Variable 1 value was : '$myvariable1'" | Out-file c:\temp\MyVariableValue.txt -Append
"Variable 2 value is now : '$myvariable2'" | Out-file c:\temp\MyVariableValue.txt -Append


<#
when run in a powershell session you get the correct output
Variable 1 value was : 1
Variable 2 value is now : 2
Variable 1 in function : '1'
Variable 2 in function : '2'
Variable 1 value was : 2
Variable 2 value is now : 3

when run as an ARS scheduled task you get unchanged values
Variable 1 value was : 1
Variable 2 value is now : 2
Variable 1 in function : ''
Variable 2 in function : ''
Variable 1 value was : 1
Variable 2 value is now : 2

#>

  • Would a global variable work for you?

    i.e.

    $MyVariable:global in your function?

  • HI, no that has the same issue but it did remind me of the one thing I hadn't tried which was to explicitly declare the variable scope in the main part of the script and this resolves the issue, i.e. as long as I use $global:MyVariable or $script:MyVariable everywhere it works as expected.  

    It's probably a feature of the powershell command shell that any variable declared in the main script outside of a function is of scope global and script ( as from the script point of view the two are the same ) so you don't need to declare it as a script or global variable if you are declaring it at the top of your script.  Inside a function you can read the value of any variable by referencing it directly without adding the scope to the variable name as long as there is not a local variable declared within the function itself of course.  

    This has always been the source of errors because it appears you have access to the variable but it's read only.  Try to assign a value to it and you will actually be instantiating a new variable with local scope which will not be available outside of the function.  If you want to update it then you add the scope to the variable name and it is then possible to update the variable.  You can even create it inside the function if you want to and I make use of this sometimes.    

    It seems that unless you explicitly declare the variable scope then it is unavailable to your function even if you explicitly use the scope in the variable name.  I used to do this to all my variables I intended to use in this was to avoid the confusion created when you use the same name variable inside a function and I'd never 'cheat' by using the variable name if I was just referencing it.  Lately I've obviously decide to be a little less formal and have not been prepending my script scope variables in the main script as it IS the same thing inside most command shells.  It seems Quest are being a little more pedantic here and I can't blame them, I will go back to my better scripting practice of always adding the variable scope if I intend to use a variable and not pass it by reference or return the value from a called function.  

    thanks - we've both been on these forums a long time and I see you still respond really quickly!