It’s all about the “Right Tool for the Job” – #PowerShell FTW

I say these (somewhat-cheesy) phrases frequently:

  • “Right tool, right job”
  • “Right tool for the job”
  • “Golf club mentality”
  • “Sometimes you have to choose between a 3-iron and a 9-iron..”

Here’s an example I encountered this week. . .

Our Remote Application Delivery team wanted to capture users who were launching applications from a specific farm. They knew the information was logged in the event log under a specific application log and instance ID, so submitted a request to our SCOM team to enable a custom rule to capture the data so that they could generate reports. In testing the monitor, they found there were 90K instances captured for this event in less than a week!  This is a significant amount of data to capture in SCOM, especially since the data only needed to be captured for the next 1-2 months.

Enter PowerShell.

This simple code queries each server in the array for Event ID 184 in the “MyApp Secure Gateway” log, and exports the data to a .csv.

$computers = @('Server1005','Server1007',
    'Server1009','Server1011','Server1006',
    'Server1008','Server1010','Server1012')
$a = Get-EventLog -LogName "MyApp Secure Gateway" `
    -ComputerName $computers -InstanceId 184
$a | export-csv c:\logs\myApp.csv -NoTypeInformation

As you can see from my example, if the admin had been aware of the power of PowerShell (as well as proper rights to the servers in question), he could quickly pull the data by himself, and even used PowerShell to further manipulate the data to make the information more useful to him, faster. Don’t get me wrong here – SCOM is a great tool, and I’m using it more and more everyday. Just be sure to evaluate your end-state goal, and spend some time to determine the right tool for the job.

I have a new saying. PowerShell will put your kids through college. If you haven’t quite started learning PowerShell, it’s time to invest in yourself. You will be amazed at the amount of things you can do with a few lines of code. And even if you’re not an expert, you will know enough to know the potential.

Another thing I’m saying frequently is “Think, Type, Do.” Here’s why :

Snover: PowerShell is the glue coat. We’ve glued things together. So we deal with the world as it is. It’s a messy world. Ultimately, we’re trying to drive to these cmdlets — these high-level task-oriented abstractions that allow people to think about what they want, type it, and get it. Ultimately, perfection would be do/myjob/ordermeapizza. Obviously we’re not going to get there. But if you think about what you want, and you can type it and get it, PowerShell is very easy. The fact that you have to type it isn’t a big issue. It’s a different input device, but that’s not the issue. You think about something; you type it and get it. In the past, you had to do some COM programming or find some WMI classes, or invoke some command-line shell and parse the output — which can get pretty rough. Some people just love that stuff and are very successful at it. But ultimately a lot of people just want to type it and get it. So this is where the 130, 230, 2,300 cmdlets come in. (emphasis added – full article at Windows IT Pro)

And that’s just it-the more time  you spend with PowerShell, the more you will realize how many things you can accomplish with Think Type, Do.

Happy Scripting,

 

Greg

 

This post first appeared on http://www.gregramsey.net

Sample #PowerShell code for my TechEd Session – Demo 2 – Create Collections and Maintenance Windows for Server Patching.

Here’s an example of creating collections (with maintenance windows) for server patching.

Start with a .csv of servers, and the desired patch window (download sample csv).

You then run the PowerShell script to group the systems and populate new collections.
Download Script…

(if you’re having trouble with copy/paste of the code, or if code doesn’t look correct (with line feed, etc), please try an alternate browser. . .:( )


#Demo 2 - Software Update Scenario
$CMModulePath = $Env:SMS_ADMIN_UI_PATH.ToString().SubString(0,$Env:SMS_ADMIN_UI_PATH.Length - 5) `
    + "\ConfigurationManager.psd1"
Import-Module $CMModulePath -force
cd PR1:

#Removing Old Collections that begin with 'Serverpatch'
Get-CMDeviceCollection| Where-Object {$_.name -like "serverpatch*"} | Remove-CMDeviceCollection -force

#import the csv of servers with patch window
$Servers = import-csv C:\scripts\ServerPatchWindows.csv

#use the group-object cmdlet to group servers into patch windows
$Servers | Group-Object Patchwindow | ForEach-Object {
    #Gives us a window name like ServerPatch_5_26_2014_20_00
    $PatchWindowName = "ServerPatch_" + $_.Name -replace "[:\./ ]", "_"
    $PatchWindowName  

    "Creating Collection {0}" -f $PatchWindowName
    $NewColl = New-CMDeviceCollection -Name $PatchWindowName -LimitingCollectionName "All Servers" 

    #Create Schedule for 5 hour MW
    $Schedule = New-CMSchedule -Nonrecurring  -Start ([datetime]$_.Name) -durationinterval Hours -DurationCount 5

    #Create MW
    New-CMMaintenanceWindow -Name ("Maint" + $PatchWindowName) -ApplyToSoftwareUpdateOnly `
        -CollectionID $NewColl.CollectionID -Schedule $Schedule
    $Schedule = $null

    #Create Server list separated by commas
    $_.Group | foreach {
       $string = $string + '"' + $_.Servername + '",'
    }

    #build the WQL and remove the extra comma
    $wql = "select *  from  SMS_R_System where SMS_R_System.Name in (" + $string.trimend(',') + ")"
    Add-CMDeviceCollectionQueryMembershipRule -CollectionId $NewColl.CollectionID `
        -RuleName "Query1" -QueryExpression $wql
    $string = $null
}

Download Script…

Happy Scripting!

Greg

This post first appeared on http://www.gregramsey.net