powershell workshop series: session 2

18
WELCOME TO THE POWERSHELL DEVELOPMENT WORKSHOP SESSION 2 Bryan Cafferky Business Intelligence Consultant BPC Global Solutions LLC [email protected] www.sql-fy.com http://www.meetup.com/The-RI-Microsoft-BIUG/

Upload: bryan-cafferky

Post on 16-Jul-2015

68 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: PowerShell Workshop Series: Session 2

WELCOME TO THE POWERSHELL

DEVELOPMENT WORKSHOP

SESSION 2Bryan Cafferky

Business Intelligence ConsultantBPC Global Solutions LLC

[email protected]

http://www.meetup.com/The-RI-Microsoft-BIUG/

Page 2: PowerShell Workshop Series: Session 2

WORKSHOP FORMAT

PowerShell Development Workshop 2

• Series of presentations and exercises that build on each other.

• Start at the very basics and work up.

• Webinar for all sessions.

• GoTo Meeting Messages to ask questions.

Page 3: PowerShell Workshop Series: Session 2

GOALS OF THIS LESSON

PowerShell Development Workshop 3

• Discuss cmdlets, pronounced command-lets.

• Introduce some useful cmdlets.

• Introduce PowerShell variables.

• Discuss Aliases.

• Discuss PowerShell Providers.

• Learn about PowerShell Drives.

Page 4: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: CMDLET REVIEW

PowerShell Development Workshop 4

Is a single-feature command that manipulates objects in Windows PowerShell.

You can recognize cmdlets by their name format -- a verb and noun separated by a dash (-), such as Get-Help, Get-Process, and Start-Service

Are designed to be used in combination with other cmdlets

"get" cmdlets only retrieve data,

"set" cmdlets only establish or change data,

"format" cmdlets only format data,

"out" cmdlets only direct the output to a specified destination..

Each cmdlet has a help file that you can access by typing:

get-help <cmdlet-name> -detailed

Example: get-help write-host

Page 5: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: CMDLET REVIEW: PARAMETERS

PowerShell Development Workshop 5

Write-Host -Object "Good day!" -ForegroundColor DarkGreen -BackgroundColor Yellow

Values passed to a cmdlet to control what it does.

Parameter Name

Parameter Values

Page 6: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: CMDLET REVIEW: PARAMETERS - EXAMPLE

PowerShell Development Workshop 6

Passing Parameters by Position

Write-Host "Good day!“

Write-Host [-Object] [-NoNewline] [-Separator] [-ForegroundColor] [BackgroundColor]

[<CommonParameters>]

Passing Parameters by Name

Write-Host -Object "Good day!" Parameter Name

No Parameter Name

Page 7: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: CMDLET REVIEW: COMMON PARAMETERS

PowerShell Development Workshop 7

Parameters that are supported by all cmdlets.

-Debug (db)-ErrorAction (ea)-ErrorVariable (ev)-OutVariable (ov)-OutBuffer (ob)-PipelineVariable (pv)-Verbose (vb) -WarningAction (wa)-WarningVariable (wv)

* Aliases are in parentheses.

Page 8: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: CMDLET REVIEW

USEFUL CMDLETS

PowerShell Development Workshop 8

Get-Help Get Help on a cmdlet.

Read-Host Get user input.

Write-Host Write to the console.

Get-Member Display methods and properties of an object.

Set-Location Set the current location for a provider.

Get-ChildItem List the items within a collection.

Export-Csv Write out a CSV file.

Import-CSV Load a CSV file.

Invoke-Item Loads the file using the application associated with the file's extension.

Examples: scr_useful_cmdlets

Page 9: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: VARIABLES

PowerShell Development Workshop 9

• Types of Variables

• Creating and Using Variables

Page 10: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: VARIABLES

PowerShell Development Workshop 10

User Created – These variables are created in the shell and in scripts and only persist for the session. We can create variables in scripts with global, script, or local scope.

Automatic – These variables keep the state of the PowerShell session and can not be modified directly. The values of this variables change as we execute and use the PowerShell session.

Preference – These variables store user preferences for PowerShell. These variables are created by PowerShell when a session is started and are populated with default values. We can change the values of these variables.

Environment – These variables are the variables set by the system for Command and PowerShell environments.

Page 11: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: USER CREATED VARIABLES

PowerShell Development Workshop 11

User Created – These variables are created in the shell and in scripts and only persist for the session. We can create variables in scripts with global, script, or local scope.

Examples:

$name = “Bryan” # Declares and assigns a value to an untyped variable.

[int]$amount = 200 # Declares an integer type variable and assigns a value.

Page 12: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: USER CREATED VARIABLES

PowerShell Development Workshop 12

[string] Fixed-length string of Unicode characters[char] A Unicode 16-bit character[byte] An 8-bit unsigned character

[int] 32-bit signed integer[long] 64-bit signed integer[bool] Boolean True/False value

[decimal] A 128-bit decimal value[single] Single-precision 32-bit floating point number[double] Double-precision 64-bit floating point number[DateTime] Date and Time

[xml] Xml object[array] An array of values[hashtable] Hashtable object

[object] Default type when no type specified.

Taken from http://ss64.com/ps/syntax-datatypes.html

• Blue indicates most frequently used types.

Examples: scr_variables and scr_declare_variables

Page 13: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: CREATING VARIABLES

PowerShell Development Workshop 13

• Create untyped and assign a value in one statement.$var1 = “some value”

• Create typed variable and assign value.[string]$var1 = “some value”

• Create variable with cmdlet. Gives you greater control over properties.New-Variable -Name var1 -Value "some value" -Scope private

• Assign value with cmdlet.Set-Variable –Name var1 –Value “some value”

• Remove a variable with cmdlet.Remove-Variable –Name var1

Page 14: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: ALIASES

PowerShell Development Workshop 14

• An alternate name for a cmdlet, function, or program.

• Used to create shortcuts and easier to remember names for commands.

• PowerShell has a number of predefined aliases.

• Some emulate DOS or Linux commands.

• We can create and maintain our own aliases with the following cmdlets.

• New-Alias – creates a new alias.

• Set-Alias – changes or creates a new alias.

• Get-Alias – gets information about aliases.

• Export-Alias – Saves aliases to csv file.

• Import-Alias – Loads aliases from a csv file.Example: scr_alias.ps1

Page 15: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: PROVIDERS

PowerShell Development Workshop 15

• Allow us to interact with a resource as if it were a physical network drive.

• Built-in providers include:• Alias• Variables• Registry• FileSystem• Function• Certificate• WSMan

• To see a list of installed providers use the Get-PSProvider cmdlet.

Example: scr_provider_demo.ps1

Page 16: PowerShell Workshop Series: Session 2

WORKSHOP LESSON 2: POWERSHELL DRIVES

PowerShell Development Workshop 16

• Simulates a network drive mapping but can be to any PowerShell provider.

• Is not limited to drive letters. Can be any name you want.

• Do not persist beyond the PowerShell session unless you use the –Persist parameter.

• Related cmdlets:

• New-PSDrive

• Get-PSDrive

• Remove-PSDrive

Example: scr_psdrives.ps1

Page 17: PowerShell Workshop Series: Session 2

REVIEW

PowerShell Development Workshop 17

• What cmdlets are.

• Parameters and some useful cmdlets

• Creating and using PowerShell variables.

• What Aliases are and how to use them.

• What PowerShell Providers are.

• What PowerShell Drives are and how to use them.

Page 18: PowerShell Workshop Series: Session 2

QUESTIONS?

PowerShell Development Workshop 18