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:
-
# We can query for some crazy things…
-
Get–QADUser –FirstName Anna
-
# LDAP paths are not friendly
-
Get–QADUser –SearchRoot ‘OU=STO,DC=EU,DC=PXL,DC=INT’ –Enabled
-
Get–QADUser –SearchRoot ‘OU=STO,DC=EU,DC=PXL,DC=INT’ –Enabled | select Name, PasswordExpires
-
# So we store them in variable and forget this crazy syntax. 😉
-
$AD_STO = ‘OU=STO,DC=EU,DC=PXL,DC=INT’
-
# Step-by-step in our time machine… 🙂
-
$Today = Get-Date
-
$MonthAgo = $Today.AddDays(–30)
-
# let’s take a look on my PC. SP3 there already…?
-
Get–QADComputer -Name WAR000260 | Select Name, OSServicePack
-
# How are we doing in STO with that?
-
Get–QADComputer –LastChangedAfter $MonthAgo –SearchRoot $ad_sto –OSName ‘Windows XP Professional’ | select name, OsServicePack, Description
-
# Wonder if there are some people who have SP4 already. 😉
-
Get–QADComputer –OSServicePack ‘Service Pack 4’ –SizeLimit 0 –LastChangedAfter $MonthAgo | select Name, OSName, OSServicePack
-
# Now: volounteer to see all properties that we get by default… 🙂
-
‘EldereE’, ‘BaumgaG’, ‘PobereA’, ‘MoyseyS’, ‘RadzkiM’, ‘GerasiD’ | Get–Random | Get–QADUser | fl *
-
# And something even more intense: full set of AD properties…
-
Get–QADUser BielawB –IncludeAllProperties | Format-List *
-
# Cost center? Title? Anyone? 🙂 As Html? With Css?
-
Get–QADUser –SearchRoot $AD_STO –IncludedProperties division | select Name, Title, division | ConvertTo-Html -Head $MyTableCss | Out-File Report.htm
-
# So now let us send this report to ourselfs, originitated from some fellow from MS. 😉
-
$To = ‘EldereE’, ‘BaumgaG’, ‘PobereA’, ‘MoyseyS’, ‘RadzkiM’, ‘GerasiD’, ‘KroeseM’ | Get–QADUser | select -ExpandProperty email
-
Send–MailMessage –From ‘bgates@microsoft.com’ –To $To –Subject ‘TADAM!’ –SmtpServer ‘eunorth.parexel.com’ –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:
-
# 1. Try to check in registry what is your PC name in NDS using PowerShell registry provider
-
# hint: info is stored in HKLM\Software\Novell\Workstation Mangager\Identification…
-
Get-ItemProperty -?
-
# 2. Check your System eventlog to see what is the Source of most of Errors
-
Get-EventLog -?
-
Get-Help Get-EventLog -Parameter EntryType
-
Get-EventLog -LogName System | select -First 1 | Get-Member Source
-
Group-Object -?
-
Get-Help Group-Object -Parameter Property
-
# 3. Get info (formated in table) about 5 computers: computer Manufacturer, Model, TotalPhysicalMemory, UserName and format it as a table
-
# hint: all info available in win32_ComputerSystem class in WMI
-
Get-WmiObject Win32_ComputerSystem | select Caption, Manufacturer, Model, TotalPhysicalMemory, UserName
-
# 4. Get names of 5 users that will have their password expire soonest in your location, use only -Enabled users for that report.
-
Get-Help Select-Object -Parameter First
-
Sort-Object -?
-
# 4a. Make it HTML and sent it to me. 😉
-
Send–MailMessage -?
-
# 5. Save all files in your roor directory in $array and display first and last element in this array.
-
Get-ChildItem c: | Get-Member PsIsContainer
-
Get-Help Where-Object -Examples
-
# Spoiler. 😉
-
$MyArray = ‘first’,‘second’,‘third’,‘last’
-
$MyArray[0]
-
$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