Upload
thomas-lee
View
910
Download
63
Embed Size (px)
DESCRIPTION
This deck presents an overview to PowerShell and some aspects of the language, best practice, etc.
Citation preview
Thomas Lee - MCT
Plataan
PowerShell 101
1. What IS PowerShell?
2. What are Cmdlets, Objects and The Pipeline?
3. Introducing the PowerShell Language
4. Installing and using PowerShell
5. Configuring your environment with profiles
Course Overview
PowerShell 101
1. What is PowerShell?
2. PowerShell Architecture
3. Core PowerShell Components
1. What is
PowerShell?
PowerShell 101
• Microsoft’s Strategic Task Automation Platform for IT Professionals
• Shell – think Unix like
• Scripting Language – power of Perl/Ruby
• Extensible – bring on the community
• Built on .NET and Windows – MS-centric
1. What is
PowerShell?1.1 What Is PowerShell?
PowerShell 101
1. What is
PowerShell?1.1 PowerShell Architecture
PowerShell 101
1. What is
PowerShell?1.1 PowerShell Architecture With Remoting
PowerShell 101
1. What is
PowerShell?1.2 PowerShell Components
Cmdlets Objects Pipeline
PowerShell 101
1. What are Cmdlets?
2. What are Objects?
3. What is the Pipeline
4. Discovery and The Community
5. Why Does this Design Matter?
2. Cmdlets,
Objects, and
the Pipeline
PowerShell 101
• A unit of functionality – a mini-program that does something useful
• Implemented as a .NET Class
• Get some with PowerShell or with applications and Windows roles/feature, buy some commercial Cmdlets, find some on the internet, or build your own!
• Cmdlets can have aliases
• Built in or add your own
• Aliases do NOT include parameter aliasing
• Cmdlets take parameters
• Parameters have names (prefaced with “-”)
• Parameter names can be abbreviated and sometimes omitted
2. Cmdlets,
Objects, and
the Pipeline
2.1 What Are Cmdlets?
PowerShell 101
• Cmdlets are named with Verb-Noun syntax
Noun always singular
Sometimes Verb-PrefixNoun
• Examples
Get-Process
Get-AdUser
• Discovering cmdlet names and usage is easy
Get-Help
Get-Command
• You can use Get-Verb to get all the verbs
2. Cmdlets,
Objects, and
the Pipeline
2.1 Cmdlet Naming
PowerShell 101
• Basic form
CmdetName -Parametername ParameterValue …
Get-Process –Name notepad
• Parameters can be abbreviated or omitted
Get-Process –Na notepad
Get-Process notepad
• Parameter values can include Wildcards (aka ‘globbing’)
Get-Process –Name power*
• See help text for details of parameters
2. Cmdlets,
Objects, and
the Pipeline
2.1 Calling Cmdlets
PowerShell 101
• 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
• Objects dramatically simplifies scripting
2. Cmdlets,
Objects, and
the Pipeline
2.2 What are Objects?
PowerShell 101
• Everything in PowerShell is an object
• Cmdlets produce and consume objects
Eg Get-Process produces System.Diagnostics.Process
• You can use Get-Member to tell you the object type
• Refer to MSDN for more detail (in most cases!)
2. Cmdlets,
Objects, and
the Pipeline
2.2 Objects in PowerShell
PowerShell 101
• PowerShell supports:
.NET objects
COM objects
WMI objects
Custom Objects
• Syntax and usage vary
So similar, yet so different
2. Cmdlets,
Objects, and
the Pipeline
2.2 PowerShell Object Support
PowerShell 101
• The pipeline connects cmdlets
One cmdlet outputs objects
Next cmdlet uses those objects as inputGet-Process | Sort-Object Name
• Pipeline is not a new concept
Came From Unix/Linux
PowerShell Pipes objects not text
• The Pipeline provides rich functionality and convenience
But there can be a performance hit
2. Cmdlets,
Objects, and
the Pipeline
2.3 The Pipeline
PowerShell 101
• Discovery means using the product to find out more about the product
How easy is it to discover what you need to know?
Discovery is a key attribute of PowerShell
• Discovery includes
• Predictable command names (standard verbs, and nouns)
• Consistent parameter usage
• Consistent output (objects vs text)
• Built-in help (Get-Help, Get-Command)
• Online help (Get-Help Get-Process –Online)
• Discovery leverages what you know
• Discovery is something all PowerShell users depend on!
2. Cmdlets,
Objects, and
the Pipeline
2.4 Discovery And The Community
PowerShell 101
• You never walk alone
• HUGE PowerShell ecosystem
Other PowerShell Users
Product team
Vendors
MVPs
• Various ways to engage with the community
• SpiceWorks
• PowerShell.Com
• Microsoft Forums
• Etc, etc, etc
2. Cmdlets,
Objects, and
the Pipeline
2.4 The PowerShell Community
PowerShell 101
• Simple to use
Far easier to compose
• Powerful in operation
PowerShell (and .NET) do the heavy lifting
• Helps to integrates functionality stacks
Operating System
Application (Microsoft and others)
PowerShell Base
Community efforts
• The IT Industry is embracing PowerShell
Cisco, Vmware, EMC, etc all support PowerShell cmdlets
2. Cmdlets,
Objects, and
the Pipeline
2.5 Why Does This Design Matter?
PowerShell 101
• PowerShell is an IT Professional tool to help you manage Windows and some non-windows systems
• PowerShell’s key components include Cmdlets, Objects, and The Pipeline
• This approach has become mainstream both within Microsoft and with the wider IT eco-system
2. Cmdlets,
Objects, and
the Pipeline
Module Summary
PowerShell 101
1. Introducing the PowerShell Language
2. Variables and Operators
3. Scalars (numbers and strings)
4. Arrays
5. Hash tables
6. Variable types
7. Other language features
3. The
PowerShell
Language
PowerShell 101
• The PowerShell language is derivative – some parts coming from:
• Linux and Unix shells
• Windows CMD.EXE based applications
• C#
• DEC DCL
• Perl
• And others!
• You need to know the language before you can write scripts
• Learning is underpinned by Discovery
3. The
PowerShell
Language
3.1 Introducing the PowerShell Language
PowerShell 101
• 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
3. The
PowerShell
Language
3.2 Variables
PowerShell 101
• 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
• See variables by:Get-ChildItem Variable:*
3. The
PowerShell
Language
3.2 Built-in Variables
PowerShell 101
• Scalar variable contains a single value$i=42
• Can use value directly$i=42; $i
• Often used to calculate a value used for output$count = (Get-ChildItem –File C:\Foo).count
"You have {0:n0} files in C:\Foo" –f $count
3. The
PowerShell
Language
3.3 Scalars
PowerShell 101
• Numbers can be integer or [int]
• Numbers can be floating point/decimal [double]
• PowerShell can convert between number and string$i = 10 + '10'
$i #what is $I
• But sometimes the conversion is not obvious• $i = '123' + 10
• $i #what is $I now
3. The
PowerShell
Language
3.3 Numbers and Number Conversion
PowerShell 101
• You can express strings with single quotes ' or double quotes "
$string1 = 'have a nice day'
$string2 = "have a nice day"
• Double quoted strings supports substitution
$i = 42
"the value of is $i"
• Scalar only gotcha
$s = ls c:\foo -file
"There are $s.count files in c:\foo"
3. The
PowerShell
Language
3.3 Strings
PowerShell 101
• Array variables contain multiple values
• Array members addressed with [], e.g. $a[0]
$a[0] is first
$a[-1] is last
Use .GetType() to get type of an array
$myfoo = LS c:\foo
$myfoo.gettype()
3. The
PowerShell
Language
3.4 Arrays
PowerShell 101
• Arrays can be one type, multiple types
$array = 1,
'hello',[system.guid]::newguid()
$array | Get-Member
• You typically use loop constructs to process an array
3. The
PowerShell
Language
3.4 More on Arrays
PowerShell 101
• 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!
• Hash tables are used throughout PowerShell
• For more details see Get-Help about_hash_tables
3. The
PowerShell
Language
3.5 Hash Tables
PowerShell 101
• Variables can be implicitly typed
PowerShell works it out by default
$I=42;$i.gettype()
• Variables can be explicitly typed
[system.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()
3. The
PowerShell
Language
3.6 Variable Type
PowerShell 101
• To specify type use [<type name>] before variable name
[System.Int64] $i = 42
• Type accelerators
Synthetic types created by PowerShell
Exist for .NET and WMI objects
WMI covered more in module 8
Translated transparently
[int] translated into [system.int32]
• Usie type accelerators as if they were real .NET types
[int64] $i = 42
3. The
PowerShell
Language
3.6 Types and Type Accellerators
PowerShell 101
[int]/[int16]/[int32]/[int64]
[uint]/[uint16]/[uint32]/[uint65]
[float]/[single]/[double]
[char]/[byte]/[sbyte]
[boolean]
[datetime]
[guid]
[void]
Etc, etc, etc./
• NB Some of these are type accelerators
• You can create new ones – but why?
3. The
PowerShell
Language
3.6 What Types Can I Use
PowerShell 101
• Operators
• Expressions
• Wild Cards
• Regular Expressions
• Case sensitivity – or not
• You have to know the language to write scripts
• More details of the scripting language are in separate course(s)
3. The
PowerShell
Language
3.7 More PowerShell Language Features
PowerShell 101
• We looked at the PowerShell Language in overview and saw
Introducing the PowerShell Language
Variables and Operators
Scalars (numbers and strings)
Arrays
Hash tables
Variable types
Other language features
• There is more to the PowerShell language – but outside the scope of this course
3. The
PowerShell
Language
Module Summary
PowerShell 101
1. Installing PowerShell
2. Using PowerShell4. Installing
and using
PowerShell
PowerShell 101
• Installation is a bit of a variable feast
• Installation depends on the OS
• PowerShell is built into Win 8, Server 2012 R2 and later
• So nothing to install for these OSs
• Earlier versions vary
Different versions of PowerShell are supported on different versions of Windows
With Server 2008 for example, PowerShell was an optional feature
RTFM Carefully
• Version 5 is available either as part of Win 10 pre-release or as a separate download for Windows 8.x/Server 2012 R2
• Beware of search engine links to beta versions
4. Installing
and using
PowerShell
4.1 Installing PowerShell
PowerShell 101
• From the Windows client/server Start screen
Windows Server has a PowerShell icon on the start bar
Or type PowerShell from the Start Screen
• From the Windows Desktop
Create an icon on the desktop or start screen if using Windows 8.x or later as an alternative to having it on the Start bar
• PowerShell can also be p-art of an application
GUI layered on PowerShell (eg Exchange Management Console)
An application that makes use of PowerShell (eg Server Mangagerin Server 2012 R2)
• Third Party IDEs
PowerShell Plus
Sapien PowerShell Studio
PowerGUI
4. Installing
and using
PowerShell
4.2 Using PowerShell
PowerShell 101
• Using PowerShell can be as simple as using the application
• Using Server Manager to install a new feature on windows
• Using Exchange Management Shell to creat a new mailbox
• But for most IT Pros using PowerShell means writing scripts that automate your IT environment
• That means writing scripts that utilise objects relevant to your needs (files, computers, AD components etc.)
• More about those aspects of PowerShell in a more detailed PowerShell course
4. Installing
and using
PowerShell
4.2 Using PowerShell
PowerShell 101
• Installing PowerShell is, increasingly, something you do not need to do as it’s already installed
• You can use PowerShell in a variety of ways
• From the command line or the ISE (Interactive Scripting Environment)
• From within third party tools (eg PowerGUI)
• From withing an application that makes use of PowerShell under the covers (e.g. Server Manager in Server 2012 R2 and later).
4. Installing
and using
PowerShell
Module Summary
PowerShell 101
1. What is a profile?
2. Why do you use Profiles?
3. What can you put into your profile?
5. Configuring
PowerShell
With Profiles
PowerShell 101
• Special scripts that run at startup
• Each System has 4 Profiles
• Per User for a single PowerShell host (e.g. the ISE)
• For ALL users for a single PowerShell host
• Per user for all PowerShell hosts
• For all users for all PowerShell hosts
• Profiles allow you to leverage other people’s work and to change PowerShell to work like you want
5. Configuring
PowerShell
with Profiles
5.1 What is a profile?
PowerShell 101
• You have 4 as follows:
$profile | Get-Member *Host* |
Format-List name,definition
• Most users just use CurrentuserCurrentHost
$Profile variable points to that file
5. Configuring
PowerShell
with Profiles
5.1 Where do Profiles Live?
PowerShell 101
• Profiles are just .ps1 files that run when PowerShell/ISE start
• You can use the ISE or Notepad to edit them
• You do not need to use ALL 4 profile – just use $profile inside the host
5. Configuring
PowerShell
with Profiles
5.1 Managing your Profiles
PowerShell 101
• CurrentUserThisHost profile easiest to manage
• Built in $profile variable points directly to that file
• Does it exist?
Test-Path $profile
• Create it if not
New-item –path $profile –itemtype file -Force
• Edit it in notepad/ISE
Notepad $profile
PSEDIT $profile # in ISE only!
Other Profiles
Create variable in profile to point to them
Edit vs. copy
5. Configuring
PowerShell
with Profiles
5.1 What is a profile?
PowerShell 101
• Profiles configure your environment as you need it
• Profiles help to make PowerShell easier for YOU to use
• You can share profiles to simplify things
For example AllUsersAllShell profile holds corporate aliases
• Be careful to avoid your profile files being overly long
5. Configuring
PowerShell
with Profiles
5.2 Why do you use Profiles?
PowerShell 101
• Lots of things including:
Create new variables holding useful things
Create new Provider drives giving you shortcuts to common places including in the registry, the file system, AD, etc
Setting up your PowerShell Prompt
Adding menus to the ISE
Adding functions (or script cmdlets) that you commonly use
5. Configuring
PowerShell
with Profiles
5.3 What Can You Put Into a Profile?
PowerShell 101
• Installing PowerShell is, increasingly, something you do not need to do as it’s already installed
• You can use PowerShell in a variety of ways
• From the command line or the ISE (Interactive Scripting Environment)
• From within third party tools (eg PowerGUI)
• From within an application that makes use of PowerShell under the covers (e.g. Server Manager in Server 2012 R2 and later).
5. Configuring PowerShell with Profiles
Module Summary
PowerShell 101
• In this course we looked at:
What IS PowerShell?
What are Cmdlets, Objects and the Pipeline?
An brief overview to the PowerShell Language
How you install and use PowerShell
Configuring PowerShell with profile files
Course Summary
PowerShell 101
Plataan
online & classroom training: www.plataan.be
www.facebook.com/PlataanAlumni
@PlataanAlumni
http://www.linkedin.com/company/plataan
Plataan App for Windows Phone and iPhone
Thank You For Watching