none
Powershell module in c# - How to set the variable scope?

    Question

  • Hi!

    I´m writing a module for Powershell in C#.

    But I don´t know how to set a variables scope like:

    $global:WPKGProfilesXML = "C:\Profiles.xml"

    Could someone help me?

    • Moved by Richard MuellerMVP Saturday, March 17, 2012 4:55 PM C# question (From:The Official Scripting Guys Forum!)
    Wednesday, March 07, 2012 8:48 AM

All replies

  • That is one way of declaring a global variable in Powershell.  Another way is:

    New-Variable -Name testvar -Value "Hello World" -Scope global


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, March 07, 2012 10:50 AM
  • I know, but I want to do this in Visual Studio / C#.

    Wednesday, March 07, 2012 11:01 AM
  • Ah, I suspected as much.  In that case, you should ask in the C# forum, here:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/threads


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, March 07, 2012 11:06 AM
  • Hi!

    I´m writing a module for Powershell in C#.

    But I don´t know how to set a variables scope like:

    $global:WPKGProfilesXML = "C:\Profiles.xml"

    Could someone help me?

    In C# modules we do not generally decalare variables 'into' PowerSHell.  variables are passed in and objects are returned.  It is up to the caller of teh CmdLet to capture and assign the results to a variable scoped by the calling script.

    It is posiible to call a CmdLet in the PowerSHell session from C#,  Just invoke the Cmdlet 'New-Variable'.  This is not a recommended design strategy and violates many good programing practices.


    ¯\_(ツ)_/¯

    Wednesday, March 07, 2012 12:24 PM
  • Here is a workaround that might do what you need,

    // workatound for setting a global variable frm inside a CmdLet written in C#.
    // get an instNce of teh session state.
    SessionState ss = new SessionState()
    // call the 'InvokeCommand.InvokeScript method.
    ss..InvokeCommand.InvokeScript('new-variable -scope global -name gtest -value 3')

    You can use GetVariable to return a reference to the variable.

    You should be careful about creating global variables from within a CmdLet.   It can be done but was not planned for as there is a GetVariable method but no SetVariable method.  I suspect this was by design.


    ¯\_(ツ)_/¯

    Saturday, March 17, 2012 6:09 PM
  • This ius probably a more correct and useful method:

    http://dotnetfiguredout.blogspot.com/2008/08/its-possible-you-faced-with-situation.html


    ¯\_(ツ)_/¯

    Saturday, March 17, 2012 6:41 PM
  • I need to set the path to an XML file, which is used in every cmdlet of the module.

    I solved it by passing the path to every cmdlet. I think it is cumbersome but it works.

    In my opinion I should call something like "connect-xml" cmdlet and set the path. Another cmdlet can then read the variable which is set.

    Anyhow, it works..... thank you all for reading and giving hints.

    Monday, March 19, 2012 3:55 PM
  • I need to set the path to an XML file, which is used in every cmdlet of the module.

    I solved it by passing the path to every cmdlet. I think it is cumbersome but it works.

    In my opinion I should call something like "connect-xml" cmdlet and set the path. Another cmdlet can then read the variable which is set.

    Anyhow, it works..... thank you all for reading and giving hints.

    When you design a module for import you can use the module setup code to define global variables.

    Take a look at other modules that define globals when installed.  The 'provider' model will show you how to do this I believe.

    WHat you are aasking is not how to create a clobal from a CmdLet but how to create a shared global for a group of CmdLets.

    I believe yu can also just declare a public varqailble at teh module level.  This would not be visible from PowerShell but would be shared and available to all CmdLet in the module.  In most cases the value can be set using a 'config' CmdLet.


    ¯\_(ツ)_/¯

    Monday, March 19, 2012 5:12 PM