在Azure Web应用程序中设置执行策略Powershell

时间:2017-07-31 21:11:04

标签: powershell azure asp.net-web-api

我需要从我在Azure App Service中部署的web api执行一些powershell代码。我无法实现对Set-ExecutionPolicy'因为我需要将其设置为不受限制但我收到错误

  

无法加载文件D:\ home \ powershell \ teams_v2.psm1,因为在此系统上禁用了运行脚本。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=135170

上的about_Execution_Policies

我有以下代码

using (Runspace runspace = RunspaceFactory.CreateRunspace())
                            {
                                runspace.Open();
                                var script = String.Format(@"Import-Module 'D:\home\powershell\teams_v2.psm1'
                                                            connect-teamsservice -user admin@contoso.onmicrosoft.com -tenant contoso.onmicrosoft.com
                                                            new-Team -displayname '{0}' -description '{1}' -smtpaddress '{2}' -alias '{3}' -type 'private'",
                                                        group.Name, group.Description, String.Format("{0}@contoso.onmicrosoft.com", group.MailNickName), "team");
                                RunspaceInvoke scriptInvoker = new RunspaceInvoke();
                                // set powershell execution policy to unrestricted
                                //scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
                                Pipeline pipeline = runspace.CreatePipeline();
                                pipeline.Commands.AddScript(script);

                                // add an extra command to transform the script
                                // output objects into nicely formatted strings

                                // remove this line to get the actual objects
                                // that the script returns. For example, the script

                                // "Get-Process" returns a collection
                                // of System.Diagnostics.Process instances.

                                pipeline.Commands.Add("Out-String");

                                // execute the script

                                Collection <PSObject> results = pipeline.Invoke();

                                // close the runspace

                                runspace.Close();

                                // convert the script result into a single string

                                StringBuilder stringBuilder = new StringBuilder();
                                foreach (PSObject obj in results)
                                {
                                    stringBuilder.AppendLine(obj.ToString());
                                }

                            }

如何才能正确加载ps模块并使用其功能。

0 个答案:

没有答案