0

The original question is quite big, and now I narrow down to:

The following powershsell script works if I run it in powershell console window, and $appPool is Microsoft.IIs.PowerShell.Framework.ConfigurationElement type, and a new application pool can be created in my IIS.

Import-Module WebAdministration
$appPool = New-Item ("IIS:\AppPools\$iissitename")

However if I use C# to run my script, the $appPool will be NULL, and no application pool is created.

Any idea? Thanks heaps!

--------- Update ------------

If I use other Web Server (IIS) Administration Cmdlets like:

Import-Module WebAdministration

Get-Website

My C# project will throw the exception like: Cannot find a provider with the name 'WebAdministration'

------ Update More ------

Some other powershell command like "Get-ChildItem IIS:\apppools" all not working when calling from C#.

2 Answers 2

1

It appears that the WebAdministration module is 64-bit only - at least on the 64-bit system I tested on. Either that or some of the COM components it depends on haven't been registered in the 32-bit registry hive. Make sure to compile your C# app as x64.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi Keith, I did, and it's the same.
It is interesting that if I fire up an x86 PowerShell prompt, I can import webadministration with no errors. But if I try to GCI IIS:\ I get the error: 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)).. It seems like some required COM server isn't registered in the 32-bit hive.
For the error you mention here, maybe could use regsvr32 xxx. dll
Also to register COM dll in Powershell, if running console in normal mode, must have "-Verb runAs"
When I need to use WebAdministration, I just use it from a 64-bit prompt and have no problems. And since our admin disables UAC I'm running as admin, so again no problems due to lack of privileges. :-)
|
1

I think I might find the answer. It seems IIS related command in Powershell needs administrator right to run. If I use C# to call the ps file, the command in ps should like:

Start-Process powershell -Verb runAs -ArgumentList "Invoke-Command -ScriptBlock {Get-WebSite }"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.