从远程 Powershell 调用获取函数值
我远程调用 Powershell 脚本 (.ps1),该脚本从第 3 方程序集(Rebex SFTP 组件)调用函数。该函数返回一个整数值。此远程调用是通过 C# 代码完成的。
我想将该调用的结果转换为 int 以便在 C# 代码中进行进一步处理。
如何才能最有效地做到这一点?
以下是一些代码:
RemoteIncationManager 的代码片段(使用 Powershell Remoting 的自定义类,只是重要部分):
using (Pipeline pipeline = remoteRunspace.CreatePipeline(scriptText))
{
Collection<PSObject> results = pipeline.Invoke();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
}
调用 RemoveInocationManager 的代码片段:
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()
I invoke a Powershell script(.ps1) remotely which calls a function from a 3rd party assembly (Rebex SFTP component). This function returns an integer value. This remote call is done from C# code.
I want to cast the result of that call to int for further processing in the C# code.
How can this be done most effeciently ?
Here is some code:
Code snippet of the RemoteInvocationManager (custom class with Powershell Remoting, just the important parts):
using (Pipeline pipeline = remoteRunspace.CreatePipeline(scriptText))
{
Collection<PSObject> results = pipeline.Invoke();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
}
Code snippet of the call to the RemoveInocationManager:
string command = @"& c:\temp\sftp\mytransfer.ps1";
string result = RemoteInvocationManager.RunScript(command);
Code snippet of the Powershell script (.ps1 file):
[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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来像你想要的:
更新:
要抑制你不需要的东西的输出,请使用
[void]
或管道到Out-Null
或
Looks like you want:
Update:
To suppress out put for things you don't need use
[void]
or pipe toOut-Null
or