Powershell: Set-ItemProperty not working when scri

2019-07-10 21:14发布

I have a C# web application which hosted on IIS 7.5,

which runs powershell script for creating apppool and website.

if I run the powershell script from command prompt it working but when i run it through C# application 'Set-ItemProperty' is not working. its not giving error too.

I couldn't find exact problem, pls help

PowerShell Script

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# code

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条回答
登录 后发表回答