0205 | Objects for the Admin
Recommended Material
- resource link
- powershell.org
- recommended book chapter
- Don Jones | "Learn Winows Powershell in a month of lunches" | chapter 9
- book
- "Learn PowerShell in a Month of Lunches, Fourth Edition"
Get the objects properties and methods
get-member
gm
Object | simple definition
- an object has things it has --> properties
- there are things it can do --> methods
Example | Processes with handles above x and sort by handles
Get-Process | where handles -gt 1000 | sort handles
Example | Display the folder content and sort by size
get-childitem | select -property name, length | sort -property lenght -descending
Example | Select specific properties when displaying the event logs
get-eventlog -logname system -newest 5 | select -property eventid, timewritten, message | sort -property timewritten | convertto-html | out-file c:\errorlog.html
Example | Casting a file and operating on it
# read in
$x = [xml](cat .\r_and_j.xml)
# display version
$x
# get the type
$x.gettype()
# get play.act.
$x.play.act[0].scene[0].speech
# group and sort
$x.play.act.scene.speech | group speaker | sort count
Filtering with where
- 2 usages
- with script and with strings
- simple version
gps | where handles -ge 1000
where
| Script version | Basic algorithm
- #1 | Take each object coming through the pipeline and assign it to a variable (
$PSItem
or$_
) - #2 | Evaluate the code between
{}
- #3 | If the code returns true --> pass the object through; if not --> discard the object
- example
get-service | where {$_.status -eq "Running"}
# is the same as
get-service | where {$PSItem.status -eq "Running"}
# extended
get-service | where {$PSItem.status -eq "Running" -and $_.name -like "b*"}
List the available operators
get-help *operators*