简体   繁体   English

如果使用C#调用Powershell,则New-Item无效

[英]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. 如果我在powershell控制台窗口中运行以下powershsell脚本,$ appPool是Microsoft.IIs.PowerShell.Framework.ConfigurationElement类型,并且可以在我的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. 但是,如果我使用C#运行我的脚本,$ appPool将为NULL,并且不会创建任何应用程序池。

Any idea? 任何想法? Thanks heaps! 谢谢堆!

--------- Update ------------ --------- 更新 ------------

If I use other Web Server (IIS) Administration Cmdlets like: 如果我使用其他Web服务器(IIS)管理Cmdlet,如:

Import-Module WebAdministration

Get-Website

My C# project will throw the exception like: Cannot find a provider with the name 'WebAdministration' 我的C#项目将抛出异常,例如:找不到名为“WebAdministration”的提供程序

------ Update More ------ ------ 更新更多 ------

Some other powershell command like "Get-ChildItem IIS:\\apppools" all not working when calling from C#. 从C#调用时,其他一些名为“Get-ChildItem IIS:\\ apppools”的powershell命令都无效。

It appears that the WebAdministration module is 64-bit only - at least on the 64-bit system I tested on. WebAdministration模块似乎只是64位 - 至少在我测试的64位系统上。 Either that or some of the COM components it depends on haven't been registered in the 32-bit registry hive. 它所依赖的那个或一些COM组件尚未在32位注册表配置单元中注册。 Make sure to compile your C# app as x64. 确保将您的C#应用​​程序编译为x64。

I think I might find the answer. 我想我可能会找到答案。 It seems IIS related command in Powershell needs administrator right to run. 看起来Powershell中的IIS相关命令需要管理员才能运行。 If I use C# to call the ps file, the command in ps should like: 如果我使用C#来调用ps文件,ps中的命令应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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