New-Variable question

Welcome Forums General PowerShell Q&A New-Variable question

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
3 years, 6 months ago.

  • Author
    Posts
  • #24240

    Participant
    Points: 0
    Rank: Member

    If i set a new-variable how long is it current for ? So for example if i do a new-variable during provisioning (During SSCM) will i be able to call on that variable at a later stage ? Its a com object that i want the value of later but can't call run the script later as the com object will not exist later.
    I could create XML file i guess.

  • #24241

    Member
    Points: 0
    Rank: Member

    New-Variable follows the same scoping rules as any other variable assignment. By default, it's only active for as long as your current scope is active. (Until the end of a script if the statement is in the script scope, unless the caller dot-sources the script into the global scope, etc.)

    PowerShell variables are never available past the end of the current session, so if you want something that works even if you restart PowerShell, you'd need to put it into a profile script, or something along those lines.

  • #24256

    Participant
    Points: 0
    Rank: Member

    Did you mean SCSM or SCCM?

    If you're wanting to store a variable that can be represented as a string, then you could use an environment variable, which you could read later

    [environment]::SetEnvironmentVariable('Variable1','Var1Value','machine')

    Or are you talking about actually wanting to export the object itself? The closest you could do to this would be to use Export-CliXML, but bear in mind that this makes an XML representation of your object(s).

  • #24262

    Participant
    Points: 0
    Rank: Member

    Sorry, SCCM.

    I want to pull the information towards the end of the odd build.

    The variable is set in provisoning by powershell.

  • #24269

    Participant
    Points: 0
    Rank: Member

    Are you by any chance using a task sequence? If so, then you can directly set and use task sequence variables. You can use the com object to read and write the task sequence variables.

    You've not said yet though whether it's the properties of an object that you want to store, or an individual string/number?

  • #24270

    Participant
    Points: 0
    Rank: Member

    Hi Tim,

    Yes am SCCM task sequence. It's the object im after.

The topic ‘New-Variable question’ is closed to new replies.