none
How to configure ExecutionPolicy as “RemoteSigned” in Powershell 4.0 api

    Pertanyaan

  • I need to configure Execution Policy as 'RemoteSigned' at initial session state or somehow before execution of script. I don't want to execute script to set policy. This would alter the policy at client machine which i don't want.

    In Powershell 5.0 reference assemblies one can do easily,

    var iss = InitialSessionState.CreateDefault(); iss.ExecutionPolicy = ExecutionPolicy.RemoteSigned

    but how can I achieve the same while remaining with Powershell 4.0 as referenced assemblies.

    C# code to execute script

    var iss = InitialSessionState.CreateDefault();
    iss.ExecutionPolicy = ExecutionPolicy.RemoteSigned; --> this is OK for Powershell ReferenceAssemblies 5.0 but not 4.0
    
    iss.ImportPSModule(new[] { typeof(Parameter).Assembly.Location });
    using (var powerShell = PowerShell.Create(iss))
    {
         var psScript = _inlineScript ?? File.ReadAllText(_path);
         powerShell.AddScript(psScript);
    }
    down votefavori

    Selasa, 28 Agustus 2018 20.58

Semua Balasan

  • Hi Usman Khalil,

    Thank you for posting here.

    For your question is more related to PowerShell, I will move it to PowerShell forum for suitable support.

    https://social.technet.microsoft.com/Forums/en-US/home?forum=winserverpowershell

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Rabu, 29 Agustus 2018 06.54
  • Thanks for your effort.

    But I can't my question there. Can you please help me to point out and exact location where I can follow my posted question.

    Rabu, 29 Agustus 2018 07.49
  • Hi Usman Khalil,

    Sign your account and the Click Ask a question in the link which I provided in my previous reply.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Rabu, 29 Agustus 2018 08.04