Samuel
Samuel

Reputation: 651

New-Item not working if use C# to call Powershell

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

Upvotes: 0

Views: 901

Answers (2)

Samuel
Samuel

Reputation: 651

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 }"

Upvotes: 1

Keith Hill
Keith Hill

Reputation: 201632

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.

Upvotes: 1

Related Questions