我想使用c# language.It在远程计算机上执行一些Active directory查询
PowerShell ps = PowerShell.Create();;
ps.AddScript(@"$Session = New-PSsession -Computername com1");
ps.AddScript(@"Invoke-Command -Command {Import-Module ActiveDirectory} -Session $Session");
ps.Invoke();我想在第一次执行后跳过上面的命令执行,并且应该保持会话,直到程序exit.Please帮助我执行更多的脚本,并建立相同的会话。
确切地说,我只想在整个program.Thanks中创建一次会话
发布于 2013-06-03 18:47:47
最后,我得到了我的问题的解决方案,非常简单的..its。
将Powershell对象设置为静态变量,并在脚本调用函数后清除命令。
ps.commands.clear();现在,在不影响当前会话的情况下,我们可以执行查询easily..Let me know是否有其他有效的方法。
谢谢。
发布于 2013-06-05 02:26:45
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对象作为类中的静态变量。
https://stackoverflow.com/questions/16892353
复制相似问题