6. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Why PowerShell?
PowerShell was designed to do for Windows what the UNIX shells dooes for UNIX:
provide a powerful, well-integrated command-line experience for the operation system.
But better :)
Unlike most scripting languages, the basic object model for PowerShell is .NET
PowerShell has full access to all of the types in the .NET framework and not a few simple
objects
Windows is mostly managed through objects comming from
MI (WINDOWS MANAGEMENT INFRASTRUCTURE)
COM (COMPONENT OBJECT MODEL)
and .NET
As Windows moves from the desktop to server farms or application servers (like print,
DNS and LDAP services,etc.) command-line automation becomes a fundamental
requirement.
7. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Who has PowerShell?
Its installed out of the box on Windows 7 and Windows Server 2008 R2.
It has also been released for older platforms:
Windows XP SP3
Windows Server 2003 SP2
Windows Vista SP1
and Windows Server 2008
8. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
PowerShell Concepts
- Cmdlets
These are built-in commands written in a .NET language like C# or Visual Basic.
Typically Developers can extend the set of cmdlets by writing and loading PowerShell snap-ins.
Functions
Functions are commands written in the PowerShell language.
These can be developed without an IDE like Visual Studio by sysadmins and devs
Scripts
Scripts are textfiles on disk with a .ps1 extension
Applications (aka native commands)
Applications are existing windows programs.
9. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Providers
Most applications have some sort of "hierarchical" data store
Example are Exchange, Active Directory, SharePoint, SQL Server
for example in sql server multiple instances of sql server can be installed
Once you are in an instance you can select jobs, roles, etc or databases
In a specific database you can choose triggers, logfiles, etc and tables
Providers in PowerShell allow data to be presented from different data stores in a
consistent fashion to existing cmdlets.
There are 7 providers built into PowerShell (Alias, Environment, File System, Function,
Registry, Variable, and Certificate).
10. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Pipeline
UNIX scripting shells also use the concept of a pipeline.
This is extremely powerful, but suffers from the disadvantage that, in most cases, the
pipelined data is just raw text.
This means prayer-based parsing. Rather than passing text, PowerShell passes .NET
objects. That means a cmdlet can use .NET reflection to look inside what is getting passed,
and know what's being passed
12. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
DEMO
============================================================
==== DEMO START based on video 1 (http://www.idera.com/Promo/Practical-PowerShell/)
============================================================
*. To see everything PS can do run
> get-command
*. to get all the manuals
> help
*. get a list of all process :
> get-process
*. Piping |
Default there is "Out-Default" , per defalt it outputs everything as text
> get-process | out-default
*. Cleaning the scree
> cls
*. Lets get the top 10 memory consuming processess
> get-process | sort vm -descending | select -first 10
*. How did I know that vm was a property of a Service Object
Get-Service | get-member
* Comments in Powershell, block
<#
Now
is
the
time
to
use
PowerShell
#>
* Comments in Powershell, line
# Now
# is
# the
* Lets tweak the output
get-process | format-table name,id,responding,path
=> some columns are cut
* show help for format-table
* lets do something about the cut columns
get-process | format-table name,id,responding,path -autosize -wrap
* to learn about autosize and wrap
For more information, type:
> get-help Format-Table -detailed
* Focused help
> get-help Format-Table -examples
* lets write the report to a textfile
> Get-Process | Format-table name,id | Out-File c:temptest.txt
=> Not that we still use the pipe symbol and not > as we would do in good old dos
* Lets peek inside the event log
> Get-EventLog security -newest 50
* And take a look at it in Excel
> Get-EventLog security -newest 50 | Export-Csv c:temptest.csv
13. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Runspaces: Windows PowerShell hosting mechanism
The Windows PowerShell runtime can be embedded inside other applications (jargon: in a
PowerShell "runspace").
These applications then leverage Windows PowerShell functionality to implement certain
operations
This capability has been utilized by Microsoft Exchange Server 2007 :
As for Exchange Server 2007, the entire server was built with Windows PowerShell in mind. In fact,
the Exchange Management Interface was designed in such a way that all the mouse-clicks and
menu-clicks are actually calls to Exchange-PowerShell cmdlets.
This is Microsoft's direction by defining this in their common engineering criteria
In the future all Microsoft Applications running on the Windows platform are to be
PowerShell aware.
3th Party providers bundle PowerShell Support with their products as PS Snap-ins
15. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
PowerShell enabled applications
Application Version Cmdlets Provider Management GUI
Exchange Server 2007 402 Yes Yes
Windows Server 2008 Yes Yes No
Microsoft SQL Server 2008 Yes Yes No
System Center Operations Manager 2007 74 Yes No
System Center Virtual Machine Manager 2007 Yes Yes Yes
System Center Data Protection Manager 2007 Yes No No
Windows Compute Cluster Server 2007 Yes Yes No
Microsoft Transporter Suite for Lotus Domino[37] 08.02.0012 47 No No
Microsoft PowerTools for Open XML[38] 1.0 33 No No
IBM WebSphere MQ[39] 6.0.2.2 44 No No
Quest Management Shell for Active Directory[40] 1.1 40 No No
Special Operations Software Specops Command[41] 1.0 Yes No Yes
VMware Infrastructure Toolkit[42] 1.5 157 No No
Internet Information Services[43] 7.0 54 Yes No
Ensim Unify Enterprise Edition[44] 1.6 Yes No Yes
Windows 7 Troubleshooting Center[45] 6.1 Yes No Yes
Microsoft Deployment Toolkit 2010 Yes No No
[edit] SnapIns and hosts
16. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
A top overview of the features introduced in PowerShell 2
PowerShell Remoting: Using WS-Management, PowerShell 2.0 allows scripts and cmdlets
to be invoked on a remote machine or a large set of remote machines.
Background Jobs: Also called a PSJob, it allows a command sequence (script) or pipeline to
be invoked asynchronously. Jobs can be run on the local machine or on multiple remote
machines.
Transactions: Enable cmdlet and provider developers to perform transacted operations.
Script Debugging: It allows breakpoints to be set in a PowerShell script or function.
Eventing: This feature allows listening, forwarding, and acting on management and system
events.
Network File Transfer: Native support for prioritized, throttled, and asynchronous transfer
of files between machines using the Background Intelligent Transfer Service (BITS).
17. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Consistency and Discoverablity
One of the great things of PS is the consistency and discoverablity.
If you switch from one domain to another, lets say SQL Server to Active Directory, you can
predict what you can ask PowerShell of AD beceause you are used to SQL Server
18. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Save Tasks and Schedule
A task can be saved as a script and reused at a later time or even scheduled.
For example, if you are responsible for a server park of 10 (or 1000) servers and you are interested in
the temperature you can write out the script once, save it as a script and give it a list of the servers
you are interested in.
When new servers are added you just update this list and it will give you brief report of the data you
are interested in.
IMO graphical user interfaces have their place but the power you get if you can do everying from 1
central place is not to be underestimated.
19. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Why PowerShell? Why not C#?
First of all C# is not a scripting language. YOu need a compiler to make use of C#.
If you consider a system administrator managing 10+ web servers, he/she will never install
2 gigabytes of Visual Studio on every server to edit his C# utility.
If you have any utility that you are running on production system and want to make some
minor changes then you need to go back to your desktop where you have your source
code preferably Visual Studio installed, edit your C# program, recompile it and copy it back
to production system.
If you really want to write actual code in C#, you need to have some knowledge of OOPs
(object oriented programming) concept (how to define class? what are static methods?)
Even if you are writing a simple program you need to write at least main method? What
are class access rules, why to make this function public, or static etc?
20. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Sysadmin Challenges
To manage a network today, Administrators face a range of challenges with respect to
tools:
Wide range: Admins are required to be experts in a huge range of tools and scripts. That’s because,
thus far, no one tool, does everything.
GUI Scalability: a GUI is a great tool for performing a single operation on just one computer, but it
can be slow and tedious if you need to perform that same operation on 1,000 systems.
21. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Starting up PowerShell from SQL Server 2008
This is a locked down version of PowerShell so it exposes reproducible behavior.
22. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
* the "whatif" feature : PS> get-process p* | stop-process -
whatif
23. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
SharePoint 2010
STSADM is legacy, PowerShell is the replacement:
http://dmitrysotnikov.wordpress.com/2009/10/19/sharepoint-2010-cmdlet-reference/
24. 17 December, 2009
Windows PowerShell 2.0, By Tom Pester
Recommended books & resources
Publicly available : http://powershell.com/cs/blogs/ebook/
Publically available : free : http://www.idera.com/Promo/Practical-PowerShell/
Nice video on Powershell in SharePoint 2010 http://technet.microsoft.com/en-
us/sharepoint/ee518673.aspx
MSDN http://msdn.microsoft.com/en-us/library/cc281954.aspx
Start with http://www.amazon.com/Microsoft-PowerShell-Programming-Absolute-
Beginner/dp/1598638998/ref=sr_1_2?ie=UTF8&s=books&qid=1258411028&sr=8-2
Become an expert with http://www.amazon.com/Windows-Powershell-Action-Bruce-
Payette/dp/1932394907/ref=sr_1_3?ie=UTF8&s=books&qid=1258411028&sr=8-3
Microsoft SQL Server 2008 Administration with Windows PowerShell
http://www.amazon.com/Microsoft-Administration-Windows-Power-
ShellProgrammer/dp/0470477288/ref=sr_1_14?ie=UTF8&s=books&qid=1258411028&sr=8-14
Managing VMware Infrastructure with Windows PowerShell TFM
http://www.amazon.com/Managing-VMware-Infrastructure-Windows-
PowerShell/dp/0982131402/ref=sr_1_1?ie=UTF8&s=books&qid=1258411111&sr=8-1