Powershell Seminar @ ITWorx CuttingEdge Club


Published on

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

Published in: Business, Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • INF210
  • Powershell Seminar @ ITWorx CuttingEdge Club

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