Powershell:从C#webapplication

时间:2015-07-27 10:15:31

标签: command-line powershell c#

我有一个托管在IIS 7.5上的C#Web应用程序,

运行PowerShell脚本来创建apppool和网站。

如果我从命令提示符运行powershell脚本它可以工作,但是当我通过C#application' Set-ItemProperty'不管用。它也没有给出错误。

我无法找到确切的问题, 请帮忙

PowerShell脚本

Import-Module WebAdministration -Force;
$siteName=$args[0];
$AppPoolName=$args[1];
$PortNumber=$args[2];
$PhysicalPath=$args[3];



New-Item IIS:\AppPools\$AppPoolName
Set-ItemProperty IIS:\AppPools\$AppPoolName managedRuntimeVersion v4.0

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

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");
            runSpaceInvoker.Invoke("Import-Module WebAdministration");

            Pipeline pipeline = runspace.CreatePipeline();
            var appPoolName = new CommandParameter(null, "test");
            var siteName = new CommandParameter(null, "test");
            var scriptPath = ConfigurationManager.AppSettings["PowershellSscriptRootPath"];
            var websiteRootFolder = ConfigurationManager.AppSettings["PhysicalPathRoot"];
            var websitePath = "C://inetpub//wwwroot//test";

            #region CreateWebsite
            string scriptPathForCreateWebsiteFromConfiguration = scriptPath + ConfigurationManager.AppSettings["CreateAppPoolScriptName"];
            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)); 

0 个答案:

没有答案