none
Storing and accessing the variables in powershell RRS feed

  • General discussion

  • Hello Scripters

    I would like to know how can I store a global variable which resides so that I can access even after the session is cancelled.

    For instance

    Script runs @ 4 AM

    Variable A = "success"

    Script runs @ 5 AM

    how can I access the variable A value which is  created  @ 4 AM cycle

    creating a file and reading  file one option, is there an better option or easy way in power shell

    Thanks in advance

    Saravana

    Friday, June 12, 2015 8:17 PM

All replies

  • What session?  PowerShell does not have sessions.  You can have a WMI session or a remote session.  I have jam sessions and Congress has Congressional sessions.

    What kind of a session are you having?

    If you are talking about scheduled taks then consider saving the session state to a file using $session | Export-CliXml state.clixml


    \_(ツ)_/

    Friday, June 12, 2015 9:53 PM
  • Thanks Jrv for the reply.Will it be good practice to use sleep statement for about 5 minutes and resume the execution of the scripts.Will there be performance use ?.

    Thank you

    Monday, June 15, 2015 5:19 PM
  • Hello Saravana,

    So you are saying, you are going to run a script at 4 AM and set a variable with some values and when you run the same script at 5 AM, you want to retrieve the same value/variable and make use of it?

    If thats the case, you can consider writing a environment variable and quit once you are done.

    Next time, you can read it from your script where ever you need to.

    Let me know, if this helps!

    Please mark as answer or vote as helpful if you find so!

    Thanks,

    Vasu

    Tuesday, June 16, 2015 8:21 AM
  • Environment variables do not normally persist across process execution boundaries.  They would have to be set at a session level.  This can be done.  I would use the registry as it would be less disruptive.  Reading and writing a file is amore usual method.


    \_(ツ)_/

    Tuesday, June 16, 2015 9:55 AM