none
Running Powershell -File command from Factory.Automation under Lightswitch RRS feed

  • Question

  • I have a small LightSwitch application that opens Powershell session upon activation.  It looks like so:
    using System;
    using System.Linq;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Runtime.InteropServices.Automation;
    using System.Collections.Generic;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Framework.Client;
    using Microsoft.LightSwitch.Presentation;
    using Microsoft.LightSwitch.Presentation.Extensions;
    namespace LightSwitchApplication
    {
        public partial class LS_Process_Spawner
        {
            partial void LS_Process_Spawner_Activated()
            {
                // Write your code here.
                if (AutomationFactory.IsAvailable)
                {
                    this.Hello_World = "xxxxxxx";
                    dynamic shell = AutomationFactory.CreateObject("shell.Application");
                    // ShellExecute(file, Args, Directory, Operation, ShowNum)
                    shell.ShellExecute("powershell.exe", "-NoExit -Command C://Users//dot_net//sleep.exe 20", "", "", 1);
                }
                else
                {
                    this.ShowMessageBox("Automation not available");
                }
            }
        }
    
        }
    


    This application runs fine.  However, if I substitute the -Command parameter with the -file qualifier and a ps1 file parameter containing the same command, I get the following error stating that the excution policy on the system is restricted against script execution.  Sure enough, I let the -Command called sleep program finish (with the -NoExit qualifier) and check the execution policy I see it is restriced:

    Get-ExecutionPolicy

    Restricted

    However, I changed the execution policy on the machine as the administrator in another Powrshell session:

    Get-ExecutionPolicy

    Restricted

    How can I have two different system-level policies at the same time on the same system?  If I run the same ps1 script in a non-adminstrator powershell session, it executes.  Only fails when I call it under LightSwitch. 

    Any assistance appreciated.

     

     

     


    markdolar
    Sunday, November 6, 2011 4:23 PM

Answers

  • The Execution policy is based on the user and profile running the script. One way to fix it is to change the execution policy for the user that is running the Lightswitch session.... but that will also depend on where the script is running. It will be easy to test n the local machine, but if you deploy to a web application you are going to have to know the application pools identity so that you can set the policy for that.

    Aother option which is the best option is to sign the script and set up the certificate to be allowed to run scripts on the machine, this can be done through group policey and other ways, but I would check the powershell guys for that.


    http://www.virtualrealm.com.au - SQL Server DBA, MVP and Lightswitch Enthusiast.
    Monday, November 7, 2011 1:22 AM

All replies

  • The Execution policy is based on the user and profile running the script. One way to fix it is to change the execution policy for the user that is running the Lightswitch session.... but that will also depend on where the script is running. It will be easy to test n the local machine, but if you deploy to a web application you are going to have to know the application pools identity so that you can set the policy for that.

    Aother option which is the best option is to sign the script and set up the certificate to be allowed to run scripts on the machine, this can be done through group policey and other ways, but I would check the powershell guys for that.


    http://www.virtualrealm.com.au - SQL Server DBA, MVP and Lightswitch Enthusiast.
    Monday, November 7, 2011 1:22 AM
  • Will signing the script work if the policy is set to restricted?  Thanks
    markdolar
    Monday, November 7, 2011 5:49 PM