Govindaraj Rangan
Technology Strategist
Microsoft India
Agenda

 Introduction to Windows Powershell

 Scripting with Windows Powershell

 Working with Objects (WMI, COM, .NET)

 ...
Agenda

 Introduction to Windows Powershell

 Scripting with Windows Powershell

 Working with Objects (WMI, COM, .NET)

 ...
Windows Powershell - Overview

 Interactive Shell
 Rich Scripting Environment
 Object Oriented
 Extensible
 Secure
 More t...
Architecture

     MMC Snap-In       Interactive Shell          Scripts


                 Windows PowerShell Cmdlets


  ...
CmdLet Syntax
                                             Argument
                              Name             String
...
Powershell Security
 Powershell not associated with .PS1
    Doesn’t run script by Default
 Does not run scripts without a...
Common CmdLets
PS C:> Get-Command

CommandType     Name                Definition
-----------     ----                ----...
Introducing Windows Powershell
Few Commonly Used CmdLets
Agenda

 Introduction to Windows Powershell

 Scripting with Windows Powershell

 Working with Objects (WMI, COM, .NET)

 ...
Variables
 $ represents variable
    $txt = get-content “C:test.txt”
    Type determined based on usage
    Strong typing:...
Operators
 Arithmetic
    +, -, *, /, %
 Assignment
    =, +=, -=, *=, /=, %=
 Conditional
    -gt, -lt, -ge, -le, -ne, -e...
Constructs
If (condition) {
    # do something
} ElseIf {
    # do something
} Else {
    # do something
}
Constructs
For ($i = 0; $i –lt 10; $i++) {
  # do something
  if ($i –eq 5) {
      break
  }
}
Constructs
Foreach ($item in collection) {
  # Do something to the item
  if ($item –eq “a value”) ,
      break
  }
}
Constructs
Switch (variable) {
  “value1” , #do something-
  “value2” , #do something-
  “value3” , #do something-
}

Swit...
Functions
function take_all_args {
  write-host $Args[0]
}

function take_sp_args (*string+$label = “t”)
{
  # do somethin...
Build a script to identify IP addresses
that are currently in use in a given
subnet, using a simple ping test
Agenda

 Introduction to Windows Powershell

 Scripting with Windows Powershell

 Working with Objects (WMI, COM, .NET)

 ...
WMI Objects
   Windows Management Instrumentation (WMI)
    ◦ WMI is a core technology for Windows system administration
...
Working with WMI
   Get free hard disk space available
   Get the amount of RAM installed
COM Objects
   Create a COM object using New-Object
    ◦ > $xl= New-Object -ComObject Excel.Application
   Reflect agai...
Working with Excel – Make an Excel
report of Software Installed on a given
machine
.NET Objects
   Creating .Net objects
        $f = New-Object System.Windows.Forms.Form

   Inspecting properties-metho...
Getting user credentials using a dialog box
.NET Namespace: System.Windows.Forms
Agenda

 Introduction to Windows Powershell

 Scripting with Windows Powershell

 Working with Objects (WMI, COM, .NET)

 ...
Scripting Best Practices
  Use sensible variable names
  Indent within constructs
  Use Source Control
  Avoid aliases wit...
Powershell Resources

Powershell Download link:
http://www.microsoft.com/downloads/details.aspx?FamilyID=c913aeab-d7b4-4bb...
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be...
Powershell Tech Ed2009
Powershell Tech Ed2009
Powershell Tech Ed2009
Upcoming SlideShare
Loading in …5
×

Powershell Tech Ed2009

868 views
815 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
868
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
40
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Get-HelpGet-CommandGet-ProcessGet-Service
  • Get-HelpGet-CommandGet-ProcessGet-Service
  • Get-HelpGet-CommandGet-ProcessGet-Service
  • Get-HelpGet-CommandGet-ProcessGet-Service
  • Powershell Tech Ed2009

    1. 1. Govindaraj Rangan Technology Strategist Microsoft India
    2. 2. Agenda Introduction to Windows Powershell Scripting with Windows Powershell Working with Objects (WMI, COM, .NET) Scripting Best Practices
    3. 3. Agenda Introduction to Windows Powershell Scripting with Windows Powershell Working with Objects (WMI, COM, .NET) Scripting Best Practices
    4. 4. Windows Powershell - Overview Interactive Shell Rich Scripting Environment Object Oriented Extensible Secure More than everything, EASY! 
    5. 5. Architecture MMC Snap-In Interactive Shell Scripts Windows PowerShell Cmdlets Platform and Application Interfaces such as Functionality WMI, ADSI as COM and .NET Objects
    6. 6. CmdLet Syntax Argument Name String Verb Noun PS> get-service –name “*net*” Command Parameter Property Names Status Name DisplayName ------ ---- ----------- Stopped NetLogon NetLogon Running Netman Network Connections Property Values
    7. 7. Powershell Security Powershell not associated with .PS1 Doesn’t run script by Default Does not run scripts without a path You need to autograph your script Execution Policy Restricted, Allsigned, Remote-signed, Unrestricted Standard parameters like “-whatif”, “-confirm” to save you from making accidental changes Read-Host -assecurestring
    8. 8. Common CmdLets PS C:> Get-Command CommandType Name Definition ----------- ---- --------- Function A: Set-Location A: Cmdlet Add-Computer Add-Computer [[-ComputerName] <String[]>] [-Do... Cmdlet Add-Content Add-Content [-Path] <String[]> [-Value] <Objec... PS C:> Get-Help Get-Command PS C:> Get-Process PS C:> Get-Process | Where-Object {$_.CPU –gt 100} PS C:> Get-Service PS C:> Get-EventLog PS C:> $var = Read-Host PS C:> Write-Host $var PS C:> Restart-Computer –ComputerName “MYBOSSPC”
    9. 9. Introducing Windows Powershell Few Commonly Used CmdLets
    10. 10. Agenda Introduction to Windows Powershell Scripting with Windows Powershell Working with Objects (WMI, COM, .NET) Scripting Best Practices
    11. 11. Variables $ represents variable $txt = get-content “C:test.txt” Type determined based on usage Strong typing: [int] $n Constants: Set-Variable pi 3.14 –option Constant Arrays $arr = @(1,2,3). $arr[0] returns 1 Associative Arrays (Hashtables) $marks = @,ram=“100”;ravan=“0”- $marks.ram returns “100” $marks*“ram”+ returns “100”
    12. 12. Operators Arithmetic +, -, *, /, % Assignment =, +=, -=, *=, /=, %= Conditional -gt, -lt, -ge, -le, -ne, -eq, -contains Append i or c for case insensitive or sensitive operations String +, *, -replace, -match, -like
    13. 13. Constructs If (condition) { # do something } ElseIf { # do something } Else { # do something }
    14. 14. Constructs For ($i = 0; $i –lt 10; $i++) { # do something if ($i –eq 5) { break } }
    15. 15. Constructs Foreach ($item in collection) { # Do something to the item if ($item –eq “a value”) , break } }
    16. 16. Constructs Switch (variable) { “value1” , #do something- “value2” , #do something- “value3” , #do something- } Switch –regex|-wildcard (variable) { “.*?” , #do something- }
    17. 17. Functions function take_all_args { write-host $Args[0] } function take_sp_args (*string+$label = “t”) { # do something }
    18. 18. Build a script to identify IP addresses that are currently in use in a given subnet, using a simple ping test
    19. 19. Agenda Introduction to Windows Powershell Scripting with Windows Powershell Working with Objects (WMI, COM, .NET) Scripting Best Practices
    20. 20. WMI Objects  Windows Management Instrumentation (WMI) ◦ WMI is a core technology for Windows system administration ◦ It exposes a wide range of information in a uniform manner.  Get-WmiObject cmdlet  Listing WMI classes ◦ Get-WmiObject -List ◦ Get-WmiObject -list -ComputerName cflabsql01  Getting WMI objects ◦ Get-WmiObject -Class Win32_OperatingSystem ◦ Get-WmiObject -class Win32_LogicalDisk ◦ Get-WmiObject -Class Win32_Service | Select-Object -Property Status,Name,DisplayName
    21. 21. Working with WMI Get free hard disk space available Get the amount of RAM installed
    22. 22. COM Objects  Create a COM object using New-Object ◦ > $xl= New-Object -ComObject Excel.Application  Reflect against properties/methods ◦ > $xl |get-member  Access properties/methods ◦ > $xl.Visible = “True”  Drill down into Excel object model ◦ > $wb = $xl.Workbooks.Add() ◦ > $ws = $xl.Worksheets.Item(1) ◦ > $ws.Cells.Item(1,1) = quot;TEST“ ◦ > $ws.Cells.Item(1,1).Font.Bold = quot;True“ ◦ > $ws.Cells.Item(1,1).Font.Size = 24 ◦ $xl.Workbooks.Add().Worksheets.Item(1).Cells.Item(1,1) = quot;HELLOquot;
    23. 23. Working with Excel – Make an Excel report of Software Installed on a given machine
    24. 24. .NET Objects  Creating .Net objects  $f = New-Object System.Windows.Forms.Form  Inspecting properties-methods  $f|Get-Member  Accessing properties-methods  $f.Text = quot;Give me the username and password“  Adding Controls to the Parent Object (Form)  Create control object:  $OKButton = New-object System.Windows.Forms.Button  Add control to Parent:  $f.Controls.Add($OKButton)  Load any assembly and use its objects ◦ [Reflection.Assembly]::LoadFrom(“…abc.dll”);
    25. 25. Getting user credentials using a dialog box .NET Namespace: System.Windows.Forms
    26. 26. Agenda Introduction to Windows Powershell Scripting with Windows Powershell Working with Objects (WMI, COM, .NET) Scripting Best Practices
    27. 27. Scripting Best Practices Use sensible variable names Indent within constructs Use Source Control Avoid aliases within Scripts Use as descriptive comments as possible Use Functions and Script blocks to reduce the number of lines of code Test thoroughly for boundary conditions before running in production Capture and report all logical errors
    28. 28. Powershell Resources Powershell Download link: http://www.microsoft.com/downloads/details.aspx?FamilyID=c913aeab-d7b4-4bb1- a958-ee6d7fe307bc&displaylang=en Powershell Blog: http://blogs.msdn.com/powershell/
    29. 29. © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

    ×