Finding the PowerShellAsyncResult on PowerShell Object After Using BeginInvoke

I’ve been here many of times when working with the console and creating a PowerShell instance and runspace to kick off a runspace in the background. I have everything situated and then call BeginInvoke() against my PowerShell object and realize my mistake when I see the PowerShellAsyncResult being displayed proudly in my console.

$Runspace = [runspacefactory]::CreateRunspace()
$PowerShell = [powershell]::Create()
$Runspace.Open()
$PowerShell.Runspace = $Runspace
[void]$PowerShell.AddScript({
    #Imagine that this was some crazy long operation
    [pscustomobject]@{
        Name = 'Boe Prox'
        PowerShell = $True
    }
})
$PowerShell.BeginInvoke()

SNAGHTML4ec3557

If you’ve worked with this before of done something where you had to use BeginInvoke() and not captured the resulting System.Management.Automation.PowerShellAsyncResult output, you know the pain in having to close out the console and re-running this and making sure to capture the output.

I had mentioned this in my talk on PowerShell runspaces that if you don’t capture this output, then it is the end of the line for your command running in the background.

It is painful. It is frustrating. And it is not the end of the world! There is actually hope for you if this happens and you do not want to kill whatever you are doing to restart the command in the runspace.

The trick lies in using Reflection to look into the PowerShell object for the invokeAsyncResult field and pulling the object out so you can use it later on with EndInvoke().

First we need to get the proper binding flags so we can look for our field. In this case, I only need the nonpublic and instance flags. I already know the name of the field: invokeAsyncResult from performing a all out look at the fields on my object.

Armed with this, I can now pull the field from the object.

$BindingFlags = [Reflection.BindingFlags]'nonpublic','instance'
$Field = $PowerShell.GetType().GetField('invokeAsyncResult',$BindingFlags)

image

Now all I have to do is get the value of this field. In order to do that, I use the GetValue() method on the field and give it the PowerShell object as the required object in the parameter.

$Handle = $Field.GetValue($PowerShell)

SNAGHTML4f785f2

We now have our PowerShellAsyncResult  object back and can now use it to properly end the command using EndInvoke().

image

Perfect! Now we have a way to save ourselves if we happen to call BeginInvoke() on our PowerShell object and happen to forget to save the output object to use later on!

Posted in powershell | Tagged , , , | 2 Comments

Video and Materials Available From my Talk on PowerShell Runspaces With Mississippi PowerShell User Group

The video from my presentation to the Mississippi PowerShell User Group is now live on YouTube for your viewing pleasure.

https://www.youtube.com/watch?v=qyTm-bQrkLY

The slides and demo code that I used for this presentation is also available at the link below.

https://onedrive.live.com/redir?resid=607A450CAC265331%213960

I had a great time presenting on a topic that is fun to talk about. I hope to follow up on a couple of questions in the future with a blog article answer to answer them. Please let me know if you have any questions regarding what I talked about!

Posted in powershell | Tagged , , , | 2 Comments

Speaking at Mississippi PowerShell User Group on Runspaces

MSPSUG Virtual Meeting: The Art of PowerShell Runspaces

A week from tonight (8 Sept, 2015) you can find me online speaking at the Mississippi PowerShell User Group talking about The Art of PowerShell Runspaces. This is going to be a great talk and I will be covering a bunch of things on runspaces ranging from stepping into your first runspace build to throttling with runspacepools as well as using runspaces in your UIs. Near the end I will provide a demo of my module: PoshRSJob provides a PSJobs like approach to using runspacepools and runspaces.

And anything else that I can think of!

You can attend the user group by signing up at this link: https://www.eventbrite.com/e/mspsug-virtual-meeting-the-art-of-powershell-runspaces-tickets-18292593640

If you cannot attend, have no fear as this will be recorded and available for viewing on YouTube later on.

Hope to see you there!

Posted in powershell | Tagged , , , | Leave a comment

Quick Hits: Listing All Permanent WMI Event Subscriptions

This is a quick script that I put together a while back to help locate and track what systems had WMI event subscriptions configured.

I don’t want to completely rehash this topic as you can look at my previous blog post to catch up on all things permanent WMI subscriptions right here: https://learn-powershell.net/2013/08/14/powershell-and-events-permanent-wmi-event-subscriptions/ Feel free to give that a read and then head back over here to check out the script!

The basics of this script are performing WMI queries against the __EventFilter, __EventConsumer and __EventBinding WMI classes which are commonly located in the root/Subscription namespace.

Keep in mind that while I am using the root/Subscription namespace, these subscriptions can exist in other namespaces.

From there it is simply a matter of outputting everything as a single object which you can then dig deeper into by checking the Filter, Consumer and Binding properties.

You can download the script below and once you do, go ahead and dot source the script to load the Get-WMISubsciptionEvent function.

Get-WMIEventSubscription

image

Feel free to give it a download and let me know what you think!

Script Download

https://gallery.technet.microsoft.com/scriptcenter/List-all-WMI-Permanent-73e04ab4

Posted in powershell | Tagged , , , | Leave a comment

Guest Spot on Hey, Scripting Guy Talking PowerShell and USN Change Journal

Starting today and ending Wednesday, I will be guest blogging on the Hey, Scripting Guy! blog talking about working with the USN Change Journal using PowerShell. Be sure to check it out and leave your comments with any questions!

Connect to USN Change Journal

View Entries in USN Change Journal

Use PoshUSNJournal Module to Work with Change Journal

Posted in powershell | Tagged , , , , | Leave a comment