简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM