Chris Green
Technology Specialist
Chris.Green@Microsoft.COM
- What is PowerShell
0.6
0.4
0.2
0
Nov'06 Dec'06 Jan'07 Feb'07 Mar'07 Apr'07 May'07 June'07
* The Bell Curve: Server-to-System Administrator Ratios First published on January 8, 2002
Exchange 2007 eq "Much Simpler"
>360 cmdlets are provided by the Exchange 2007 SnapIn http://technet.microsoft.com/en-us/library/bb124413.aspx
Exchange Server 2003 (VBScript) E2K7 (PowerShell one-liner)
Mailbox
Statistics
Set listExchange_Mailboxs =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\COMPUTERNAME\ROOT\MicrosoftExchangeV
2").InstancesOf("Exchange_Mailbox")
get-mailboxstatistics
For Each objExchange_Mailbox in listExchange_Mailboxs
server $servername
WScript.echo "AssocContentCount = + objExchange_Mailbox.AssocContentCount
WScript.echo " DateDiscoveredAbsentInDS = + objExchange_Mailbox.DateDiscoveredAbsentInDS
WScript.echo " DeletedMessageSizeExtended = + objExchange_Mailbox.
DeletedMessageSizeExtended
WScript.echo " LastLoggedOnUserAccount = + objExchange_Mailbox. LastLoggedOnUserAccount
WScript.echo " LastLogoffTime = + objExchange_Mailbox. LastLogoffTime
WScript.echo " LastLogonTime = + objExchange_Mailbox. LastLogonTime
WScript.echo " LegacyDN = + objExchange_Mailbox. LegacyDN
WScript.echo " MailboxDisplayName = + objExchange_Mailbox. MailboxDisplayName
WScript.echo " MailboxGUID = + objExchange_Mailbox. MailboxGUID
WScript.echo " ServerName = + objExchange_Mailbox. ServerName
WScript.echo " Size = + objExchange_Mailbox. Size
WScript.echo " StorageGroupName = + objExchange_Mailbox. StorageGroupName
WScript.echo " StorageLimitInfo = + objExchange_Mailbox. StorageLimitInfo
WScript.echo " StoreName = + objExchange_Mailbox. StoreName
WScript.echo " TotalItems = + objExchange_Mailbox. TotalItems
Next
- Get-Help
- Get-Command
- Get-Member
- Get-PSDrive
Shell
Finding Commands
PS> Get-Command i*
PS> Get-Command Noun Process
PS> Get-Command Type {Alias | Function | Filter |
Cmdlet | ExternalScript | Application | Script | All }
Powershell Online Help
- What is PowerShell
- Getting Started
- Language
- Architecture
- Execution Engine
- Pipeline
- Security Model
- Customization
- Output Format
- Personal Settings
- Developing cmdlets
-Scripting Examples...
- Some tips...
Consistency
-ErrorAction Enum. Determines how the cmdlet responds when an error occurs.
Values are: Continue [default], Stop, SilentlyContinue, Inquire.
-ErrorVariable String. Specifies a variable that stores errors from the command during processing.
This variable is populated in addition to $error.
-OutVariable String. Specifies a variable that stores output from the command during processing.
-OutBuffer Int32. Determines the number of objects to buffer before calling the next cmdlet in the pipeline.
-WhatIf Boolean. Explains what will happen without actually executing the command.
-Confirm Boolean. Prompts the user for permission before performing any action that modifies the system.
Powershell Common Parameters
Exploring Providers
Other forms of hierarchical storage exist in Windows
Registry
Environment
Certificates
PS> Get-PSDrive
PS> New-PSDrive -Name myDrive -PSProvider FileSystem -Root C:\myDir
PS> Remove-PSDrive -Name myDrive
Powershell PSDrive Providers
Cheat Sheet created by
Ben Pearce
The Language
Premier Field Engineer
Microsoft UK
The Language
The Language
The Language
The Language
The Language
- What is PowerShell
- Getting Started
- Language
- Architecture
- Execution Engine
- Pipeline
- Security Model
- Customization
- Output Format
- Personal Settings
- Developing cmdlets
-Scripting Examples...
- Some tips...
Architecture 375 cmdlets
Hosting Applications
(PowerShell.exe) PS CLI > _
Microsoft.PowerShell.Host, Security, Utility, etc Exchange 2007 PSSnapins
System.Management.Automation.Runspace (SMA.DLL)
.Net 2.0
netsh wmic CMD.exe cscript WShost VBscript PHP, Perl, etc...
Windows
Script / Parser - processes language constructs such as scripts, predicates, conditionals, etc.
Pipeline Processor manages inter-command communication via pipes
Command Processor manages command execution, registration and associated metadata
Session State variables and state accessible to script and cmdlets
Extended Type System a common interface to accessing properties, methods independent of
the underlying object type
Error Handler manages exception to error mapping and reporting
Architecture
Dev Doco here http://msdn2.microsoft.com/en-us/library/aa139691.aspx
AllSigned
- Scripts can run but requires a digital signature from a trusted publisher on all scripts
- Prompts you before running scripts from trusted publishers.
- Risks running signed, but malicious, scripts.
RemoteSigned
- Scripts can run but requires a digital signature from a trusted publisher on scripts
downloaded from the Internet.
- Does not require digital signatures on scripts run from the local computer.
- Does not prompt you before running scripts from trusted publishers.
- Risks running signed, but malicious, scripts.
Unrestricted
- Unsigned scripts can run but warns you that the file originated
from the Internet.
- Risks running malicious scripts.
The Security Model - Default
%windir%\system32\WindowsPowerShell\v1.0\profile.ps1
This profile applies to all users and all shells.
%windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
This profile applies to all users, but only to the Microsoft.PowerShell shell.
%UserProfile%\My Documents\WindowsPowerShell\profile.ps1
This profile applies only to the to the current user, but affects all shells.
%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
This profile ($profile) applies only to the current user and the Microsoft.PowerShell shell.
Customizing the User profile
Prompt
Function Prompt {
#Anything that returns a string
$(pwd) >
}
<TableColumnItem>
<ScriptBlock>[int]($_.WS / 1024)</ScriptBlock>
</TableColumnItem>
Customizing Output Formats
c:\demo\My-DotNetTypes.format.ps1xml
...
<TableHeaders>
<TableColumnHeader>
<Label>Window Title</Label>
<Width>40</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
...
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>MainWindowTitle</PropertyName>
</TableColumnItem>
Customizing Output Formats
3. Start PowerShell
Parameterize in
an informal script
Script against various sources:
Instrumentation: Text, COM, .NET, Cmdlets
Object types: WMI, XML, ADO, ADSI
Datastores: Filesystem, Registry, Certs, etc
function MyEcho
{
for ($i = 0; $i lt $args.count; $i++)
{
arg{0} = {1} f $i, $args[$i]
}
}
function Get-Total (
[String]$property = $(throw "Property Required"),
[string]$formatString = "Total {1} = {0}"
)
{ Begin
{ $total = 0
}
Process
{ $total += $_.$property
}
End
{ $formatString -f $total,$property
}
}
{
TRAP [System.Exception] {Write-Error ERROR: $_.Exception}
Itteration
PS> gps |foreach {$($_.Name): $($_.Handles)}
PS> gps |foreach { $l = 0 } { $l += $_.length } { $l }
Grouping
PS> gps |group Company
Measuring
PS> gps |Measure-Object Handles Sum Ave Min
Max
Filtering
PS> gps |Select name,id expand modules
PS> gps |Sort handles |Select first 10
Conditional selection
PS> gps |where {$_.handles ge 500}
Scripting
Here-strings
PS> @
A
Here-string
@
Scripting
Loose
PS> $d="4/20/2005
PS> Function foo() {$args[0]}
Strong
PS> [datetime]$d=4/20/2005
PS> function foo([datetime]$date) {$date}
PS> [int] [char] a
Extensible
PS> add-member $d Note Description "MMS TALK
PS> $d.Description
Common parameters
PS> get-process ErrorAction
{Continue*, Stop, SilentlyContinue, Inquire}
PS> get-process|stop-process WhatIf or -Confirm
PS> get-something Verbose or -Debug
Tracing
PS> start-transcript, stop-transcript
PS> set-PSDebug -Trace <0,1,2>
PS> set-PSDebug -Strict
Break points
PS> $host.EnterNestedPrompt()
$error
PS> $error
PS> get-process ErrorVariable myErrors
Powershell Debugging
Powershell WMI and Event Logs
A Big User: MySpace*
F5 Networks: Support use of Windows PowerShell to manage F5's BIG-IP network hardware.
/n Software: Network management, messaging and ability to remotely manage Windows via
Windows PowerShell.
PowerGadgets: Visualization product that allows users to run real-time Gadgets such as
charts, gauges and maps. Uses Windows PowerShell to allow users or administrators to easily
create Windows Vista Sidebar gadgets.
PowerGUI by Quest Software: Graphical user interface for executing Windows PowerShell
commands and writing scripts.
Remoting.
1:n (Fan-out):
Admin connects to 1000 server and performs a set of
management tasks capturing the results on the client machine.
n:1 (Fan-in):
Hosting model: Many users connects to one server to perform
their management tasks remotely.
1:1 Implicit invocation and Push/Pop-Runspace
Admins should be able to interact with the remote machine
seamlessly.
Async Job management.
Admins should be able to manage and schedule background jobs
locally and on the remote servers
V2 CTP Features (Continued)
Any or all of this could change before the V2 RTM. Participate and send your feedback through
http://blogs.msdn.com/powershell
Engine.
Debugging:
New tools to improve debugging such as setting
breakpoints, stepping, tracing and examining the session
state
Modules and Packages
Enabling deployment of scripts and cmdlets without the
need for registration .
SQM
Already in LHS.
Eventing
V2 CTP Features (Continued)
Any or all of this could change before the V2 RTM. Participate and send your feedback through
http://blogs.msdn.com/powershell
Hosting
Constrained Runspace:
Provide a mechanism for restricting the runspace-
configuration entities that are available for invocation to the
user based on some rules/configuration.
Delegated Admin support
The Blog
http://blogs.msdn.com/powershell
2007 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.