A Community of Learning
SUNGARD SUMMIT 2007 | sungardsummit.com 1
Microsoft PowerShell
Presented by: Jeff Modzel
March 22, 2007 Course ID 453
Agenda
• Introduction to PowerShell • PowerShell Power
• Developer Angle
A Community of Learning
SUNGARD SUMMIT 2007 | sungardsummit.com 3
What is PowerShell?
• New command-line shell and scripting language • Multiple perspectives for use:
• System Administrators • Developers
• Object based, not text based (.NET 2.0 objects)
• Can still use the regular, old .exe’s you’re used to! • Formerly code-named Monad
5
Course ID 453
Cmdlets (pronounced “command-lets”)
• Command that performs a single function • Operate on objects
• Use a verb-noun naming convention
• Can chain cmdlets together to do powerful things • About 130 built-in cmdlets
7
Course ID 453
More examples:
Get-command Shows all cmdlets installed in
the shell.
Get-content/set-content Reads a text file/writes a text
file.
Get-childitem Equivalent of dir in DOS, for the
file system
Get-process Retrieves the running
processes, equivalent to
tasklist.exe
Get-service Retrieves running services,
equivalent to sc.exe
9
Course ID 453
Scripting Support
• New scripting language, object oriented-like • Language for manipulating .NET objects. • Consistent environment for using cmdlets.
• Support complex tasks, without making simple tasks more complex.
• Consistent with higher-level languages used in .NET programming, such as C#.
Scripting Language
• Variables begin with a $ (variables have scope)
• Variables are objects and have properties/methods • Has all the constructs you’d expect from a modern
language: if/then, while, for, foreach, do, switch, trap/throw
• Scripts can be stored in script files (.ps1 extension) or even other variables.
A Community of Learning
SUNGARD SUMMIT 2007 | sungardsummit.com 11
Object Pipeline
• Pipes | connect cmdlets
• Pipes pass objects, not text
• Objects package together related information
Examples:
• get-process | format-list | out-file “c:\list.txt”
• get-service | where-object {$_.Status –eq “Running”} • get-childitem | sort-object length
• get-content “c:\list.txt” | select-object –first 5 • get-date | get-member
13
Course ID 453
General use cmdlets
• Filtering pipeline objects use where-object,
select-object, sort-object and group-object.
• Format pipeline objects for output (or other cmdlets) use list, table, wide and
Text File manipulation
• Use set-content, add-content and get-content to read, append and write text files.
• Can still use > and >> redirection
• Use import-csv and export-csv to read and write comma separated values files. Note: Excel can use CSV files. • Use import-clixml and export-clixml to read and write
15
Course ID 453
Variables
• Always begin with a $
• Can contain any other characters after $ but need to use curly braces
• No need to declare, just create and use • Have scope
• Variables are .NET objects
Examples: • $var = 1
• ${student name} = “Joe Smith” • $date = get-date
.NET, COM and WMI
• Can access .NET classes, COM objects and WMI with
new-object and get-wmiobject.
Examples:
• $wc = new-object –Typename System.Net.WebClient • $list = new-object System.Collections.ArrayList
• [System.IO.File]::Exists(“c:\junk.txt”)
• $wsh = new-object –ComObject WScript.Shell
• $fso = new-object –Com Scripting.FileSystemObject • $ie = new-object –Com InternetExplorer.Application • $excel = new-object –Com Excel.Application
17
Course ID 453
WMI continued…
• Windows Management Instrumentation (WMI) is an administration technology for Windows
Examples:
• get-wmiobject –class win32_computersystem • get-wmiobject win32_operatingsystem
• Get-wmiobject win32_logicaldisk
19
Course ID 453
Providers
• Provides connectivity to a type of resource (data store)
• File system • Registry • Alias
• Environment (windows environment) • Function
• Variable
• Certificate (digital certificates)
• PowerShell has it’s own concept of a drive – location for a data store.
Scripts
• PowerShell code can be saved in script files for reuse • Use a .ps1 extension
• Don’t forget - can call regular old .exe’s in script! • Type name of file minus the .ps1 extension and
interactive shell will search for the file in the environment Path variable. Or…
• Type full path name with the ps1 extension.
• Execute with a period . To run the script in the current scope.
• Interactive shell looks for first for an alias, then function, then cmdlet, then script and finally an external .exe.
• Execute from DOS\Batch\Windows “powershell.exe
21
Course ID 453
Customizing the shell environment
• Use a profile script to set aliases, functions, map drives or any other “startup” commands
• Profiles don’t exist by default
Profiles
• %windir%\system32\WindowsPowerShell\v1.0\profile.ps1
• This profile is loaded for all users.
• %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
• This profile is loaded for all users, and only for the default instance of PowerShell.
• %UserProfile%\My Documents\WindowsPowerShell\profile.ps1
• This profile is loaded per-user, and affects all versions of PowerShell which are installed.
• %UserProfile%\\My Documents\WindowsPowerShell\ Microsoft.PowerShell_profile.ps1
• This profile is loaded per-user, but only affects the default instance of PowerShell.
23
Course ID 453
Security
• Execution policy determines if user can load
configuration files, run scripts and/or require digital signatures for scripts.
• Integrated with Active Directory / Group Policy
• Policies are: Restricted, AllSigned, RemoteSigned and
Unrestricted
• Use get/set-executionpolciy to view and change • Can use certificates to sign scripts (see
SUNGARD SUMMIT 2007 | sungardsummit.com 24
25
Course ID 453
Additional things you can do with PowerShell
• Author cmdlets, providers and filters in Visual Studio • Create your own custom shell (typically for integration
with other applications)
• As of 2/7/07, need to install the Windows PowerShell SDK which is part of the Microsoft Windows SDK for
Steps to author a cmdlet
• Create Visual Studio project
• Add desired cmdlets and snap-in class • Compile to a .dll
• Register the .dll with installutil.exe (verify with
get-pssnapin –registered)
• Add snap-in to the shell with add-pssnapin YourSnapIn • Verify cmdlets have been added to the shell with
get-command
• Run new cmdlets!
• Can use export-console to save “state” of console for later reuse
27
Course ID 453
Creating a custom shell
• Use the make-shell.exe utility from the SDK
• Allows creation of customized shells – primarily for specialized snap-ins
SUNGARD SUMMIT 2007 | sungardsummit.com 28
29
Course ID 453
Where does it fit in Microsoft’s strategy?
• Strategic commitment to .NET, PowerShell is built on .NET
• Eventually become part of Windows (not for Vista or Longhorn, though)
• Exchange 2007 has PowerShell interface known as
Exchange Management Shell
Where to get it? Requirements?
• http://www.microsoft.com/technet/scriptcenter/topics/m sh/download.mspx
• 2 meg download but needs .NET 2.0
• OS requirements: Windows XP SP 2, Windows Server 2003, Vista and Longhorn
31 Course ID 453
Useful links
• Homepage • http://www.microsoft.com/windowsserver2003/technologie s/management/powershell/default.mspx• PowerShell Team Blog
• http://blogs.msdn.com/powershell/
• Other good PowerShell sites
• http://thepowershellguy.com/blogs/posh/
• http://www.microsoft.com/technet/scriptcenter/hubs/msh.
33
Course ID 453
Thank You!
Jeff Modzel
Please complete the online class evaluation form for:
Microsoft PowerShell
Course ID 453
SunGard, the SunGard logo, Banner, Campus Pipeline, Luminis, PowerCAMPUS, Matrix, and Plus are trademarks or registered trademarks of SunGard Data Systems Inc. or its subsidiaries in the U.S. and other countries. Third-party names and marks referenced herein are trademarks or registered trademarks of their respective owners.