• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Powershell Tech Ed2009
 

Powershell Tech Ed2009

on

  • 1,338 views

 

Statistics

Views

Total Views
1,338
Views on SlideShare
1,338
Embed Views
0

Actions

Likes
0
Downloads
38
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Get-HelpGet-CommandGet-ProcessGet-Service
  • Get-HelpGet-CommandGet-ProcessGet-Service
  • Get-HelpGet-CommandGet-ProcessGet-Service
  • Get-HelpGet-CommandGet-ProcessGet-Service

Powershell Tech Ed2009 Powershell Tech Ed2009 Presentation Transcript

  • Govindaraj Rangan Technology Strategist Microsoft India
  • Agenda Introduction to Windows Powershell Scripting with Windows Powershell Working with Objects (WMI, COM, .NET) Scripting Best Practices
  • Agenda Introduction to Windows Powershell Scripting with Windows Powershell Working with Objects (WMI, COM, .NET) Scripting Best Practices
  • Windows Powershell - Overview Interactive Shell Rich Scripting Environment Object Oriented Extensible Secure More than everything, EASY! 
  • Architecture MMC Snap-In Interactive Shell Scripts Windows PowerShell Cmdlets Platform and Application Interfaces such as Functionality WMI, ADSI as COM and .NET Objects
  • 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
  • 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
  • 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”
  • Introducing Windows Powershell Few Commonly Used CmdLets
  • Agenda Introduction to Windows Powershell Scripting with Windows Powershell Working with Objects (WMI, COM, .NET) Scripting Best Practices
  • 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”
  • 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
  • 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- } Switch –regex|-wildcard (variable) { “.*?” , #do something- }
  • Functions function take_all_args { write-host $Args[0] } function take_sp_args (*string+$label = “t”) { # do something }
  • 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) Scripting Best Practices
  • 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
  • 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 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;
  • 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-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”);
  • 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
  • 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
  • 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/
  • © 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.