Sie sind auf Seite 1von 16

PowerShell

Essentials:
For The Busy Admin(Part 2 of 5)

Presenter: Ed Wilson, MCSE, MCT, MCDBA, MCSD


Microsoft Scripting Guy
Author of: Windows PowerShell 2.0 Best
Practices, MS Press
Heard it through the
PipeLine:
OR: How to compound
PowerShell commands for fun
and profit
What is the pipeline?
Allows ability to easily work at command
line
To retrieve items and work on them
To filter out data
To persist information
To format output
Certain PowerShell cmdlets automatically
accept input from other cmdlets
Get-process notepad | stop-process
Use Get-Help on cmdlet to find if accept pipeline
input
Retrieving items and working
on them
Working with processes
Get-Process
Get-Process notepad, calc
Stop-Process
Get-Process notepad, calc | Stop-Process
Start-Process
"notepad", "calc" | foreach { start-process $_ }
Wait-Process
Get-Process notepad | Wait-Process ; dir
Demo: Retrieving and working
w/ items
Working with processes
Get-Process
Get-Process notepad, calc
Stop-Process
Get-Process notepad, calc | Stop-Process
Start-Process
"notepad", "calc" | foreach { start-process $_ }
Wait-Process
Get-Process notepad | Wait-Process ; dir
Pipe to filter
Filter processes using too much CPU or Memory
Get-Process | where { $_.pm -gt 20MB }
Get-Process | where { $_.cpu -gt 10 }
Sort processes
Get-Process | sort cpu Descending
Get-Process | sort cpu -Descending | select -First 3
Get-Process | sort processname Unique
Sort event log entries
Get-EventLog -LogName application -EntryType error | sort
source
Get-EventLog -LogName application -EntryType error | sort
source | group source
Get-EventLog -LogName application -EntryType error | sort
source | group source | sort count Descending
Demo: Pipe to filter
Filter processes using too much CPU or Memory
Get-Process | where { $_.pm -gt 20MB }
Get-Process | where { $_.cpu -gt 10 }
Sort processes
Get-Process | sort cpu Descending
Get-Process | sort cpu -Descending | select -First 3
Get-Process | sort processname Unique
Sort event log entries
Get-EventLog -LogName application -EntryType error | sort
source
Get-EventLog -LogName application -EntryType error | sort
source | group source
Get-EventLog -LogName application -EntryType error | sort
source | group source | sort count -Descending
Filter to the left when
you can
Dont do this:
Get-EventLog -LogName application | where
{ $_.timegenerated -gt [datetime]"2/28/12" }
When you can do this:
Get-EventLog -LogName application -after 2/28/12
Not just ease of use
measure-command {Get-EventLog -LogName
application | where { $_.timegenerated -gt
[datetime]"2/28/12" } }
Measure-command {Get-EventLog -LogName
application -after 2/28/12 }
Demo: Filter to the left when
you can
Dont do this:
Get-EventLog -LogName application | where
{ $_.timegenerated -gt [datetime]"2/28/12" }
When you can do this:
Get-EventLog -LogName application -after 2/28/12
Not just ease of use
measure-command {Get-EventLog -LogName
application | where { $_.timegenerated -gt
[datetime]"2/28/12" } }
Measure-command {Get-EventLog -LogName
application -after 2/28/12 }
Pipe to present
Format-Table
get-process | format-table name, id
get-process | format-table name, id AutoSize
get-process | format-table name, id -GroupBy
name -AutoSize
Format-List
get-process | Format-List *Format-Wide
Get-Service | where { $_.status -eq "running" } |
Format-List *
Format-Wide
Get-Process | Format-Wide name
Get-Process | Format-Wide name -Column 3
Get-Process | Format-Wide name -AutoSize
Demo: Pipe to present
Format-Table
get-process | format-table name, id
get-process | format-table name, id AutoSize
get-process | format-table name, id -GroupBy
name -AutoSize
Format-List
get-process | Format-List *Format-Wide
Get-Service | where { $_.status -eq "running" } |
Format-List *
Format-Wide
Get-Process | Format-Wide name
Get-Process | Format-Wide name -Column 3
Get-Process | Format-Wide name -AutoSize
Pipeline to persist information
Out-File
get-process | format-table name, id AutoSize |
out-file c:\fso\processtable.txt
Export-CSV
gps | select name, id | Export-Csv -Path
c:\fso\processCSV.csv
gps | select name, id | Export-Csv -Path
c:\fso\processCSV.csv -NoTypeInformation
Export-CliXML
GPS | Export-Clixml -Path c:\fso\proc.xml
$a = Import-Clixml C:\fso\proc.xml
Demo: Pipeline to persist
information
Out-File
get-process | format-table name, id AutoSize |
out-file c:\fso\processtable.txt
Export-CSV
gps | select name, id | Export-Csv -Path
c:\fso\processCSV.csv
gps | select name, id | Export-Csv -Path
c:\fso\processCSV.csv -NoTypeInformation
Export-CliXML
GPS | Export-Clixml -Path c:\fso\proc.xml
$a = Import-Clixml C:\fso\proc.xml
Learning Resources
The Scripting Guys Blog
Getting Started articles
Scripting Wife articles
Learn PowerShell Page
The PowerShell Quizzes (on learn PowerShell
page)
2012 Scripting Games
All in one page
Four more Road to TechEd live meetings
Last years five Road to TechEd live meetings

(PowerShell: Learn it now before it is an


Emergency)
Keep in touch Really I
mean it
Scripter@Microsoft.Com
www.twitter.com/ScriptingGuys
Scripting Guys Facebook page
The Official Scripting Guys Forum
Http://www.ScriptingGuys.Com
Http://www.ScriptingGuys.Com/blog
Http://www.ScriptingGuys.Com/LearnPowerSh

ell
2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S.
and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to 16
changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the
date of this presentation.

Das könnte Ihnen auch gefallen