Advertisement
Old-Lost

Global variables at module scope

Mar 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I recently ran into the scenario of needing global variables for use within a new module I was creating. This naturally lead to consulting the hive mind that is otherwise known as “The Internet”. Luckily enough, I stumbled across a [post by “GregM”] on the TechNet forums, that proposed an interesting solution.
  2.  
  3. 1. Create a module manifest for the module.
  4. 2. Update the “PrivateData” record in the manifest.
  5.  
  6. ...
  7. FileList = @()
  8. PrivateData = @{g1 = "string";g2 = "stringTwo";g3 = "stringThree"}
  9.  
  10. And that’s all it takes to initialize a global variable within the module scope. Now, on to the manipulation.
  11.  
  12. # Retrieving the global variable value and assigning it to a variable
  13. $test = $MyInvocation.MyCommand.Module.PrivateData['g1']
  14.  
  15. # Modify the global variable value
  16. $MyInvocation.MyCommand.Module.PrivateData['g1'] = $test
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement