Windows PowerShell  The Next Generation Command Line Scripting Essam Salah Team Leader  ITWorx http://esamsalah.blogspot.com/
Agenda Introduction What is Windows PowerShell? Using PowerShell How can I script with Windows PowerShell? PowerShell and .net How .net developers will benefit from PowerShell
Introduction What is PowerShell?
What is PowerShell Revolutionary new interactive shell and scripting language Based on .NET New set of built-in tools (+120) Can continue to use current tools Can continue to use current automation (COM) Allows access to data stores as  easy  to access  as file systems
Why? Try things out in an interactive shell Stitch things together with utilities Put the results in a script file  Generalize Clean it up    production-quality Be more Productive Secure by default
Where ? Supported on Windows XP and above Microsoft Products Exchange 2007 System Center (Family) 2007 New Management Architecture based on PowerShell Windows Server 2008 None Microsoft Products IBM WebSphere MQ 6 VMWare InfrastructureToolkit
MMC 3.0 Layered Over Windows PowerShell Command Line GUI Layered Over Your Application To Manage Windows PowerShell Engine Custom Application Microsoft Management Console 3.0 PSObject Windows PowerShell Cmdlets Windows Forms Early Bound Objects
Using PowerShell
How to start? PowerShell is a free download from Microsoft Separate download for each windows version
Explore Get-Help Get-Command > Get-Command i* > Get-Command –Noun Process Use Tab for Auto Complete
The basics Syntax:  verb-noun  –parameter   arguments Always singular, parameters starts with “ - ” Examples: > Get-Process –name ie* > Get-Process ie* > Get-Service > Read-Host “Please Enter a number”
The basics (cont’d) You can alias Set-Alias gps Get-Process Parameters can be positional gps –ProcessName w3wp gps w3wp Many parameters can be wildcarded gps c* Partial parameter names allowed Gps –p w3wp
Trusting Operations Commands with side-effects support: Whatif Stop-Service win* –WhatIf Confirm Stop-process S* -Confirm Verbose
Security is key for PowerShell By default, only interactive usage allowed If you want to use scripting, you need to change the  ExecutionPolicy Get-ExecutionPolicy / Set-ExecutionPolicy PS1 files opened with Notepad by design
Finding Data in Data Stores Data stores surfaced as “Drives” Filesystem, Registry, Alias, Certs, Env, Functions, Variables, etc. > Get-PSDrive > dir  HKLM:\SOFTWARE\Microsoft Rich common semantics > Dir  \logs –Include *.txt –Exclude A* -Recurse Tab-Completion in all Drives > Dir HKLM:\So<TAB>\Mi<TAB> => HLKM:\Software\Microsoft Demo 1 : File System Navigation Demo 2 : Registry Navigation Demo 3 : Environment Variables Navigation
Working with objects .NET All classes from .net framework WMI Access to your whole system and remote systems, too COM Microsoft Office Applications ActiveX controls
PowerShell and COM Demo 1 Using the Speech Class in Windows Vista Demo 2 Using Excel to list all services in the machine
PowerShell and WMI WMI Windows Management Instrumentation A way for System components to provide information and notification Demo Using WMI to get Physical Memory Size WMI Providers exist for most of Microsoft Products : SQL Server, BizTalk , CRM , SharePoint …
Commands Emit Objects Traditional text parsing replaced with direct object manipulation A default text view of objects is  dynamically computed Table for objects with < 5 properties List for objects with 5 or more properties
Explicit Object Formatting Built-in formatters for lists, tables, wide, and custom-views > Get-Command Format-* Formatters allow you to specify properties, propertysets and property expressions > gps |format-table name,id,handlecount > gps |format-table name,@{ Expression= {$_.mainmodule.filename}; Label=&quot;File&quot;} > gps |format-list name,*size64
Object Utilities Group  > gps |group Company Select > gps |Select name,id  > gps |Select –first 10 Where > gps |where {$_.handles –ge 500} Sort >Get-EventLog System -Newest 100 | group  Source | sort count
Uniform Data Access Syntax Provides a common user interface to objects  of different type systems XML > $x=[xml]&quot;<a><b><c>TEST</c></b></a>&quot; > $x.a.b.c No more Property bags $x.Handles vs. $x.Properties[“Handles”]
PowerShell Extensions 40+ Project for PowerShell on CodePlex PowerTab Power Gadget [DEMO] PowerShell Reflactor Add-in PowerShell Community Extensions  PowerShell Remoting Providers PowerShell Windows Mobile Provider PowerShell SharePoint Provider PowerShell SQL Server Analysis Services Provider PowerShell BizTalk Provider
PowerShell & .net
PowerShell is based on .net Everything in PowerShell is a .net object Reflection is every where > (Get-Process)[0] | Get-Member
Scripting with .net classes Any .net class ca be used Example $query = &quot;SELECT * FROM [Customers]“ $connString = &quot;server=.;integrated security;database=northwind“ $dataset = new-object &quot;System.Data.DataSet&quot; &quot;MyDataSet“ $da = new-object &quot;System.Data.SqlClient.SqlDataAdapter&quot; ($query, $connString) $da.Fill($dataset)
Develop your own Cmdlets Visual Studio Template for PowerShell
Call PowerShell Cmdlet from .net
Call PowerShell Cmdlet from .net
Q & A
Resources Technical Chats and Webcasts http://www.microsoft.com/communities/chats/default.mspx  http://www.microsoft.com/usa/webcasts/default.asp Microsoft Learning and Certification http://www.microsoft.com/learning/default.mspx MSDN & TechNet  http://microsoft.com/msdn http://microsoft.com/technet Virtual Labs http://www.microsoft.com/technet/traincert/virtuallab/rms.mspx Newsgroups http://communities2.microsoft.com/ communities/newsgroups/en-us/default.aspx Technical Community Sites http://www.microsoft.com/communities/default.mspx User Groups http://www.microsoft.com/communities/usergroups/default.mspx

Powershell Seminar @ ITWorx CuttingEdge Club

  • 1.
    Windows PowerShell The Next Generation Command Line Scripting Essam Salah Team Leader ITWorx http://esamsalah.blogspot.com/
  • 2.
    Agenda Introduction Whatis Windows PowerShell? Using PowerShell How can I script with Windows PowerShell? PowerShell and .net How .net developers will benefit from PowerShell
  • 3.
  • 4.
    What is PowerShellRevolutionary new interactive shell and scripting language Based on .NET New set of built-in tools (+120) Can continue to use current tools Can continue to use current automation (COM) Allows access to data stores as easy to access as file systems
  • 5.
    Why? Try thingsout in an interactive shell Stitch things together with utilities Put the results in a script file Generalize Clean it up  production-quality Be more Productive Secure by default
  • 6.
    Where ? Supportedon Windows XP and above Microsoft Products Exchange 2007 System Center (Family) 2007 New Management Architecture based on PowerShell Windows Server 2008 None Microsoft Products IBM WebSphere MQ 6 VMWare InfrastructureToolkit
  • 7.
    MMC 3.0 LayeredOver Windows PowerShell Command Line GUI Layered Over Your Application To Manage Windows PowerShell Engine Custom Application Microsoft Management Console 3.0 PSObject Windows PowerShell Cmdlets Windows Forms Early Bound Objects
  • 8.
  • 9.
    How to start?PowerShell is a free download from Microsoft Separate download for each windows version
  • 10.
    Explore Get-Help Get-Command> Get-Command i* > Get-Command –Noun Process Use Tab for Auto Complete
  • 11.
    The basics Syntax: verb-noun –parameter arguments Always singular, parameters starts with “ - ” Examples: > Get-Process –name ie* > Get-Process ie* > Get-Service > Read-Host “Please Enter a number”
  • 12.
    The basics (cont’d)You can alias Set-Alias gps Get-Process Parameters can be positional gps –ProcessName w3wp gps w3wp Many parameters can be wildcarded gps c* Partial parameter names allowed Gps –p w3wp
  • 13.
    Trusting Operations Commandswith side-effects support: Whatif Stop-Service win* –WhatIf Confirm Stop-process S* -Confirm Verbose
  • 14.
    Security is keyfor PowerShell By default, only interactive usage allowed If you want to use scripting, you need to change the ExecutionPolicy Get-ExecutionPolicy / Set-ExecutionPolicy PS1 files opened with Notepad by design
  • 15.
    Finding Data inData Stores Data stores surfaced as “Drives” Filesystem, Registry, Alias, Certs, Env, Functions, Variables, etc. > Get-PSDrive > dir HKLM:\SOFTWARE\Microsoft Rich common semantics > Dir \logs –Include *.txt –Exclude A* -Recurse Tab-Completion in all Drives > Dir HKLM:\So<TAB>\Mi<TAB> => HLKM:\Software\Microsoft Demo 1 : File System Navigation Demo 2 : Registry Navigation Demo 3 : Environment Variables Navigation
  • 16.
    Working with objects.NET All classes from .net framework WMI Access to your whole system and remote systems, too COM Microsoft Office Applications ActiveX controls
  • 17.
    PowerShell and COMDemo 1 Using the Speech Class in Windows Vista Demo 2 Using Excel to list all services in the machine
  • 18.
    PowerShell and WMIWMI Windows Management Instrumentation A way for System components to provide information and notification Demo Using WMI to get Physical Memory Size WMI Providers exist for most of Microsoft Products : SQL Server, BizTalk , CRM , SharePoint …
  • 19.
    Commands Emit ObjectsTraditional text parsing replaced with direct object manipulation A default text view of objects is dynamically computed Table for objects with < 5 properties List for objects with 5 or more properties
  • 20.
    Explicit Object FormattingBuilt-in formatters for lists, tables, wide, and custom-views > Get-Command Format-* Formatters allow you to specify properties, propertysets and property expressions > gps |format-table name,id,handlecount > gps |format-table name,@{ Expression= {$_.mainmodule.filename}; Label=&quot;File&quot;} > gps |format-list name,*size64
  • 21.
    Object Utilities Group > gps |group Company Select > gps |Select name,id > gps |Select –first 10 Where > gps |where {$_.handles –ge 500} Sort >Get-EventLog System -Newest 100 | group Source | sort count
  • 22.
    Uniform Data AccessSyntax Provides a common user interface to objects of different type systems XML > $x=[xml]&quot;<a><b><c>TEST</c></b></a>&quot; > $x.a.b.c No more Property bags $x.Handles vs. $x.Properties[“Handles”]
  • 23.
    PowerShell Extensions 40+Project for PowerShell on CodePlex PowerTab Power Gadget [DEMO] PowerShell Reflactor Add-in PowerShell Community Extensions PowerShell Remoting Providers PowerShell Windows Mobile Provider PowerShell SharePoint Provider PowerShell SQL Server Analysis Services Provider PowerShell BizTalk Provider
  • 24.
  • 25.
    PowerShell is basedon .net Everything in PowerShell is a .net object Reflection is every where > (Get-Process)[0] | Get-Member
  • 26.
    Scripting with .netclasses Any .net class ca be used Example $query = &quot;SELECT * FROM [Customers]“ $connString = &quot;server=.;integrated security;database=northwind“ $dataset = new-object &quot;System.Data.DataSet&quot; &quot;MyDataSet“ $da = new-object &quot;System.Data.SqlClient.SqlDataAdapter&quot; ($query, $connString) $da.Fill($dataset)
  • 27.
    Develop your ownCmdlets Visual Studio Template for PowerShell
  • 28.
  • 29.
  • 30.
  • 31.
    Resources Technical Chatsand Webcasts http://www.microsoft.com/communities/chats/default.mspx http://www.microsoft.com/usa/webcasts/default.asp Microsoft Learning and Certification http://www.microsoft.com/learning/default.mspx MSDN & TechNet http://microsoft.com/msdn http://microsoft.com/technet Virtual Labs http://www.microsoft.com/technet/traincert/virtuallab/rms.mspx Newsgroups http://communities2.microsoft.com/ communities/newsgroups/en-us/default.aspx Technical Community Sites http://www.microsoft.com/communities/default.mspx User Groups http://www.microsoft.com/communities/usergroups/default.mspx

Editor's Notes