c#.net remote powershell for iis
本文关键字:for iis powershell remote net | 更新日期: 2023-09-27 18:16:53
你好,我有以下代码位
//set up exchange server name
var Exchangesrv = "ex1.iah.ac.uk";
// Initialize PowerShell engine
var shell = PowerShell.Create();
// Add the script to the PowerShell object
shell.Commands.AddScript("Invoke-Command -Computername hostname.example.com -ScriptBlock{get-alias}");
// Execute the script
var results = shell.Invoke();
Input.DataSource = results;
Input.DataBind();
很简单,我想调用一个power shell命令input是我要填充的一个列表框对象
但是当我在IIS服务器上运行它时,它不起作用,我认为这是因为IIS应用程序没有使用远程服务器上的管理员帐户运行,我试图运行该命令。有人能帮助解决这个问题吗?如何让网页使用正确的凭据调用此invoke命令?
编辑
所以我决定在web服务器上创建一个配置文件,这意味着当我使用的帐户打开电源外壳时,交换模块被加载。但是当使用以下代码
using (var powershell = PowerShell.Create())
{
//powershell.Runspace = runspace;
// Add the script to the PowerShell object
//powershell.AddScript("Get-alias");
powershell.AddScript("Get-DynamicDistributionGroup");
// Execute the script
var results = powershell.Invoke();
Input.DataSource = results;
Input.DataBind();
所以应该发生的是power shell在web服务器上本地运行,但我需要它来加载配置文件,但是我看不出为什么它不这样做。是否有命令强制它加载模块。
应用程序池的属性为"Load User Profile"。必须设置为True
我不能告诉任何人这是为什么,但它解决了我的访问被拒绝的问题。
关于此属性的更多信息:https://blogs.msdn.microsoft.com/vijaysk/2009/03/08/iis-7-tip-3-you-can-now-load-the-user-profile-of-the-application-pool-identity/
最后,模拟和调用没有达到目的(我本可以使用invoke,但我不希望在代码中出现凭据)
我发现我必须改变应用程序池的属性,然后它工作得很好。