0

C# 言語を使用して、リモート マシンでいくつかの 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();

最初の実行後に上記のコマンドの実行をスキップし、プログラムが終了するまでセッションを維持する必要があります。確立された同じセッションでさらにスクリプトを実行するのを手伝ってください。

正確には、プログラム全体で一度だけセッションを作成したい.Thanks

4

2 に答える 2

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 オブジェクトをクラス内の静的変数として作成します。

于 2013-06-04T18:26:44.900 に答える