none
PS global varibale change from a Task

    Domanda

  • I put global in a  windows task and calling the ps1 script. Value is not getting updated . Need to toggle a policy every 5 min for a test.

    Import-Module GroupPolicy
    New-Variable -Name a -Value 1 -Scope "Global"
    # assign a set of pre-made GPO's to the gpo variable
    $gpo1 = Get-GPO "test-1"
    $gpo2 = Get-GPO "test-2"



    if ($a % 2 ){

    New-GPLink $gpo1.DisplayName -target "dc=tree1, dc=test" -order 1
    Remove-GPLink $gpo2.DisplayName -target "dc=tree1, dc=test"

    }
    else
    {
    New-GPLink $gpo2.DisplayName -target "dc=tree1, dc=test" -order 1
    Remove-GPLink $gpo1.DisplayName -target "dc=tree1, dc=test"

    }

    function increment {
      $global:a++
    }

     increment $global:a


    # run a gp update
    "running a gp update process.."
    gpupdate

    If I execute this script I see modified value each time. If I put in WIndows task no change in value. Anything else to be done?

    martedì 26 giugno 2012 02:43

Risposte

  • I'm not sure how you run it in task scheduler, but I suspect you just have the scripts scheduled every 5 min and want script to update one of GPOs every second time. Well, if that is the case putting variables in global scope will have no effect at all. There is nothing that will put your script in a loop - so it will just exit. And so will Powershell.exe. And your global variable is now gone.

    You have at least three options to use:

    • use environment variable (keep in mind: to make change to environment variables persistent between sessions - you have to use [System.Environment]::SetEnvironmentVariable()
    • use registry to store your "flag"
    • export/ import variable using CliXML (that's overkill, but should work just fine)


    martedì 26 giugno 2012 06:44
  • As a note: if you decide to go with registry - here is a good article: http://blogs.technet.com/b/heyscriptingguy/archive/2012/05/09/use-powershell-to-easily-create-new-registry-keys.aspx

    Although, to be honest, I find myself liking the environment variable option. Just make sure to set it if it isn't already set upon execution of the script. Also - I like the idea of setting the value to True or False instead of incrementing all the time. Just a personal preference. Something like this:

    if($a -eq $True) {

    $a = $False

    ... do stuff ..

     } else {

    $a = $True

    ... do stuff ...

    }



    G. Samuel Hays

    martedì 26 giugno 2012 18:43

Tutte le risposte

  • I'm not sure how you run it in task scheduler, but I suspect you just have the scripts scheduled every 5 min and want script to update one of GPOs every second time. Well, if that is the case putting variables in global scope will have no effect at all. There is nothing that will put your script in a loop - so it will just exit. And so will Powershell.exe. And your global variable is now gone.

    You have at least three options to use:

    • use environment variable (keep in mind: to make change to environment variables persistent between sessions - you have to use [System.Environment]::SetEnvironmentVariable()
    • use registry to store your "flag"
    • export/ import variable using CliXML (that's overkill, but should work just fine)


    martedì 26 giugno 2012 06:44
  • As a note: if you decide to go with registry - here is a good article: http://blogs.technet.com/b/heyscriptingguy/archive/2012/05/09/use-powershell-to-easily-create-new-registry-keys.aspx

    Although, to be honest, I find myself liking the environment variable option. Just make sure to set it if it isn't already set upon execution of the script. Also - I like the idea of setting the value to True or False instead of incrementing all the time. Just a personal preference. Something like this:

    if($a -eq $True) {

    $a = $False

    ... do stuff ..

     } else {

    $a = $True

    ... do stuff ...

    }



    G. Samuel Hays

    martedì 26 giugno 2012 18:43