My PowerShell training. :)

Last week I had finally chance to share what I know about PowerShell with others in my company. It was real ‘crush-course’ – 2 hours of speaking, without any breaks, and I still have not mentioned all goodies PowerShell has for any IT Pro within range. 😉 I’ve used some slides from PowerShell’s Almighty’s presentation from TechEd Berlin: great to show objects and pipelines. I had number of demos to show how it works in action (ISE is really great for demos!), prepared homework so that guys could give it a try and jump on this horse. I tried to focus on stuff that may be handy for people on a position like mine, but had to start with some basics and I guess it was really hard (no scripters, very little cmdline experience, no idea about object programming, 0 experience with WMI). My manager also wanted to attend and I’m happy that he really enjoyed the training and already played with PowerShell (did first task from homework 😀 )

So what topics did I covered? Basics (what it is, how to install it, use it, few words about hosts, how you can jump from cmd/ bash, few words about objects, pipeline, variables, arrays), 3 cmdlets to start with (Get-Command, Get-Help, Get-Member) and technologies: WMI and AD (Quest). Later I mentioned some things that one can do to avoid problems (-WhatIf, –Confirm, building command one pipe at the time). When I finished and asked for questions I got silence instead of a reply. Than my manager just said that it’s hard to ask questions now, because they need to consume this piece of data first. 😀 So I just added Christmas Tree from Jaykul and show them in action script to prepare weekly report (with data taken from AD, imported to Excel, with pivot table, chart and conditional formatting). Just to let them know – nothing is preventing you from starting with Get-Process, and finally end up with Get-JobDone. 😉

One sample from demos:

PowerShell, using GeSHi
  1. # We can query for some crazy things…
  2. GetQADUser FirstName Anna
  3. # LDAP paths are not friendly :/
  4. GetQADUser SearchRoot ‘OU=STO,DC=EU,DC=PXL,DC=INT’ Enabled
  5. GetQADUser SearchRoot ‘OU=STO,DC=EU,DC=PXL,DC=INT’ Enabled | select Name, PasswordExpires
  6. # So we store them in variable and forget this crazy syntax. 😉
  8. # Step-by-step in our time machine… 🙂
  9. $Today = Get-Date
  10. $MonthAgo = $Today.AddDays(30)
  11. # let’s take a look on my PC. SP3 there already…?
  12. GetQADComputer -Name WAR000260 | Select Name, OSServicePack
  13. # How are we doing in STO with that?
  14. GetQADComputer LastChangedAfter $MonthAgo SearchRoot $ad_sto OSName ‘Windows XP Professional’ | select name, OsServicePack, Description
  15. # Wonder if there are some people who have SP4 already. 😉
  16. GetQADComputer OSServicePack ‘Service Pack 4’ SizeLimit 0 LastChangedAfter $MonthAgo | select Name, OSName, OSServicePack
  17. # Now: volounteer to see all properties that we get by default… 🙂
  18. ‘EldereE’, ‘BaumgaG’, ‘PobereA’, ‘MoyseyS’, ‘RadzkiM’, ‘GerasiD’ | GetRandom | GetQADUser | fl *
  19. # And something even more intense: full set of AD properties…
  20. GetQADUser BielawB IncludeAllProperties | Format-List *
  21. # Cost center? Title? Anyone? 🙂 As Html? With Css?
  22. GetQADUser SearchRoot $AD_STO IncludedProperties division | select Name, Title, division | ConvertTo-Html -Head $MyTableCss | Out-File Report.htm
  23. # So now let us send this report to ourselfs, originitated from some fellow from MS. 😉
  24. $To = ‘EldereE’, ‘BaumgaG’, ‘PobereA’, ‘MoyseyS’, ‘RadzkiM’, ‘GerasiD’, ‘KroeseM’ | GetQADUser | select -ExpandProperty email
  25. SendMailMessage From ‘’ To $To Subject ‘TADAM!’ SmtpServer ‘’ BodyAsHtml -Body (Get-Content .\Report.htm | Out-String)

I think it was really worth it… I was tired like hell after, but you know – PowerShell is a joy you want to share. It’s a key to crazy stuff that you would like others to discover. I’m happy because I already got a list of another 15 people that would like to attend to this training. And this time I won’t have to hurry so much. Last time I had a group of people that are alone on site and need to do everything by themselves. What it means? They had no time to waste. They could expect storm any second. 😉 And when you have to choose between some fancy training and fire at your door, what would you choose to take care of first? So homework I’ve created has no deadline:

PowerShell, using GeSHi
  1. # 1. Try to check in registry what is your PC name in NDS using PowerShell registry provider
  2. # hint: info is stored in HKLM\Software\Novell\Workstation Mangager\Identification…
  3. Get-ItemProperty -?
  4. # 2. Check your System eventlog to see what is the Source of most of Errors
  5. Get-EventLog -?
  6. Get-Help Get-EventLog -Parameter EntryType
  7. Get-EventLog -LogName System | select -First 1 | Get-Member Source
  8. Group-Object -?
  9. Get-Help Group-Object -Parameter Property
  10. # 3. Get info (formated in table) about 5 computers: computer Manufacturer, Model, TotalPhysicalMemory, UserName and format it as a table
  11. # hint: all info available in win32_ComputerSystem class in WMI
  12. Get-WmiObject Win32_ComputerSystem | select Caption, Manufacturer, Model, TotalPhysicalMemory, UserName
  13. # 4. Get names of 5 users that will have their password expire soonest in your location, use only -Enabled users for that report.
  14. Get-Help Select-Object -Parameter First
  15. Sort-Object -?
  16. # 4a. Make it HTML and sent it to me. 😉
  17. SendMailMessage -?
  18. # 5. Save all files in your roor directory in $array and display first and last element in this array.
  19. Get-ChildItem c: | Get-Member PsIsContainer
  20. Get-Help Where-Object -Examples
  21. # Spoiler. 😉
  22. $MyArray = ‘first’,‘second’,‘third’,‘last’
  23. $MyArray[0]
  24. $MyArray[1]

Man, I really can’t wait to next training session. I guess it might be even better: I would expect more scripters/ quasi-programmers/ cli-users this time. And I probably will split the group in 3 based on that 3 factors.

UPDATE: Uploaded my SlideDeck, in case you want to take a look/ comment/ re-use: PowerShell Training slide deck


Migration is always pain in the… back.

I just took a quick look at my old posts. And I’m really upset with what I saw there. I’m not sure if this is because GeSHi is not working fine with WordPress, or is it caused by migration of posts between Windows Live platform and WordPress platform…

Anyway: all code I had on blog with ‘\’ is no presented without this sign. And I really don’t feel like updating it all to get it back where it should be. 😦

I’m not sure if that can be automated, I would guess not. Not really nice thing to have. So let’s give it a try, path in GeSHi using ‘/’ and ‘\’:

PowerShell, using GeSHi
  1. # First folder with modules in $env:PsModulePath:
  2. MOD1:\BSonPosh\BSonPosh.psm1
  3. # Second one…
  4. MOD2:\BitsTransfer\BitsTransfer.psd1
  5. # Last but not least (using / )
  6. MOD3:/ISEFun/ISEFun.psm1

Wonder what all those MOD# are all about? Well, I’ve decided that it will make it easier for me to psedit any module .psm1 file. I don’t have to remember paths to modules, I just have to test where (in which of MOD# drives) it is stored. And Get-PsDrive will help me with that:

PowerShell, using GeSHi
  1. PS C:\TEMP\PowerShell> Get-PSDrive MOD* | Format-Table name, root  -AutoSize
  3. Name Root                                                                   
  4. —- —-                                                                   
  5. MOD1 C:\Documents and Settings\bielawb\My Documents\WindowsPowerShell\Modules
  6. MOD2 C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\                    
  7. MOD3 C:\TEMP\PowerShell\Backup\Modules\           

OK. So now let us see what WordPress will do with my paths this time… 😉