Monthly Archives: March 2016

The 100th Post

In less than two years time, I’ve written and published 99 posts:

the-100th-post-01

On this note, welcome to post 100! This is a exciting achievement, especially since I didn’t even like PowerShell much when it was first introduced (as Monad). I really preferred VBS then, and I didn’t even care much that we — Microsoft admins — were getting a shell, too. It didn’t all register then, and realistically, it was probably because I was still somewhat early on in my career.

It feels like I just bought the tommymaynard.com domain name and made the commitment to associate myself with Windows PowerShell. Post 100 is a post I didn’t even know was a possibility. In fact, the number of posts were never a consideration. It’s always been about quality, even in brief, right-to-the-point posts. I’ve tried to elevate this blog, by using simple and clear examples to help teach some of the basic and intermediate concepts. I intentionally add clues and use full descriptions to encourage and assist my readers.

In celebration of post 100, I’ll list my top 10 favorite posts, in no particular order:

1. Quick Learn – Clear-Host, Without Clearing the Host
http://tommymaynard.com/ql-clear-host-without-clearing-the-host

2. Quick Learn – Using OutVariable — Why Don’t I Do that More Often?
http://tommymaynard.com/ql-using-outvariable-why-dont-i-do-that-more-often

3. Script Sharing – Return File Sizes in Bytes, KBs, MBs, and GBs, at the Same Time
http://tommymaynard.com/script-sharing-return-file-sizes-in-bytes-kbs-mbs-and-gbs-at-the-same-time-2015

4. Script Sharing – Two Old HTAs: LoggedOnUser and Remote Desktop Assistant
http://tommymaynard.com/script-sharing-two-old-htas-loggedonuser-and-remote-desktop-assistant-2015

5. Quick Learn – Run Background Commands after Every Command (Part II)
http://tommymaynard.com/quick-learn-run-background-commands-after-every-command-part-ii-2015

6. Quick Learn – Determine if the Alias, or Function Name was Used
http://tommymaynard.com/ql-determine-if-the-alias-or-function-name-was-used

7. Quick Learn – Proving PowerShell’s Usefulness to Newbies, Part III
http://tommymaynard.com/quick-learn-proving-powershells-usefulness-to-newbies-part-iii-2016

8. Quick Learn – Give a Parameter a Default Value
http://tommymaynard.com/quick-learn-give-a-parameter-a-default-value-2015

9. Quick Learn – Save External Dynamic IP to Dropbox
http://tommymaynard.com/quick-learn-save-external-dynamic-ip-to-dropbox-2015

10. Extra – Why is there a tommymaynard.com?
http://tommymaynard.com/extra-why-is-there-a-tommymaynard-com-2016

Keep an Attached Drive Active

I recently received two drives sent via FedEx for a rather large data copy. Maybe I haven’t been looking, but I’ve never seen anything like these drives before, although it wasn’t too difficult to believe they existed. They each hold 6TB and have a 10-key keypad on the drive case where you have to punch in a PIN to unlock the drive. Neat, except that the drives also have a timeout period in which they automatically lock. To unlock each drive, providing there’s been no disk activity, as that’s what starts the timeout counter, is a trip to the data center to enter the PIN again.

keep-an-attached-drive-active01

A coworker and I quickly arrived at the same idea in order to keep the drives active. I bet you just thought of an idea, too. It’s extremely simplistic, but that’s typically the road on which I travel. In the simple Do-While loop below, we created a file on each of the attached drives. In my example, the loop slept for eight minutes after it created the file. Then it removed the files and slept for another 10 seconds before repeating.

Do {
    New-Item -Path E:\keepalivefile.txt,F:\keepalivefile.txt -ItemType File | Out-Null
    Start-Sleep -Seconds 480 # equal to 8 minutes
    Remove-Item -Path E:\keepalivefile.txt,F:\keepalivefile.txt
    Start-Sleep -Seconds 10
}
While ($true)

That’s all I’ve got tonight, other than to say, I have no idea what I would’ve done if I didn’t know Windows PowerShell. Seriously, how would I have been able to guarantee the drives wouldn’t lock, besides to manually create the disk activity? Keeping File Explorer open wasn’t enough, I tried. PowerShell to the rescue. Again.

An Enhanced Active Directory Group Membership Compare

Like everyday, there was a Windows PowerShell question on the Internet today. While I may not have provided everything the original poster was asking for, I rather like what I had accomplished. Being that I have a website that resolves around PowerShell 100% of the time, it seemed like a good little chunk of code to share. I’m going to want it one day, I just know it, and I’m not going to want to rewrite it.

What we’ve done is compared the Active Directory (AD) group memberships of two different users using Get-ADPrincipalGroupMembership and the Compare-Object cmdlet. Stay tuned, this isn’t the standard Compare-Object output you might be used to seeing. I’m doing this much like the original forum question, so l stored the SamAccountName of two AD users, in two different variables.

PS> $FirstUser = 'tommymaynard'
PS> $SecondUser = 'lanceandrews'

With the variables set, we’ve used them as part of our Get-ADPrincipalGroupMembership commands below. In this example, you can see two ways of accomplishing the same thing — useful stuff, really.

PS> $FirstUserGroups = Get-ADPrincipalGroupMembership $FirstUser | Select-Object -ExpandProperty Name
PS> $SecondUserGroups = (Get-ADPrincipalGroupMembership $SecondUser).Name

With this second set of variables assigned, we’ve set up the command below to obtain the differences and store those in a variable we’ve called $Difference. Including the -IncludeEqual parameter has allowed us to be able to see the group memberships that both users share.

PS> $Difference = Compare-Object -ReferenceObject $FirstUserGroups -DifferenceObject $SecondUserGroups -IncludeEqual

Next, we put together some calculated properties from our $Difference variable. If calculated properties are new to you, be sure to do some additional research. They can be used to simply rename properties, but also, like I’ve demonstrated below, they can include conditional logic. You might consider copying and pasting the example somewhere else, so you can better digest what’s happening. We’ve created three calculated properties: the first is renaming the InputObject property to Group. The next two properties write one of two things: either that the user is a member, or that they’re not, by entering two, side-by-side dashes. See the image further below.

PS> $Difference | Select-Object @{N='Group';E={$_.InputObject}},@{N='First User';E={If ($_.SideIndicator -eq '<=' -or $_.SideIndicator -eq '==') {'Member'} Else {'--'}}},@{N='Second User';E={If ($_.SideIndicator -eq '=>' -or $_.SideIndicator -eq '==') {'Member'} Else {'--'}}}

This modification of the standard Compare-Object output reminds us which user is which. The standard output uses arrows. A left arrow indicates the first user is a member and a right arrow indicates the second user is a member. Because we’re using the -IncludeEqual parameter, we can distinguish when both users are a member of the same AD group, and that, without the double equal sign, as the indicator.

compare-active-directory-membership-between-two-users01

Neat. I’ll be back to visit you later, modified Compare-Object command. Thanks for reading, everyone.

How Do I Start to Learn PowerShell?

I’ve been around the Windows PowerShell community awhile, and its various forums, and have noticed a consistent theme: People often ask how and what to use to learn PowerShell. There’s plenty of articles and other content out there — ten years’ worth now — and people still ask. While I began this site to help teach PowerShell, depending on the day, it may not always the best place to start. So, what is?

There’s two things I’ve often recommended to help people learn PowerShell. One is what I would consider to be the de facto standard of all introductory PowerShell reading, Learn Windows PowerShell in a Month of Lunches, Second Edition written by Don Jones and Jeffery Hicks and published by Manning Publications.

I read this book after I had already spent a couple years learning PowerShell. While I appreciated seeing what I missed in my own education, I realized with every page turn that I could’ve learned everything I knew, in a lot less time. All I would’ve had to do was buy the book first, and make a commitment to read it. If you’re new to PowerShell and you suspect that you’re going to be a Windows system administrator in the future, then buy it and read it now, before you end up hating yourself for not doing it sooner. Let me know if I get the cue to tell you, “I told you so,” or not. I hope I don’t.

The other thing I’d recommend are two video series found on Microsoft Virtual Academy. This really helped solidified some concepts. The first one is called Getting Started with PowerShell 3.0 Jump Start. The second series, to be watched after the first, is called Advanced Tools & Scripting with PowerShell. 3.0 Jump Start. While these both focus on PowerShell 3.0, they are both still quite relevant to the current release (PowerShell 5.0, at the time of this writing). I’m not sure which I’d recommend you do first — the book or the videos — as I’m not sure if one sequence would be better than the other. Either way, do both as they ensure a good amount of beneficial exposure.

There you go. These are my two, top recommendations for learning Windows PowerShell. It should be said, that in addition to these two, one of the things I did (and I’ve said it several times now), is ensured I learned at least one new thing about PowerShell every day (no matter how big or small, or what day it was). PowerShell is an important part of the future as a Windows system administrator, whether or not, you believe that right now.

View Current PowerShell.org Q&A Forum Topics

Note: Update added at the bottom on this post on August, 9, 2016. Please read.

Sometimes you don’t always have the time to finish something you’ve started. For me, it was this function. I pounded this out in a quick few minutes, and while I don’t see myself investing in it any further, I didn’t want to forget the function, and thought I would hang on to it somewhere. Well, that’s why it’s here, especially as someone may find it useful, or helpful.

The function, which I called Get-PowerShell.orgForumTopic, runs out to PowerShell.org and grabs the current topics (page one) from the PowerShell Q&A forum (http://powershell.org/wp/forums/forum/windows-powershell-qa). It only returns the Thread name and the URL, because, well, that’s what seemed useful and relevant at the time I wrote it (which was many months ago).

Function Get-PowerShell.orgForumTopic {
    [CmdletBinding()]
    Param ()

    Begin {
    } # End Begin.

    Process {
        (Invoke-WebRequest -Uri 'http://powershell.org/wp/forums/forum/windows-powershell-qa/' |
            Select-Object -ExpandProperty Links |
            Where-Object {$_.outerHTML -like '*http://powershell.org/wp/forums/topic*'} |
            Select-Object @{N='Thread';E={$_.innerHTML}},@{N='Url';E={$_.href}} |
            Select-Object -First 30)[(0..30 |
                ForEach-Object {
                    If (-not($_ % 2)) {
                        $_
                    }
                 }
            )]
    } # End Process.

    End {
    } # End End.
} # End Function: Get-PowerShell.orgForumTopic

Here’s what the results looked like in the ConsoleHost, near in time to when this post was published.

current-powershell.org-qa-forum-topics-01

While I never added any more to this function, I had some ideas: add the thread status, add the “started by user,” add the user that made the last post, add the number of posts per topic, and allow it to run against other PowerShell.org forum topics. It might’ve also been helpful to include additional pages, if requested by the user of the function, such as adding a -Pages parameter (-Pages 4).

Anyway, here it is. Beside being helpful to see the top PowerShell.org Q&A forum posts at a specific point in time, it’s an interesting example of reading from a webpage, of which I had minimal experience. So yeah, I probably learned something by doing this exercise.

If you want to do the same, then start by running the first command, Invoke-WebRequest -Uri ‘http://powershell.org/wp/forums/forum/windows-powershell-qa/’. Then add the pipe and first Select-Object command and run that. Then add the Where-Object command, and so on. This will allow you to see how I finally got to only returning the “Threads” and “Urls.” Take care.

Update: Since a site redesign at PowerShell.org, this function, no longer functions. I’m not sure that I’ll bother to update it — I don’t get the feeling that it was ever used by anyone — but I’ll keep this post up for anything else it may offer, that may be helpful in learning PowerShell.