Pour les professionnels de l’informatique > Forums - Accueil > Windows PowerShell > Anyone have an example of running a script as part of the session state or session configuration?
Poser une questionPoser une question
 

QuestionAnyone have an example of running a script as part of the session state or session configuration?

  • mardi 30 juin 2009 16:48CodeSlinger Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I have my own hosting application, with UI and RawUI implemented, that generally works fine and want the runspace to run profiles before I use the pipeline.

    If there is no concept of running them for me I want to load certain scripts and run them myself. I have tried using RunspaceInvoke(myRunspace) with the Profile variable set in the session and that does strange and unatural things that I have been unable to debug or catch [puts up my dialog from within the append call without returning to the mainform_load call].

    I have tried the below code with variations on the path separation characters. As soon as I execute the append statement, my forms dialog comes up and I never actually make it to the 2nd statement which creates the RunspaveInvoke object. My call stack shows me in some werid kind of inner message loop with the last call being System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop.

    mRunspace.RunspaceConfiguration.InitializationScripts.Append(

    new ScriptConfigurationEntry(@"C:\\Users\\[MyUserid]\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1", "Profile script"));
    RunspaceInvoke lRunspaceInvoke = new RunspaceInvoke(mRunspace); 

     

    The MSDN doc is no help as it refers to skin and bone examples that do not use these methods. I would love to know how to use RunspaceInvoke, RunspaceConfiguration, ScriptConfigurationEntry etc. in general but specifically to run some profile scripts. Seems these methods will get me what I want but how to use them is not readily apparent to me or does not work.

    Thanks, Dave
    • ModifiéCodeSlinger mardi 30 juin 2009 16:49remove extra linefeeds
    •  

Toutes les réponses

  • mardi 30 juin 2009 17:12CodeSlinger Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    OK the below actually worked but I would love still to see more info on the above methods if anyone can help orient...Dave

    // open the runspace and run the profile
    mRunspace.Open();
    RunspaceInvoke lRunspaceInvoke = new RunspaceInvoke(mRunspace);
    lRunspaceInvoke.Invoke(@"C:\\Users\\[myuserid]\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1");
  • mardi 30 juin 2009 18:06Marco ShawMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    My suggestion may also be to post any issues/stuff missing on Microsoft connect.

    I'm not an experienced developer, so I lack skills to help you out here.
  • mardi 30 juin 2009 18:47CodeSlinger Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I do figure this being TechNet and not MSDN, that most PS questions are PS usage related but am hoping some of the members are developers as well so hope OK to ask developer related question on this forum since there are no PS related forums in MSDN.

    Are you suggesting I ask Connect to answer questions, for some examples, or for more complete documentation? Or all of the above. I'm unclear as to their funciton as I actually am trying to get logged onto Connect. There is some sort of snafu with it using my live id and alias. Keeps me as guest even though I registered twice. No matter if IE7, IE8 or Firefox or Vista or Win7...Thanks Marco!
  • mercredi 1 juillet 2009 01:25Marco ShawMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Yes, this is for IT Pro related stuff.

    I've already pinged the PowerShell PM to consider adding more forums here.

    Microsoft Connect is for suggestions and reporting bugs.  A suggestion could be that more documentation is required...

    You can also check the forums at www.powershellcommunity.org.  There are some developer focused forums.  I also lurk there also.  I am able to answer some basic dev-oriented questions...
  • mercredi 1 juillet 2009 02:43CodeSlinger Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Thanks Marco. I think PS is a big deal and it would be great to have a dedicated developer forum in MSDN esp. given the condition of the documentation.
     - Dave