我有2个powershell脚本,我从c#执行,我用它来首先使用IMAP cmdlet列出消息,第二个脚本执行特定消息的视图。两个都是从powershell成功执行的,第二个我试图检索一个附件,我看到一堆数据输出到控制台就像这样...
Sent from my iPhone
------=_NextPart_000_0027_01CCDAA7.399EBE00
Content-Type: image/jpeg; name="photo.JPG"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="photo.JPG"
/9j/4QH6RXhpZgAATU0AKgAAAAgACgEPAAIAAAAGAAAAhgEQAAIAAAAHAAAAjAESAAMAAAABAAYA
AAEaAAUAAAABAAAAlAEbAAUAAAABAAAAnAEoAAMAAAABAAIAAAEyAAIAAAAUAAAApAITAAMAAAAB
AAEAAIdpAAQAAAABAAAAuIglAAQAAAABAAABZgAAAABBcHBsZQBpUGhvbmUAAAAAAEgAAAABAAAA
SAAAAAEyMDA5OjA5OjIwIDE1OjEwOjU1AAAKgp0ABQAAAAEAAAE2kAAABwAAAAQwMjIxkAMAAgAA
ABQAAAE+kAQAAgAAABQAAAFSkQEABwAAAAQBAgMAoAAABwAAAAQwMTAwoAEAAwAAAAEAAQAAoAIA
BAAAAAEAAAZAoAMABAAAAAEAAASwpAYAAwAAAAEAAAAAAAAAAAAAAA4AAAAFMjAwOTowOToyMCAx
所以到目前为止看起来一切都很好 - 除了我认为函数结果的差异尽可能来自powershell的流/管道?
第一个返回一个PSObjects的集合,就像这样调用
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddScript(script);
ps.Invoke();
foreach (PSObject result in ps.Invoke())
{
dynamic val = result.BaseObject;
}
第二个脚本使用Invoke()执行时没有错误,但尝试通过每个或
检索数据dynamic xx = ps2.Invoke();
xx为空;
我想也许我需要使用BeginInvoke并异步调用所以我试过
static dynamic GotMail(dynamic o)
{
return o;
}
delegate dynamic SomeDelegate(dynamic o);
and attempted to use like so..
SomeDelegate sd = GotMail;
IAsyncResult ar = ps2.BeginInvoke();
dynamic val2 = sd.EndInvoke(ar);
我收到“异步结果对象为null或意外类型。”我甚至没有将参数传递给powershell脚本..所有内容都是硬编码的 - 我从PowerShell运行的脚本成功正是我从.NET执行的
对此有任何建议。
由于
答案 0 :(得分:1)
您通过两次调用Invoke()来调用脚本两次。删除第一个要调用的调用,例如:
PowerShell ps = PowerShell.Create(); ps.Runspace = runspace; ps.AddScript(script); //ps.Invoke(); foreach (PSObject result in ps.Invoke()) { dynamic val = result.BaseObject; }