here - The Database Guy

gayheadtibburInternet και Εφαρμογές Web

5 Φεβ 2013 (πριν από 4 χρόνια και 10 μήνες)

245 εμφανίσεις

Topic: Introduction to
PowerShell

Event: 380PASS 14
-
July
-
2009

Presenter: Chris Leonard

Copyright 2009 databaseguy.com. All rights reserved.

a bit about the presenter, and what to expect from this talk

A bit about me? I am …



Chris Leonard: chris@databaseguy.com



the SQL Server lead for Registrar Services at Go Daddy®



a Microsoft Learning SME on SQL 2005 / 2008

a bit about the presenter, and what to expect from this talk

What to expect from this talk? We will …



talk about what
PowerShell

is, obviously



give a quick tour of
PowerShell

basics



look at a few database
-
specific features



run lots of demos the whole time



still be done in an hour (or so)!

and how is it different from other shells?

A shell is a way to interact with an operating
system. There are several available for Windows:



The ever
-
present Windows Explorer interface



The ever
-
present and evil Command Prompt



Vbscript

and its cousins



Shells derived from the *nix world (
cygwin
, bash,
korn
, etc.)



Perl, Ruby, etc.

and how is it different from other shells?

So why did Microsoft go to the effort to build
another shell?

and why was it built anyway?

Two reasons for
PowerShell
:


1.
Command Prompt had several obvious
shortcomings, such as:



Limited control of flow in scripting


Inconsistent parsing rules (e.g. variables)


and why was it built anyway?

Two reasons for
PowerShell
:


2.
Desire for a shell better
-
suited to Windows:



Is it possible to improve upon the text
-
flow idioms used
by most other shells in a way that works very well
specifically for Windows?



and what are the big improvements it brings to Windows scripting?

PowerShell

addresses these two problems in
very strong ways:



Modern, well
-
structured, well
-
specified, and well
-
documented scripting constructs.



A full
-
blown object
-
oriented design based on the .Net
Framework.

if ($
slide.GetType
().
ToString
()
-
neq

"Boring" ) { Continue
-
Presentation }

PowerShell

is an object
-
oriented shell!



This is very cool.



It makes object properties very easy to work
with.



This has some deep advantages over text
-
based shells.

Get
-
Info | Where { $_.
isUseful


eq

$true }

The
builtin

commands in
PowerShell

are called
cmdlets (pronounced "command
-
lets").


Each
cmdlet

has a name that follows a Verb
-
Noun
convention. Examples:



Get
-
ChildItem


Get
-
Help


Set
-
Date


New
-
Alias

$impression = "ok, now we’re getting somewhere"

Variable names in
PowerShell

always begin
with "$".


Variables can be explicitly scoped. More info:



Get
-
Help
about_scope


Many operators can be used on variables. More info:



Get
-
Help
about_variable

get
-
slide
-
next |
foreach

{ $_.explain() }

Pipelining is the process of taking the output of
one command and passing (or "piping") it to
another command.


The special variable $_ is used to refer the current
object in the pipeline.


Since
PowerShell

is an object
-
oriented shell, our pipeline is
full of objects!

"
ls

and gm and dir, oh my!"

An alias is an alternate name for a command or
cmdlet
. There are several
builtin

aliases:



Cmdlet

abbreviations, such as:


"
gci
" for "Get
-
ChildItem
” and "
gc
" for "Get
-
Content"



Aliases based on other shells, such as:


"dir" and "
ls
" for "Get
-
ChildItem




Extreme shortcuts, such as:


"?" for "Where
-
Object” and "%" for "
ForEach
-
Object”

$topic = @{ "Name" = "Show me: arrays" ; "Topic" = "arrays" }

Arrays hold lists of objects, and
PowerShell

supports two types: basic & associative.


Basic arrays are indexed by an integer starting
with 0.


$
basicArray

= @(10,20,30,40);


# this will return 20:

$basicArray[1] # returns 20

$topic = @{ "Name" = "Show me: arrays" ; "Topic" = "arrays" }

Arrays hold lists of objects, and
PowerShell

supports two types: basic & associative.


Associative arrays are indexed by a value that you
specify.


$
empRecord

= @{ "
firstname
" = "
chris
" ; "
lastname
" =
"
leonard
" }

$
empRecord
["
firstname
"] # returns "
chris
"

$
empRecord.lastname

# returns "
leonard


CD HKLM:
\
Software
\
Microsoft
\
Windows
\
CurrentVersion

A drive letter is just a way to name a hierarchy
of files and folders, right?


What other hierarchies are present in your
computing environment?


What if we turned those into drives?

CD HKLM:
\
Software
\
Microsoft
\
Windows
\
CurrentVersion

PowerShell

lets you have "drives" that are not disk
drives. They can point at many object types,
including:



File system locations


Registry locations


Lists (variables, functions, aliases)


Environment variables


The certificate root store

CD HKLM:
\
Software
\
Microsoft
\
Windows
\
CurrentVersion

You can use the
builtin

providers to make your own
drive definitions.


Examples from my workstation:



drives that point at source code locations


drives that point at log file locations


drives that point to deployment scripts


a drive called servers
-
> files that have server names in them

function hello { "hello, world" }; hello;

Functions are one of the primary ways to write
useful, reusable scripts in
PowerShell
.


Functions allow you to group commands together
that are used together repeatedly to accomplish a
standard unit of work.

function hello { "hello, world" }; hello;

Scripts are just files that contain
PowerShell

commands.


The first script that
PowerShell

executes when you
start it is your profile script.


This is a good place to put function, alias, and drive
definitions that you'd like to in your environment each
time you run
PowerShell
.


function hello { "hello, world" }; hello;

You can also make other script files for specific
purposes. Some of my scripts:



Manage my source control


Create new projects and files from templates


Run specific queries on groups of SQL Servers


Deploy an entire project to a group of servers


Log my database interactions

”Random demos to make you think"

Some examples are from “
PowerShell

Cookbook”
by Lee Holmes. These scripts are available online:


http://examples.oreilly.com/9780596528492/



Some of his scripts need a little tweaking (in some cases
because they were written early in the
PowerShell

development cycle), but they show a lot of good ideas.


WMI Explorer is courtesy of http://www.thepowershellguy.com.

$
myJob.Automate
();

SQL Server 2005 / 2008 ships with some
PowerShell

support
builtin
.



drive providers


cmdlets

$
myJob.Automate
();

The SQL Server provider gives "drive letter" access
to:



Database Engine objects


Policy Management objects


Server automation registration


SQL Data Collection

$
myJob.Automate
();

SQL Server cmdlets allow you to interact with SQL
Server easily from a
PowerShell

prompt.


There are several SQL Server cmdlets, including:



Encode
-
SqlName


Decode
-
SqlName


Invoke
-
PolicyEvaluation


Invoke
-
SqlCmd


Stuff we don't have time for


Formatting
cmdlet

output


Filters


Operators


Instantiating COM objects


Explicitly instantiating .NET objects


Event log management


PowerShell

integration with Exchange

Recommended resources if you want to learn more


Download and install
PowerShell
.



Read the users guide that comes with
PowerShell
. No, I'm not kidding.

Recommended resources if you want to learn more


Free book, Google keywords: free
PowerShell

book Frank Koch



Retail book: "Windows
PowerShell

in Action" by
Bruce Payette



Retail book: "Windows
PowerShell

Cookbook"
by Lee Holmes (and study the free scripts!)


Recommended resources if you want to learn more


PowerShell

team blog:
http://
blogs.msdn.com/PowerShell
/



Marc van
Orsouw

(The
PowerShell

Guy) blog:
http://thepowershellguy.com



SQL PS initialization script:

http://www.idera.com/richardsworld/category/PowerShell.aspx

$Answers = Ask
-
Questions

Any questions?