42
POWERSHELL 101 – WHAT IS IT AND WHY IT MATTERS Thomas Lee ([email protected] ) MCT and PowerShell MVP

PowerShell 101 – What is it And Why It matters

  • Upload
    cargan

  • View
    70

  • Download
    1

Embed Size (px)

DESCRIPTION

Thomas Lee ( [email protected] ) MCT and PowerShell MVP. PowerShell 101 – What is it And Why It matters. What is it/Why it Matters. What IS PowerShell? What are Cmdlets, Objects and the Pipeline? Language fundamentals How do I install and setup PowerShell> How do I use PowerShell? - PowerPoint PPT Presentation

Citation preview

Page 1: PowerShell  101 – What is it And Why  It  matters

POWERSHELL 101 – WHAT IS IT

AND WHY IT MATTERS

Thomas Lee ([email protected])MCT and PowerShell MVP

Page 2: PowerShell  101 – What is it And Why  It  matters

What is it/Why it Matters

What IS PowerShell? What are Cmdlets, Objects and the

Pipeline? Language fundamentals How do I install and setup PowerShell> How do I use PowerShell? PowerShell profiles Getting the most from PowerShell Why does it matter?

Page 3: PowerShell  101 – What is it And Why  It  matters

What IS PowerShell?

PowerShell is Microsoft’s task automation platform. Part of Microsoft’s Common Engineering Criteria Included with every version of Windows

7/Server 2008 R2 (and as a OS patch for earlier versions)

In a couple of years, if you don’t know PowerShell you may not have a job as an IT Pro!

Page 4: PowerShell  101 – What is it And Why  It  matters

What’s IN PowerShell

Shell Unix like (console.exe) Lightweight IDE (sort of VS Lite)

Scripting Language Power of Perl/Ruby

Extensible Create your own cmdlets/providers/types/etc Leverage the community

Built on .NET and Windows MS-centric/MS-focused

Page 5: PowerShell  101 – What is it And Why  It  matters

PowerShell Architecture

Page 6: PowerShell  101 – What is it And Why  It  matters

Remoting with PowerShell

Page 7: PowerShell  101 – What is it And Why  It  matters

PowerShell Fundamentals

Cmdlets Objects Pipeline

Community

Discovery

PowerShell Language Features

Page 8: PowerShell  101 – What is it And Why  It  matters

What Are Cmdlets?

The fundamental unit of functionality Implemented as a .NET Class Get some, buy some, find some, or build

your own Cmdlets take parameters

Parameters have names (prefaced with “-”) Parameter names can be abbreviated

Cmdlets can have aliases Built in or add your own Aliases do NOT include parameter aliasing

Page 9: PowerShell  101 – What is it And Why  It  matters

What Are Objects?

A computer abstraction of a real life thing A process A server An AD User

Objects have occurrences you manage The processes running on a computer The users in an OU The files in a folder

Page 10: PowerShell  101 – What is it And Why  It  matters

PowerShell Object Support

PowerShell supports: .NET objects COM objects WMI objects

Syntax and usage vary So similar, yet so different

LOTS more detail – just not in this session

Page 11: PowerShell  101 – What is it And Why  It  matters

What Is The Pipeline?

Connects cmdlets One cmdlet outputs objects Next cmdlet uses them as input

Pipeline is not a new concept Came From Unix/Linux PowerShell Pipes objects not text

Page 12: PowerShell  101 – What is it And Why  It  matters

What IS the Pipeline

Connects output from a cmdlet to the input of another cmdlet

Combination of the all cmdlets etc makes a pipeline

Page 13: PowerShell  101 – What is it And Why  It  matters

PS C:> Get-Process | Sort-ObjectSort-Object

Cmdlet

Get-Process Cmdlet

Process Object

Pipe

The Pipeline In Action

Page 14: PowerShell  101 – What is it And Why  It  matters

Why This Design Matters

Simple to use Just string cmdlets/scripts/functions together Simpler to write in many cases

Very powerful in operation Lets PowerShell do the heavy lifting

Integrates functionality stacks OS Application PowerShell Base Community

Page 15: PowerShell  101 – What is it And Why  It  matters

Discovery And Community

A key concept in PowerShell Built-in help (Get-Help, Get-Command) Automatic linking to online help Huge PowerShell ecosystem – the

community Social networking: eg Twitter, Facebook, etc Mailing lists and newsgroups Web sites and blogs User Groups 3rd party support – free stuff coming!

Page 16: PowerShell  101 – What is it And Why  It  matters

DEMO

Cmdlets, Objects, and the Pipeline

Page 17: PowerShell  101 – What is it And Why  It  matters

Variables

Variables contain objects during a session Variables named starting with ‘$’

$myvariable = 42

Variable’s Type is implied (or explicit) $myfoo = ls c:\foo

Variables can put objects into pipeline $myfoo | format-table name

Variables can be reflected on $myfoo | get-member

You can use variables in scripts and the command line

Page 18: PowerShell  101 – What is it And Why  It  matters

Built-in Variables

Some variables come with PowerShell $PSVersionTable $PSHome

Some variables tell PowerShell what to do $WarningPreference $MaximumHistoryCount

You can create variables in Profile(s) that persist Using your profile (more later)

See variables LS Variable:

Page 19: PowerShell  101 – What is it And Why  It  matters

Scalar Values

Scalar variable contains a single value $i=42

Can use properties/methods directly $i=42; $i.tostring("p")

Use to calculate a value for use in formatting See more in discussion on Formatting (next

session)

Page 20: PowerShell  101 – What is it And Why  It  matters

Array Values

Array variables contain multiple values/objects

Array members addressed with [], e.g. $a[0] $a[0] is first item in array $a[-1] is last item

Use .GetType() $myfoo = LS c:\foo $myfoo.gettype()

Array members can be one or multiple types LS c:\ | Get-Member

Arrays used with loops

Page 21: PowerShell  101 – What is it And Why  It  matters

Hash Table

Special type of an array Also known as dictionary or property bag

Contains a set of key/value pairs Values can be read automagically

$ht=@{"singer"="Jerry Garcia“; "band"="Greatful Dead”}$ht.singer

Value can be another hash table! See Get-Help about_hash_tables

Page 22: PowerShell  101 – What is it And Why  It  matters

Variable Type

Variables can be implicitly typed PowerShell works it out by default $I=42;$i.gettype()

Variables can be explicitly typed [int64] $i = 42 $i.gettype()

Typing an expression $i = [int64] (55 – 13); $i.gettype() $i = [int64] 55 – [int32] 13; $i.gettype() $i = [int32] 55 – [int64] 13; $i.gettype()

Page 23: PowerShell  101 – What is it And Why  It  matters

Operators

Operator Type OperatorArithmetic operator +, -, *, /, %

See: about_arithmetic operator

Assignment operators =, +=, -=, *=, /=, %=See: about_assignment_operators

Comparison Operators -eq, -ne, -gt, -lt, -le, -ge, -like,-notlike, -match, -notmatch-band, -bor, -bxor,-bnotSee about_comparison_opeartors

Logical Operators -and, -or, -xor –not, !See about_logical_operators

Page 24: PowerShell  101 – What is it And Why  It  matters

More Operators

Operator Type OperatorRedirection operators >, >> 2> 2>&1

See: get-help about-redirection

Split/Join operators -split, -joinSee: get-help about_split,get-help about_join

Type operators -is, -isnot, -asSee get-help about_type_operators

Urinary operators ++, --

Page 25: PowerShell  101 – What is it And Why  It  matters

Special Operators

Operator Function OperatorCall &Property dereference .Range Operator ..Format operator -fSubexpression operator $( )Array Subexpression operator

@( )

Array operator ,

Page 26: PowerShell  101 – What is it And Why  It  matters

Expressions

Variables plus operators Produce some value Value can be an object too!

Simple expressions $i+1; $j-1; $k*2, $l/4

Boolean expression General format is: <value> -<operator>

<value> $a –gt $b

Page 27: PowerShell  101 – What is it And Why  It  matters

Why not use > or < etc??

History Redirection etc a historical reality

Keep the parser simple PowerShell does more than simple

“>”!! Case insensitive vs. case sensitive

comparisons

Page 28: PowerShell  101 – What is it And Why  It  matters

What else?

Lots! Modules

Way of managing PowerShell code in an enterprise

Remoting 1:1 or 1:many Remoting

XML Support Native XML

More, more, more Attend some great training

Page 29: PowerShell  101 – What is it And Why  It  matters

DEMO

Objects,Variables, Types, etc

Page 30: PowerShell  101 – What is it And Why  It  matters

How Do I Install PowerShell? Built in to Win7, Server 2008 R2

On Server, add ISE Feature On Server Core PowerShell – add feature

(and .NET) Down-level operating systems

Shipped as an OS Patch with WinRM – KB 968929

Get it from the net - http://tinyurl.com/pshr2rtm

NB: Different versions i386 vs x64, XP/Vista/2008

No version for IA64 or Windows 2000(Or earlier)

Beware of search engine links to beta versions

Page 31: PowerShell  101 – What is it And Why  It  matters

How Do I Use PowerShell?

From the Console Start/PowerShell

From the PowerShell ISE Start/PowerShellISE

Part of an application GUI layered on PowerShell Application focused console

Third Party IDEs PowerShell Plus PowerGUI

Page 32: PowerShell  101 – What is it And Why  It  matters

How Do I Configure PowerShell Add third party tools

There are lots! Using Built-in tools

This will vary with what OS you use and which applications you are running

Configure PowerShell using Profiles

Page 33: PowerShell  101 – What is it And Why  It  matters

What are Profiles?

Special scripts that run at startup Multiple Profiles

Per User vs. Per System Per PowerShell Console vs. for ALL consoles ISE profile – just for ISE

Creating a profile Leveraging other people’s work If you use it - stick it in your profile!

Page 34: PowerShell  101 – What is it And Why  It  matters

Common Profile Contents

Set up prompt Function prompt {“Psh`[$(pwd)]: “}

Add personal aliases Set-Alias gh get-help

Create PSDrives New-PsDrive demo file e:\psh\demo

Set size/title of of PowerShell console $host.ui.rawui.WindowTitle = "PowerShell Rocks!!!" $host.ui.rawui.buffersize.width=120 $host.ui.rawui.buffersize.height=9999 $host.ui.rawui.windowsize.width=120 $host.ui.rawui.windowsize.height=42

Page 35: PowerShell  101 – What is it And Why  It  matters

Getting More From PowerShell Start with replacing CMD.Exe with

PowerShell Get some good training Use shared code where possible Use great tools Don’t re-invent the wheel Leverage the community

Page 36: PowerShell  101 – What is it And Why  It  matters

PowerShell Training

Official MOC 6434 – 3 day based on PowerShell V1 10325 – upcoming 5 day course (due 8/10)

PowerShell Master Class http://www.powershellmasterclass.com

New Horizons CWL V2 course coming

Page 37: PowerShell  101 – What is it And Why  It  matters

Resources

Use your favorite search engine!!! PowerShell Owner’s Manual

http://technet.microsoft.com/en-us/library/ee221100.aspx

PowerShell – Getting Started Guide http://tinyurl.com/pshgsg

PowerShell references http://www.reskit.net/psmc

Page 38: PowerShell  101 – What is it And Why  It  matters

And for your dev trainers…

PowerShell is not just an IT Pro tool Developers need it too

Build cmdlets Build providers Build Management GUIs

More on this topic in some other day.

Page 39: PowerShell  101 – What is it And Why  It  matters

Let’s Summarise

PowerShell: Combines cmdlets, objects and the pipeline Provides a programming language and

many more features we’ve not looked at today

Enables local and remote management Is easy to get and easy to customise via

profiles Is supported by discovery and a vibrant

community PowerShell is the future of managing

Windows and Windows applications

Page 40: PowerShell  101 – What is it And Why  It  matters

Now For The FREE Stuff!

I have free copies of PowerShell Pro Idera’s PowerShell Development Tool

The first 25 folks to email me will get a link to download the software (and a license).

I gave you the email address at the start of this presentation

The codeword is “POWERSHELLROCKS!” The first 25 to mail that code word to the

address I gave earlier will win their prixe!

Sorry but this offer has now closed and the winners chosen.

However, see the .scripting newsgroup for more infoon both PowerShell and on an offer from Idera!

Page 41: PowerShell  101 – What is it And Why  It  matters

Questions?

I will answer what I can now

More questions – email me at: [email protected]

Or use the .Scripting MCT newsgroup

Page 42: PowerShell  101 – What is it And Why  It  matters

THE END