Create IIS Application from c#

0

I need to create an application on IIS server from my .NET WinForm application.

Import-Module WebAdministration
New-Item "IIS:\Sites\Default Web Site\Test" -type Application
Set-ItemProperty "IIS:\Sites\Default Web Site\Test" -Name applicationPool -Value "DefaultAppPool"
Set-ItemProperty "IIS:\Sites\Default Web Site\Test" -Name physicalPath -Value "D:\Temp\MyWebApp"

When I run this script in powershell console the application is created.

When I run this script in application by using PowerShell object in https://www.nuget.org/packages/System.Management.Automation.dll I get this error:

Cannot retrieve the dynamic parameters for the cmdlet. Retrieving the COM class factory for component with CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

this error will return three times.

My C# code is:

string script = @"Import-Module WebAdministration
New-Item ""IIS:\Sites\Default Web Site\Test"" -type Application
Set-ItemProperty ""IIS:\Sites\Default Web Site\Test"" -Name applicationPool -Value ""DefaultAppPool""
Set-ItemProperty ""IIS:\Sites\Default Web Site\Test"" -Name physicalPath -Value ""D:\Temp\_PBWI\Destination""";


using (PowerShell powerShellInstance = PowerShell.Create())
{
    powerShellInstance.AddScript(script);

    Collection<PSObject> psOutput = powerShellInstance.Invoke();

    if (powerShellInstance.Streams.Error.Count > 0)
    {
        foreach (var e in powerShellInstance.Streams.Error.Select(a => a.Exception).ToArray())
        {
            Console.WriteLine(e);
        }
    }
}

SOLVED: The problem was that the application was running 32-bit powershell instead of 64-bit

c#
powershell
iis
asked on Stack Overflow Nov 23, 2017 by Davecz • edited Nov 24, 2017 by Davecz

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0