3. Agenda
Introduction to Windows Powershell
Scripting with Windows Powershell
Working with Objects (WMI, COM, .NET)
Scripting Best Practices
4. Agenda
Introduction to Windows Powershell
Scripting with Windows Powershell
Working with Objects (WMI, COM, .NET)
Scripting Best Practices
5. Windows Powershell - Overview
Interactive Shell
Rich Scripting Environment
Object Oriented
Extensible
Secure
More than everything, EASY!
6. Architecture
MMC Snap-In Interactive Shell Scripts
Windows PowerShell Cmdlets
Platform and Application
Interfaces such as
Functionality
WMI, ADSI
as COM and .NET Objects
8. 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
19. Build a script to identify IP addresses
that are currently in use in a given
subnet, using a simple ping test
20. Agenda
Introduction to Windows Powershell
Scripting with Windows Powershell
Working with Objects (WMI, COM, .NET)
Scripting Best Practices
21. 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
22. Working with WMI
Get free hard disk space available
Get the amount of RAM installed
23. 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;
24. Working with Excel – Make an Excel
report of Software Installed on a given
machine
25. .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”);
27. Agenda
Introduction to Windows Powershell
Scripting with Windows Powershell
Working with Objects (WMI, COM, .NET)
Scripting Best Practices
28. 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