Download presentation
Presentation is loading. Please wait.
Published byMargaretMargaret Andrews Modified over 9 years ago
2
Kirk Jackson Senior Developer, Xero Wellington.NET Users Group SVR308
4
Kirk Jackson Senior Developer, Xero http://www.xero.com http://www.xero.com Wellington.NET User Group organiser http://www.dot.net.nz/wellington http://www.dot.net.nz/wellington Microsoft MVP, ASP.NET Blog http://pageofwords.com http://pageofwords.com kirk@pageofwords.com
5
UNC308 - Microsoft Windows PowerShell Scripting for Microsoft Exchange Server 2007 Johann Kruse Wed 15 Aug, 12:10-1:25pm SRV20 – Using PowerShell in Windows 2008 Hands-on lab Available throughout TechEd DEV311 - NET Framework 3.0 End-to-End: Putting the Pieces Together Matthew Winkler Wed 15 Aug, 3:45-5:00pm
6
What is Windows PowerShell? Getting Started Using PowerShell Scripting Alternate Data Sources
8
The best shell scripting environment for Windows
9
Objects Consistent Discoverable
10
New command line interface for Windows Scripting environment for Windows New programming language Microsoft’s new platform for Server & Product administration Designed for the Windows environment
11
COM Component Command Line Tool Admin Tool Graphical User Interface Admin Tool Graphical User Interface Windows or Server Product Functionality WMI Classes
12
ScriptsScripts Windows PowerShell CmdLets MMC Admin Tool Graphical User Interface MMC Admin Tool Graphical User Interface Windows or Server Product Functionality Microsoft.NET Framework
13
Exchange Server 2007 Recipients, storage, transport, policy, servers Underlies the GUI System Center Operations Manager System Center Data Protection Manager V2 System Center Virtual Machine Manager Install agents, explore objects, authoring, monitoring, alerts Third parties: F5 Networks, Quest,... Anything with WMI, COM,.NET interface
15
Exchange Server 2003 (VBScript) E2K7 (PowerShell one-liner) Mailbox Statistics Set listExchange_Mailboxs = GetObject("winmgmts:{impersonationLevel=impersonate}!\\COMPUTERNAME\ROOT\MicrosoftExchangeV 2").InstancesOf("Exchange_Mailbox") For Each objExchange_Mailbox in listExchange_Mailboxs 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 Nextget-mailboxstatistics –server $servername –server $servername Database Mgmt Dim StorGroup as New CDOEXM.StorageGroup StorGroup.DataSource.Open "LDAP://" + DCServer + "/ CN=First Storage Group,CN=InformationStore,CN=" + Server + ",CN=Servers,CN=First Administrative Group, CN=Administrative Groups,CN=First Organization, CN=Microsoft Exchange,CN=Services, CN=Configuration," + DomainName StorGroup.MoveLogFiles("C:\newlogPath", 0) move-storagegrouppath -identity “First Storage -identity “First Storage Group“ Group“ –log "C:\newlogPath” –log "C:\newlogPath” Recipient Mgmt Dim objMailbox As CDOEXM.IMailboxStore Set objMailbox = GetObject("LDAP://" + DCServer + "CN=FOO,CN=users," + DomainName) objMailbox.CreateMailbox "LDAP://" + DCServer + "/CN=Private MDB,CN=First Storage Group,CN=InformationStore,CN=" + Server + ",CN=Servers,CN=First Administrative Group, CN=Administrative Groups,CN=First Organization, CN=Microsoft Exchange,CN=Services, CN=Configuration," + DomainName enable-mailbox -identity domain\FOO -identity domain\FOO –database “First Storage –database “First Storage Group\Private MDB” Group\Private MDB” >360 cmdlets are provided by the Exchange 2007 SnapIn http://technet.microsoft.com/en-us/library/bb124413.aspxhttp://technet.microsoft.com/en-us/library/bb124413.aspx
16
Explore at the command line Find the correct command line(s) for the job Parameterize in an informal script Formalize in a production script
17
What is wrong with cmd.exe? Weak language Poor coverage / few utilities Insufficient / inflexible help Windows is a GUI What is wrong with bash / tcsh etc? Inconsistencies over the years Text-oriented Basic scripting languages, poor library support
18
$ ps -e | grep " p" | awk '{ print $1 }' | xargs kill vs PS> get-process p* | stop-process $ echo "this is a string" | tr '[a-z]' '[A-Z]' vs PS> "this is a string".ToUpper()
20
Pre-requisites: Windows Vista, Windows Server 2008, Windows XP SP2, Windows Server 2003.Net 2.0 Download from: http://www.microsoft.com/powershell Join the community News Group The Blog http://blogs.msdn.com/powershell Read the bundled documentation
21
No need to learn.NET Existing tools will all work No need to learn the PowerShell language Learn at your own pace Try using it instead of Command Prompt
22
Restricted All-Signed Remote-Signed Unrestricted help set-executionpolicy –detailed
25
Get-Command Get-Member Get-Help and -? Get-PSDrive
26
cmdlets Shell function commands Script commands Native windows commands command -parameter arg1 arg2 write-output -inputobject “hello”
27
129 provided cmdlets, you can add more > 360 cmdlets in Exchange 2007 SnapIn cmdlets can be built using C#, VB.NET Always named Verb-Noun e.g. get-content, get-childitem, sort-object, select-object, where-object cmdlets declare their parameters, the runtime parses them in a consistent way cmdlets can receive input from the pipeline
28
A piece of script code that has a name Lives in memory, temporarily Can have parameters, or just work on all arguments Can be entered interactively, or ‘dot- sourced’ (../myfunc.ps1 ) Can receive input from the pipeline
29
PowerShell code written to a.ps1 file Loaded from disk each time they are run Otherwise, the same as shell functions
30
Non-PowerShell / legacy commands Don’t use PowerShell naming standards, parameter parsing Only receive command line arguments If used in a pipeline, input is bundled up and sent as a string, output received as a string Runs in a seperate process
31
object = data + functionality Objects have properties, which contain the data for that object (e.g. Name, Length) Objects have methods, which define the functionality of that object (e.g. Delete, MoveTo) Examples: “a string” (String) 42 (Integer)
32
> first | second | third | fourth PipelinePipeline CommandCommand………… …… > first | second | third | fourth | out-default
33
No text, real objects are passed from one step to the next First cmdlet generates Subsequent cmdlets filter, sort, group, compare,... Streaming means objects are passed along the pipeline when they are generated All commands run in-process (except native Windows commands)
34
Aliases are defined for many cmdlets cd instead of set-location dir / ls instead of get-childitem Parameters: Positional parameters don’t need their name Type as little of the parameter name as possible When writing for other people, be sure to use expanded names
36
Get-Command Get-Member Get-Help and -? Get-PSDrive
37
> Get-Command i* > Get-Command –Noun Process > Get-Command –Type {Alias | Function | Filter | Cmdlet | ExternalScript | Application | Script | All }
38
List all the properties and methods of an object > dir | get-member > dir | select Name | get-member
39
Man-page style help on language and commands > Help > -? > Help About_While > Get-Help * |Where {$_.Synopsis -match "process"}
40
Expose alternate data sources as ‘drives’ for you to traverse > cd FUNCTION: > cd ENV: > ls
41
Commands with side-effects support: Whatif get-process | where {$_.handles –ge 500} | stop-process –WhatIf Confirm stop-process S* -Confirm Verbose stop-Process [a-x]*[q]*[r-t] -Verbose
42
WhatIf Confirm Verbose Debug ErrorAction ErrorVariable OutVariable OutBuffer
45
Only 13 keywords to learn: if (else, elseif) switch (default) for break do, while, until function, filter foreach
46
Maths: + - * / % as normal Assignment: =, +=, -=, *=, /=, %= Boolean: -eq, -ne, -lt, -gt, -le, -ge, –and –or, etc… Strings: -match, -notmatch, -like, -notlike Escape: backticks `n, `t, `r etc Line continuation: backtick ` Null value: $null Boolean: $true / $false
47
Variables in PowerShell are created when they are first assigned Unassigned variables act as if they contain $null $_ contains ‘current object’ (match, pipeline value, loop value) $args contains unbound arguments to a function The type of the variable determines what happens when you add, mult, etc.
48
$variable – a named variable $hash = @{ } – empty hash $hash[“key”] = $value $script = { … } – store a script &$script – run the script stored in $script Arrays are wrapped and unwrapped transparently (usually a good thing) Convention: cmdlets are named verb-noun (e.g. get-content) Convention: singular, not plural
50
$number = 7 $square = $number * $number
51
$number = 7 function square { $args[0] * $args[0] } square $number
52
$number = 7 function square ( [int]$number = 1 ) { $number * $number } square $number
53
function Get-Total ( [String]$property = $(throw "Property Required"), [string]$formatString = "Total {1} = {0}" ) { Begin { $total = 0 } Process { $total += $_.$property } End { $formatString -f $total,$property }
56
Data stores surfaced as “Drives” Filesystem, Registry, Alias, Certs, Env, Functions, Variables, etc. > Get-PSDrive > dir HKLM:\SOFTWARE\Microsoft Each drive provider exposes PSDrives Item, ChildItem, Content, ItemProperty, ACL, etc.
57
SharePoint drive provider: http://www.codeplex.com/PSSharePoint http://www.codeplex.com/PSSharePoint Users, roles, webs Active Directory Provider: http://www.codeplex.com/PowerShellCX/ http://www.codeplex.com/PowerShellCX/ SQL drive provider: http://tinyurl.com/2ycb66 http://tinyurl.com/2ycb66 Zip drive provider: http://tinyurl.com/23j9cp http://tinyurl.com/23j9cp None of these are ready for primetime
58
Windows Management Instrumentation Monitor and control system components Locally and remotely PowerShell allows access to WMI classes and wraps in PowerShell objects Get-WmiObject Win32_LogicalDisk Get-WmiObject Win32_Service Get-WmiObject win32_share
59
Component Object Model COM and.NET talk nicely PowerShell leverages this ability Use new-object –comobject to create PowerShell wraps in an object $ie = new-object -comobject InternetExplorer.Application $ie.Navigate("www.google.com")
60
XML can be loaded into PowerShell quite nicely Viewing the XML is as simple as traversing a tree of objects Modifying XML is not so pretty, and requires an understanding of.NET XML processing
61
PowerShell gadget http://andrewpeters.net/powershell-gadget/ http://andrewpeters.net/powershell-gadget/ Windows PowerShell graphical help file http://www.microsoft.com/downloads/details. aspx?familyid=3b3f7ce4-43ea-4a21-90cc- 966a7fc6c6e8 http://www.microsoft.com/downloads/details. aspx?familyid=3b3f7ce4-43ea-4a21-90cc- 966a7fc6c6e8 Codeplex - powershell community extensions http://www.codeplex.com/PowerShellCX/ http://www.codeplex.com/PowerShellCX/
63
Windows PowerShell Team blog: http://blogs.msdn.com/PowerShell http://blogs.msdn.com/PowerShell Windows PowerShell ScriptCenter http://www.microsoft.com/technet/scriptcent er/hubs/msh.mspx http://www.microsoft.com/technet/scriptcent er/hubs/msh.mspx http://www.codeplex.com/ShinyPower
64
New command line interface for Windows Scripting environment for Windows New programming language Microsoft’s new platform for Server & Product administration Designed for the Windows environment
65
UNC308 - Microsoft Windows PowerShell Scripting for Microsoft Exchange Server 2007 Johann Kruse Wed 15 Aug, 12:10-1:25pm SRV20 – Using PowerShell in Windows 2008 Hands-on lab Available throughout TechEd DEV311 - NET Framework 3.0 End-to-End: Putting the Pieces Together Matthew Winkler Wed 15 Aug, 3:45-5:00pm
68
© 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.