1

サードパーティのアセンブリ (Rebex SFTP コンポーネント) から関数を呼び出す Powershell スクリプト (.ps1) をリモートで呼び出します。この関数は整数値を返します。このリモート呼び出しは、C# コードから実行されます。

その呼び出しの結果を int にキャストして、C# コードでさらに処理したいと考えています。

これを最も効率的に行うにはどうすればよいでしょうか。

ここにいくつかのコードがあります:

RemoteInvocationManager のコード スニペット (Powershell Remoting を使用したカスタム クラス、重要な部分のみ):

using (Pipeline pipeline = remoteRunspace.CreatePipeline(scriptText))
{
Collection<PSObject> results = pipeline.Invoke();

       foreach (PSObject obj in results)
       {
         stringBuilder.AppendLine(obj.ToString());
       }
}

RemoveInotationManager への呼び出しのコード スニペット:

string command = @"& c:\temp\sftp\mytransfer.ps1";
string result = RemoteInvocationManager.RunScript(command);

Powershell スクリプト (.ps1 ファイル) のコード スニペット:

[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\Rebex\SFTP for .NET 2.0 Trial\bin\Rebex.Net.Sftp.dll")
$sftp = New-Object Rebex.Net.Sftp
$sftp.Connect("127.0.0.1")
$SshPrivateKey = New-Object Rebex.Net.SshPrivateKey("c:\temp\sftp\keys\private\myprivatekey.ppk", "myuser")
$sftp.Login("myuser", $SshPrivateKey)
$sftp.PutFile("c:\temp\sftp\input\file1.txt", "/output/fileout.txt")
$sftp.Disconnect()
4

1 に答える 1

0

あなたが望むように見えます:

int output;
bool isSuccess = int.TryParse(result, out output);
if(isSuccess){
//use outout
}

アップデート:

[void]使用またはパイプする必要のないものの出力を抑制するにはOut-Null

[void][Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\Rebex\SFTP for .NET 2.0 Trial\bin\Rebex.Net.Sftp.dll")

また

[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\Rebex\SFTP for .NET 2.0 Trial\bin\Rebex.Net.Sftp.dll") | Out-Null
于 2011-12-07T21:30:33.917 に答える