{"id":75421,"date":"2015-11-27T11:59:00","date_gmt":"2015-11-27T11:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/11\/27\/powertip-get-the-async-object-created-by-begininvoke\/"},"modified":"2019-02-18T09:20:54","modified_gmt":"2019-02-18T16:20:54","slug":"powertip-get-the-async-object-created-by-begininvoke","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-get-the-async-object-created-by-begininvoke\/","title":{"rendered":"PowerTip: Get the Async Object Created by BeginInvoke"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Boe Prox shows how retrieve a seemingly lost Async object from a runspace.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;How can I get the seemingly lost Async object created by&nbsp;<b>BeginInvoke<\/b>?<\/p>\n<p style=\"margin-left:30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Use&nbsp;<b>Reflection<\/b>&nbsp;to pull this information out of a&nbsp;<b>Runspace<\/b>&nbsp;object and get the <strong>Async<\/strong> object, which is required <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;to use with&nbsp;<b>EndInvoke()<\/b>.<\/p>\n<p style=\"margin-left:120px\">$Runspace = [runspacefactory]::CreateRunspace()<\/p>\n<p style=\"margin-left:120px\">$PowerShell = [powershell]::Create()<\/p>\n<p style=\"margin-left:120px\">$Runspace.Open()<\/p>\n<p style=\"margin-left:120px\">$PowerShell.Runspace = $Runspace<\/p>\n<p style=\"margin-left:120px\">[void]$PowerShell.AddScript({<\/p>\n<p style=\"margin-left:120px\">&nbsp;&nbsp;&nbsp; [pscustomobject]@{<\/p>\n<p style=\"margin-left:120px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name = &#8216;Boe Prox&#8217;<\/p>\n<p style=\"margin-left:120px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PowerShell = $True<\/p>\n<p style=\"margin-left:120px\">&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin-left:120px\">})<\/p>\n<p style=\"margin-left:120px\">#Intentionally forget to save this<\/p>\n<p style=\"margin-left:120px\">$PowerShell.BeginInvoke()<\/p>\n<p style=\"margin-left:120px\">#Time to retrieve our missing object<\/p>\n<p style=\"margin-left:120px\">$BindingFlags = [Reflection.BindingFlags]&#8217;nonpublic&#8217;,&#8217;instance&#8217;<\/p>\n<p style=\"margin-left:120px\">$Field = $PowerShell.GetType().GetField(&#8216;invokeAsyncResult&#8217;,$BindingFlags)<\/p>\n<p style=\"margin-left:120px\">$AsyncObject = $Field.GetValue($PowerShell)<\/p>\n<p style=\"margin-left:120px\">#Now end the runspace<\/p>\n<p style=\"margin-left:120px\">$PowerShell.EndInvoke($AsyncObject)<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Boe Prox shows how retrieve a seemingly lost Async object from a runspace. &nbsp;How can I get the seemingly lost Async object created by&nbsp;BeginInvoke? &nbsp;Use&nbsp;Reflection&nbsp;to pull this information out of a&nbsp;Runspace&nbsp;object and get the Async object, which is required &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;to use with&nbsp;EndInvoke(). $Runspace = [runspacefactory]::CreateRunspace() $PowerShell = [powershell]::Create() $Runspace.Open() [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[162,356,650,3,45],"class_list":["post-75421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-boe-prox","tag-powertip","tag-runspace","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Boe Prox shows how retrieve a seemingly lost Async object from a runspace. &nbsp;How can I get the seemingly lost Async object created by&nbsp;BeginInvoke? &nbsp;Use&nbsp;Reflection&nbsp;to pull this information out of a&nbsp;Runspace&nbsp;object and get the Async object, which is required &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;to use with&nbsp;EndInvoke(). $Runspace = [runspacefactory]::CreateRunspace() $PowerShell = [powershell]::Create() $Runspace.Open() [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=75421"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75421\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=75421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=75421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=75421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}