NorCal PowerShell User Group Presentation on Runspaces is Available

I had the honor of being a speaker at the NorCal PowerShell User Group on June 10th in which I spent the time talking about using runspaces with PowerShell. If you didn’t have a chance to attend, the good news is that the video is now live on YouTube! I had a blast talking to the group about how you can utilize runspaces (and runspacepools) in a variety of situations as well as touching on a number of other related topics to include:

  • Runspaces
  • RunspacePools
  • Synchronized Collections
  • Locking a Synchronized Collection between runspaces
  • WPF UIs with runspaces
  • Performance and Speed with Runspaces and PSJobs

The presentation materials and example code is available here: http://1drv.ms/1imuQd0

Check out the video below!

Let me know what you thought about the presentation and anything else that you would have liked to see!

Posted in News, powershell | Tagged , , , , | 2 Comments

Prevent Write-Debug From Bugging You

If you are like me, you try to take advantage of already available methods of displaying output that displays information about a running function or script or  some other internal information by using Write-Verbose/Debug. The best thing about this is that it is simple to decide when to use it by specifying the various Write* commands anywhere in the code and as long as you have [cmdletbinding()] with Param() in the beginning of your code, all you have to do is use the appropriate switch (-Verbose or –Debug) and you can have that information out there to review.

The only problem that I would run into is the use of –Debug and those annoying confirmation prompts that always occur. You never see this with –Verbose and for that reason, I typically use that  so I do not get bombarded with a confirmation prompt each and every time I use it (that’s changed for me now and I will show you how it works later on). Take this example:

Function Foo {
    [cmdletbinding()]
    Param (
        [int]$Value
    )
    $iteration = 0
    1..5 | ForEach {
        $iteration++
        Write-Debug "Iteration: $iteration"
        $return = Get-Random -InputObject (1..$Value)
        Write-Debug "Return: $Return"
    }
}

This is a very simple function meant only to highlight the issue that I have been talking about. As soon as I run this function with the –Debug statement, I will see a confirmation prompt for each and every Write-Debug that exists.

image

Ok, once was enough, but this is out of hand. Even specifying “A” to continue with All operations seems not to work (there may be a logical explanation for this). Can you imagine having to do this for each and every Write-Debug call as well as accounting for each iteration that occurs? So why is this happening? Lets take a look and see what happens here.

Function Foo {
    [cmdletbinding()]
    Param (
        [int]$Value
    )
   
    $PSBoundParameters.GetEnumerator() | ForEach {
        Write-Verbose $_
    }
    Write-Verbose "DebugPreference: $DebugPreference"
 
    $iteration = 0
    1..5 | ForEach {
        $iteration++
        Write-Debug "Iteration: $iteration"
        $return = Get-Random -InputObject (1..$Value)
        Write-Debug "Return: $Return"
    }
}

I’ve added a couple of extra lines of code here to help see what happens when I run the function with the –Debug parameter. Viewing the $PSBoundParameters and $DebugPreference automatic variable will show us what is happening here.

image

I ran it first without the –Debug parameter and you can see that $DebugPreference is set to SilentlyContinue. The second run with the –Debug parameter shows a different story. We can tell in the $PSBoundParameters that it the parameter is being used, not a big thing, but looking at $DebugPreference now shows it has been set to ‘Inquire’, not the ‘Continue’ that I would have expected. Because of this, we will always receive a prompt for confirmation whenever –Debug is used.

Knowing this, I can now use a bit of code to make sure that when –Debug is used, I will not get a prompt each time it is used.

If ($PSBoundParameters['Debug']) {
    $DebugPreference = 'Continue'
}

I have the above code used at the beginning of my function so it will set the script scope of $DebugPreference to ‘Continue’ which in turn will make sure that I will not have a confirmation prompt. Let’s give it a spin and see what happens.

image

As you can see, I had 0 confirmation prompts and the script continued on showing my Debug stream. So with that, you now have an alternative to dealing with the Debug confirmation prompt the next time you decide to use Write-Debug in your code!

Posted in Debug, powershell | Tagged , , | 3 Comments

Speaking at NorCal PowerShell User Group in June

I’ll be speaking at the NorCal PowerShell User Group on June 10th talking PowerShell runspaces. I am honored to have been asked to speak to the group and look forward to chatting about runspaces and showing off some examples of what you can do with them as well as showing off some of the techniques that I use. Be sure to check it out if you have time! It will be recorded so if you do miss it, be sure to check back here for the YouTube link.

Sign up here for the meet up.

http://www.meetup.com/Northern-California-Powershell-User-Group/events/183848522/

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

Video From My Presentation at the Philadelphia PowerShell Users Group on WSUS and PowerShell

The video has been posted to YouTube from my presentation on May 1st at the Philadelphia PowerShell Users Group. My presentation was on Managing WSUS using PowerShell and I had a great time presenting on the subject. Check it out and let me know what you think!

Posted in News, powershell, WSUS | Tagged , , , , | 1 Comment

Quick Hits: List All Available WMI Namespaces Using PowerShell

Sometimes it is worth knowing what the available namespaces are in WMI, especially if you are looking for a specific class and are not sure where it might be (sometimes it just isn’t under Root\Cimv2). Another reason is just for the sake of exploring the WMI repository to see what exactly is out there.

There are a couple of approaches to this: one using Get-WMIObject and another using Get-CIMInstance. Both ways gets you the same result as shown below:

Get-WMIObject

Get-WmiObject -Namespace Root -Class __Namespace | 
Select-Object -Property Name

image

Get-CimInstance

Get-CimInstance -Namespace Root -ClassName __Namespace

image

Note that there are some nested Namespaces below the top level ones that I showed above. We can find these by using the following function I wrote called Get-WMINamespace.

Get-WMINamespace -Recurse

image

This function also allows you to query remote systems and allows for alternate credentials. By default, it will only show the top level namespaces and if you want to see all of the available nested namespaces, you must use the –Recurse parameter.

Get-WMINamespace -Recurse -Computername DC1 `
-Credential 'rivendell\proxb'

image

That is all to exploring all of the available WMI namespaces on a local or remote system.

You can download the function below. Note it requires PowerShell 3.0 at a minimum.

Download Get-WMINamespace

Posted in powershell | Tagged , , , , | 2 Comments