Dynamic Variables and Runspaces

Welcome Forums General PowerShell Q&A Dynamic Variables and Runspaces

Viewing 5 reply threads
  • Author
    Posts
    • #245220
      Participant
      Topics: 1
      Replies: 3
      Points: 41
      Rank: Member

      Hi All, this is my first post and i’m a hack, so please be gentle.

      I’ve created a bunch of runspaces manually each with their own variable names etc,

      I’d like to make a template script or function to tidy up my code

      I assume this would require the use of New-Variable to dynamically generate the variable names,

      but I cant work out then how to access the runspace properties assigned to that dynamic variable

      Help please

      I’ve included working and test code below:

       

    • #246177
      Participant
      Topics: 8
      Replies: 565
      Points: 2,158
      Helping Hand
      Rank: Community Hero

      You need to provide a string to New-Variable, not a variable, so remove the dollar sign. Here’s an example of how to create, capture, and manipulate dynamic variables.

      [/crayon]

      Hope it helps

    • #246216
      Participant
      Topics: 1
      Replies: 3
      Points: 41
      Rank: Member

      Hi Doug, Thanks for replying

      I need to access the dynamically generated variable name directly rather than through an assigned variable name as you’ve done ($variable),

      This code let’s me do that, but how to I edit one of the properties, such as set “ThreadOptions = “ReuseThread””, or change the Runspace Name?

      thanks

      james

       

       

      Name                           Value                                                                                                                                                                        

      —-                           —–                                                                                                                                                                        

      runspaceactiveUsers            System.Management.Automation.Runspaces.LocalRunspace                                                                                                                         

      Events                 : 

      ThreadOptions          : Default

      JobManager             : 

      Debugger               : 

      RunspaceConfiguration  : System.Management.Automation.Runspaces.RunspaceConfigForSingleShell

      InitialSessionState    : 

      Version                : 5.1.18362.752

      RunspaceStateInfo      : BeforeOpen

      RunspaceAvailability   : None

      ConnectionInfo         : 

      OriginalConnectionInfo : 

      LanguageMode           : 

      ApartmentState         : Unknown

      RunspaceIsRemote       : False

      InstanceId             : 8a205646-e78d-4dc0-8318-f2e1691811ac

      DisconnectedOn         : 

      ExpiresOn              : 

      Name                   : Runspace44

      Id                     : 44

      SessionStateProxy      : System.Management.Automation.Runspaces.SessionStateProxy 

    • #246222
      Participant
      Topics: 1
      Replies: 3
      Points: 41
      Rank: Member

      Ahhh, I worked it out

       

       #name of runspace set manually or obtained from code

    • #246231
      Participant
      Topics: 8
      Replies: 565
      Points: 2,158
      Helping Hand
      Rank: Community Hero

      I see your variable turned into a string,

      my mistake. 🙂

      It seems the ideas have helped nonetheless.

      Glad you got it working!

    • #246234
      Participant
      Topics: 1
      Replies: 3
      Points: 41
      Rank: Member

      thanks mate, much appreciated

       

Viewing 5 reply threads
  • You must be logged in to reply to this topic.