开发者

Is there any way to connect asp.net and powershell on windows 2008 server?

I want to execute some powershell commands on a remote windows 2008 machine. Right now i am using psexec to run the powershell from c# asp.net application. I would like to know is there any way to execute powershell com开发者_如何学JAVAmands without using a third party tool like psexec?

Thanks,

dijo


There is, using the

System.Management.Automation

class library and Runspaces. See this MSDN article about creating Runspaces, here's an example from it in C#:

Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

PowerShell powershell = PowerShell.Create();
powershell.Runspace = runspace;
powershell.AddCommand("Get-Process").AddArgument("wmi*");

foreach (PSObject result in powershell.Invoke())
{
    Console.WriteLine("{0,-20}{1}", result.Members["ProcessName"].Value, result.Members["Id"].Value);
}

runspace.Close();

Using WS-Management this can also be done for a remote host.


I created a web application to execute powershell scripts - it might help you - http://psadmin.codeplex.com

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜