从C#程序运行powershell脚本的行为与直接运行时不同

从C#程序运行powershell脚本的行为与直接运行时不同,c#,powershell,iis,C#,Powershell,Iis,我已经为创建网站编写了powershell脚本 当我从powershell命令提示符下运行脚本时,它运行得非常完美,但当我通过C#程序运行脚本时,有时它无法工作,有时它可以工作 创建apppoool的脚本也是如此。我提到过为版本4.0创建apppool,但通过progam它创建了版本2.0的apppool 我已经在IIS 7.5中托管了我的C#web应用程序……我认为这可能是IIS安全性的问题 脚本: Import-Module WebAdministration -Force; $siteN

我已经为创建网站编写了powershell脚本

当我从powershell命令提示符下运行脚本时,它运行得非常完美,但当我通过C#程序运行脚本时,有时它无法工作,有时它可以工作

创建apppoool的脚本也是如此。我提到过为版本4.0创建apppool,但通过progam它创建了版本2.0的apppool

我已经在IIS 7.5中托管了我的C#web应用程序……我认为这可能是IIS安全性的问题

脚本:

Import-Module WebAdministration -Force;

$siteName=$args[0];
$AppPoolName=$args[1];
$PortNumber=$args[2];
$PhysicalPath=$args[3];



New-Item IIS:\Sites\$siteName -physicalPath $PhysicalPath$siteName -bindings @{protocol="http";bindingInformation=":"+$PortNumber+":"}
Set-ItemProperty IIS:\Sites\$siteName -Name applicationPool -Value $AppPoolName
C#代码

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
            Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
            runspace.Open();
            var runSpaceInvoker = new RunspaceInvoke(runspace);
            runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope Process -Force");
            Pipeline pipeline = runspace.CreatePipeline();
            var appPoolName = new CommandParameter(null, command.SiteName);
            var siteName = new CommandParameter(null, command.SiteName);
            var scriptPath = ConfigurationManager.AppSettings["PowershellSscriptRootPath"];
            var websiteRootFolder = ConfigurationManager.AppSettings["PhysicalPathRoot"];
            var websitePath = websiteRootFolder + "\\" + command.SiteName + "\\";

            #region CreateWebsite

            string scriptPathForCreateWebsiteFromConfiguration = scriptPath + ConfigurationManager.AppSettings["CreateWebsiteScriptName"];
            var createWebsiteCommand = new Command(scriptPathForCreateWebsiteFromConfiguration);
            createWebsiteCommand.Parameters.Add(siteName);
            createWebsiteCommand.Parameters.Add(appPoolName);
            createWebsiteCommand.Parameters.Add(new CommandParameter(null, "80"));
            createWebsiteCommand.Parameters.Add(new CommandParameter(null, websiteRootFolder)); 
            pipeline.Commands.Add(createWebsiteCommand);

            #endregion
            pipeline.Invoke();
            runspace.Close();

谢谢

如果您要绑定到新站点的端口已经在使用,那么它将不会创建新站点

请提供具体的不工作的详细信息,例如,您是否可以在此处共享任何错误消息?检查运行空间是否不是PS2.0。也许可以尝试。没有任何错误,但它不会创建网站。但有时它是有效的!奇怪:(谁对这样的问题投了赞成票?)??