2013-06-03 60 views
0

我想用C#language.It远程机器上执行某些Active Directory查询是查询如何在整个程序中维护PowerShell远程会话?

PowerShell ps = PowerShell.Create();; 

ps.AddScript(@"$Session = New-PSsession -Computername com1"); 
ps.AddScript(@"Invoke-Command -Command {Import-Module ActiveDirectory} -Session $Session"); 
ps.Invoke(); 

我想跳过第一次执行后上面的命令执行,并应保持会话直到程序退出。请帮助我在建立相同会话时执行更多脚本。

在确切的我想只有在整个program.Thanks

回答

0

一旦创建会话最后我得到了我的问题..its很简单的一个解决方案。

将Powershell对象设置为静态变量,并在脚本调用函数后清除该命令。

ps.commands.clear(); 

现在不影响我们可以执行查询当前会话easily..Let我知道如果任何其他有效的方法吧。

谢谢。

0
int iRemotePort = 5985; 
string strShellURI = @"http://schemas.microsoft.com/powershell/Microsoft.PowerShell"; 
string strAppName = @"/wsman"; 
AuthenticationMechanism auth = AuthenticationMechanism.Negotiate; 

WSManConnectionInfo ci = new WSManConnectionInfo(
    false, 
    sRemote, 
    iRemotePort, 
    strAppName, 
    strShellURI, 
    creds); 
ci.AuthenticationMechanism = auth; 

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

PowerShell psh = PowerShell.Create(); 
psh.Runspace = runspace; 

将Powershell对象设置为类中的静态变量。

相关问题