powershell seminar @ itworx cuttingedge club

31
Windows PowerShell The Next Generation Command Line Scripting Essam Salah Essam Salah Team Leader Team Leader ITWorx ITWorx http://esamsalah.blogspot.com/

Upload: essam-salah

Post on 02-Nov-2014

5 views

Category:

Business


1 download

DESCRIPTION

PowerShell Presentation conducted in ITWorx Cutting Edge Club 10-June-2008

TRANSCRIPT

Page 1: Powershell Seminar @ ITWorx CuttingEdge Club

Windows PowerShell The Next Generation Command Line Scripting

Windows PowerShell The Next Generation Command Line ScriptingEssam SalahEssam SalahTeam Leader Team Leader ITWorxITWorx

http://esamsalah.blogspot.com/

Page 2: Powershell Seminar @ ITWorx CuttingEdge Club

AgendaAgenda

IntroductionIntroductionWhat is Windows PowerShell?What is Windows PowerShell?

Using PowerShellUsing PowerShellHow can I script with Windows PowerShell?How can I script with Windows PowerShell?

PowerShell and .netPowerShell and .netHow .net developers will benefit from How .net developers will benefit from PowerShellPowerShell

Page 3: Powershell Seminar @ ITWorx CuttingEdge Club

IntroductionIntroduction

What is PowerShell?What is PowerShell?

Page 4: Powershell Seminar @ ITWorx CuttingEdge Club

What is PowerShellWhat is PowerShell

Revolutionary new interactive shell and Revolutionary new interactive shell and scripting languagescripting language

Based on .NETBased on .NET

New set of built-in tools (+120)New set of built-in tools (+120)

Can continue to use current toolsCan continue to use current tools

Can continue to use current automation (COM)Can continue to use current automation (COM)

Allows access to data stores as Allows access to data stores as easyeasy to access to access as file systemsas file systems

Page 5: Powershell Seminar @ ITWorx CuttingEdge Club

Why?Why?

Try things out in an interactive shellTry things out in an interactive shell

Stitch things together with utilitiesStitch things together with utilities

Put the results in a script file Put the results in a script file

GeneralizeGeneralize

Clean it up Clean it up production-quality production-quality

Be more ProductiveBe more Productive

Secure by defaultSecure by default

Page 6: Powershell Seminar @ ITWorx CuttingEdge Club

Where ?Where ?

Supported on Windows XP and aboveSupported on Windows XP and above

Microsoft ProductsMicrosoft ProductsExchange 2007Exchange 2007

System Center (Family) 2007System Center (Family) 2007

New Management Architecture based on New Management Architecture based on PowerShellPowerShell

Windows Server 2008Windows Server 2008

None Microsoft ProductsNone Microsoft ProductsIBM WebSphere MQ 6IBM WebSphere MQ 6

VMWare InfrastructureToolkit VMWare InfrastructureToolkit

Page 7: Powershell Seminar @ ITWorx CuttingEdge Club

Windows PowerShell EngineWindows PowerShell Engine

Custom ApplicationCustom Application

Microsoft Management Console 3.0Microsoft Management Console 3.0

PSObjectPSObject

Windows PowerShell CmdletsWindows PowerShell CmdletsCommanCommand Lined Line

GUIGUI

MMC 3.0 Layered Over Windows MMC 3.0 Layered Over Windows PowerShellPowerShell

Layered Layered OverOver

Windows FormsWindows Forms

Early Bound ObjectsEarly Bound Objects

Your Your ApplicatiApplicati

onon

To ManageTo Manage

Page 8: Powershell Seminar @ ITWorx CuttingEdge Club

Using PowerShellUsing PowerShell

Page 9: Powershell Seminar @ ITWorx CuttingEdge Club

How to start?How to start?

PowerShell is a free download from PowerShell is a free download from MicrosoftMicrosoft

Separate download for each windows Separate download for each windows versionversion

Page 10: Powershell Seminar @ ITWorx CuttingEdge Club

ExploreExplore

Get-HelpGet-Help

Get-CommandGet-Command> Get-Command i*> Get-Command i*

> Get-Command –Noun Process> Get-Command –Noun Process

Use Tab for Auto CompleteUse Tab for Auto Complete

Page 11: Powershell Seminar @ ITWorx CuttingEdge Club

The basicsThe basics

Syntax: Syntax:

verb-noun verb-noun –parameter–parameter argumentsargumentsAlways singular, parameters starts with “Always singular, parameters starts with “--””

Examples:Examples:

> Get-Process –name ie*> Get-Process ie*> Get-Service> Read-Host “Please Enter a number”

Page 12: Powershell Seminar @ ITWorx CuttingEdge Club

The basics (cont’d)The basics (cont’d)

You can aliasYou can aliasSet-Alias gps Get-ProcessSet-Alias gps Get-Process

Parameters can be positionalParameters can be positionalgps –ProcessName w3wpgps –ProcessName w3wp

gps w3wpgps w3wp

Many parameters can be wildcardedMany parameters can be wildcardedgps c*gps c*

Partial parameter names allowedPartial parameter names allowedGps –p w3wpGps –p w3wp

Page 13: Powershell Seminar @ ITWorx CuttingEdge Club

Trusting OperationsTrusting Operations

Commands with side-effects support:Commands with side-effects support:WhatifWhatif

Stop-Service win* –WhatIfStop-Service win* –WhatIf

ConfirmConfirmStop-process S* -ConfirmStop-process S* -Confirm

VerboseVerbose

Page 14: Powershell Seminar @ ITWorx CuttingEdge Club

Security is key for PowerShellSecurity is key for PowerShell

By default, only interactive usage allowedBy default, only interactive usage allowed

If you want to use scripting, you need to If you want to use scripting, you need to change the change the ExecutionPolicyExecutionPolicy

Get-ExecutionPolicy / Set-ExecutionPolicyGet-ExecutionPolicy / Set-ExecutionPolicy

PS1 files opened with Notepad by designPS1 files opened with Notepad by design

Page 15: Powershell Seminar @ ITWorx CuttingEdge Club

Finding Data in Data StoresFinding Data in Data StoresData stores surfaced as “Drives”Data stores surfaced as “Drives”

Filesystem, Registry, Alias, Certs, Env, Functions, Variables, etc.Filesystem, Registry, Alias, Certs, Env, Functions, Variables, etc.

> Get-PSDrive> Get-PSDrive

> dir HKLM:\SOFTWARE\Microsoft> dir HKLM:\SOFTWARE\Microsoft

Rich common semanticsRich common semantics> Dir \logs –Include *.txt –Exclude A* -Recurse> Dir \logs –Include *.txt –Exclude A* -Recurse

Tab-Completion in all DrivesTab-Completion in all Drives> Dir HKLM:\So<TAB>\Mi<TAB> => HLKM:\Software\Microsoft> Dir HKLM:\So<TAB>\Mi<TAB> => HLKM:\Software\Microsoft

Demo 1 : File System NavigationDemo 1 : File System Navigation

Demo 2 : Registry NavigationDemo 2 : Registry Navigation

Demo 3 : Environment Variables NavigationDemo 3 : Environment Variables Navigation

Page 16: Powershell Seminar @ ITWorx CuttingEdge Club

Working with objectsWorking with objects

.NET.NETAll classes from .net frameworkAll classes from .net framework

WMIWMIAccess to your whole systemAccess to your whole systemand remote systems, tooand remote systems, too

COMCOMMicrosoft Office ApplicationsMicrosoft Office Applications

ActiveX controlsActiveX controls

Page 17: Powershell Seminar @ ITWorx CuttingEdge Club

PowerShell and COMPowerShell and COM

Demo 1Demo 1Using the Speech Class in Windows VistaUsing the Speech Class in Windows Vista

Demo 2Demo 2Using Excel to list all services in the machineUsing Excel to list all services in the machine

Page 18: Powershell Seminar @ ITWorx CuttingEdge Club

PowerShell and WMIPowerShell and WMI

WMIWMIWindows Management InstrumentationWindows Management Instrumentation

A way for System components to provide A way for System components to provide information and notificationinformation and notification

DemoDemoUsing WMI to get Physical Memory SizeUsing WMI to get Physical Memory Size

WMI Providers exist for most of Microsoft WMI Providers exist for most of Microsoft Products :Products :

SQL Server, BizTalk , CRM , SharePoint …SQL Server, BizTalk , CRM , SharePoint …

Page 19: Powershell Seminar @ ITWorx CuttingEdge Club

Commands Emit ObjectsCommands Emit Objects

Traditional text parsing replaced with Traditional text parsing replaced with direct object manipulationdirect object manipulation

A default text view of objects is A default text view of objects is dynamically computeddynamically computed

Table for objects with < 5 propertiesTable for objects with < 5 properties

List for objects with 5 or more propertiesList for objects with 5 or more properties

Page 20: Powershell Seminar @ ITWorx CuttingEdge Club

Explicit Object FormattingExplicit Object Formatting

Built-in formatters for lists, tables, wide, Built-in formatters for lists, tables, wide, and custom-viewsand custom-views

> Get-Command Format-*> Get-Command Format-*

Formatters allow you to specify properties, Formatters allow you to specify properties, propertysets and property expressionspropertysets and property expressions

> gps |format-table name,id,handlecount> gps |format-table name,id,handlecount

> gps |format-table name,@{> gps |format-table name,@{Expression= {$_.mainmodule.filename}; Expression= {$_.mainmodule.filename}; Label="File"}Label="File"}

> gps |format-list name,*size64> gps |format-list name,*size64

Page 21: Powershell Seminar @ ITWorx CuttingEdge Club

Object UtilitiesObject Utilities

Group Group > gps |group Company> gps |group Company

SelectSelect> gps |Select name,id > gps |Select name,id

> gps |Select –first 10> gps |Select –first 10

WhereWhere

> gps |where {$_.handles –ge 500}> gps |where {$_.handles –ge 500}

SortSort

>Get-EventLog System -Newest 100 | group Source | sort >Get-EventLog System -Newest 100 | group Source | sort countcount

Page 22: Powershell Seminar @ ITWorx CuttingEdge Club

Uniform Data Access SyntaxUniform Data Access Syntax

Provides a common user interface to Provides a common user interface to objects objects of different type systemsof different type systems

XMLXML> $x=[xml]"<a><b><c>TEST</c></b></a>"> $x=[xml]"<a><b><c>TEST</c></b></a>"

> $x.a.b.c> $x.a.b.c

No more Property bagsNo more Property bags$x.Handles vs. $x.Properties[“Handles”]$x.Handles vs. $x.Properties[“Handles”]

Page 23: Powershell Seminar @ ITWorx CuttingEdge Club

PowerShell ExtensionsPowerShell Extensions

40+ Project for PowerShell on CodePlex40+ Project for PowerShell on CodePlex

PowerTabPowerTab

Power Gadget [DEMO]Power Gadget [DEMO]

PowerShell Reflactor Add-inPowerShell Reflactor Add-in

PowerShell Community Extensions PowerShell Community Extensions

PowerShell RemotingPowerShell Remoting

ProvidersProvidersPowerShell Windows Mobile ProviderPowerShell Windows Mobile Provider

PowerShell SharePoint ProviderPowerShell SharePoint Provider

PowerShell SQL Server Analysis Services PowerShell SQL Server Analysis Services ProviderProvider

PowerShell BizTalk ProviderPowerShell BizTalk Provider

Page 24: Powershell Seminar @ ITWorx CuttingEdge Club

PowerShell & .netPowerShell & .net

Page 25: Powershell Seminar @ ITWorx CuttingEdge Club

PowerShell is based on .netPowerShell is based on .net

Everything in PowerShell is a .net objectEverything in PowerShell is a .net object

Reflection is every whereReflection is every where

> (Get-Process)[0] | Get-Member> (Get-Process)[0] | Get-Member

Page 26: Powershell Seminar @ ITWorx CuttingEdge Club

Scripting with .net classesScripting with .net classes

Any .net class ca be usedAny .net class ca be used

ExampleExample

$query = "SELECT * FROM [Customers]“

$connString = "server=.;integrated security;database=northwind“

$dataset = new-object "System.Data.DataSet" "MyDataSet“

$da = new-object "System.Data.SqlClient.SqlDataAdapter" ($query, $connString)

$da.Fill($dataset)

Page 27: Powershell Seminar @ ITWorx CuttingEdge Club

Develop your own CmdletsDevelop your own Cmdlets

Visual Studio Template for PowerShellVisual Studio Template for PowerShell

Page 28: Powershell Seminar @ ITWorx CuttingEdge Club

Call PowerShell Cmdlet Call PowerShell Cmdlet from .netfrom .net

Page 29: Powershell Seminar @ ITWorx CuttingEdge Club

Call PowerShell Cmdlet Call PowerShell Cmdlet from .netfrom .net

Page 30: Powershell Seminar @ ITWorx CuttingEdge Club

Q & AQ & A

Page 31: Powershell Seminar @ ITWorx CuttingEdge Club

ResourcesResourcesTechnical Chats and Webcastshttp://www.microsoft.com/communities/chats/default.mspx http://www.microsoft.com/usa/webcasts/default.asp

Microsoft Learning and Certificationhttp://www.microsoft.com/learning/default.mspx

MSDN & TechNet http://microsoft.com/msdnhttp://microsoft.com/technet

Virtual Labshttp://www.microsoft.com/technet/traincert/virtuallab/rms.mspx

Newsgroupshttp://communities2.microsoft.com/communities/newsgroups/en-us/default.aspx

Technical Community Siteshttp://www.microsoft.com/communities/default.mspx

User Groupshttp://www.microsoft.com/communities/usergroups/default.mspx