Download presentation
Presentation is loading. Please wait.
1
Around the data centre in 80 scripts Richard Siddaway MVP Microsoft Practice Leader Centiq Ltd. Rsiddaway@centiq.co.uk
2
Introductions I do not work for Microsoft Opinions are MINE Microsoft Practice Leader at Centiq Interests are optimisation, migration and management Automation = Script wherever possible Worked with PowerShell since early betas Started and lead UK PowerShell User Group Blogging for 2 years PowerShell MVP Demos: Windows Server 2008 & PowerShell V2
3
QuestionsWho Uses PowerShell ? Has seen PowerShell ? Uses another scripting language ? Automates administration ? Attended February talk in Reading ?
4
Agenda What is PowerShell PowerShell version 2 Break Administering IIS 7 Administering SQL Server
5
Remote Administration Demo
6
WHAT IS POWERSHELL
7
Windows PowerShell As interactive and composable as BASH/KSH As programmatic as Perl/Python/Ruby As production oriented as AS400 CL/VMS DCL Allows access to data stores as easy as file system
8
Windows Server System Security Infrastructure IT Operations Infrastructure Applications Infrastructure Collaboration Infrastructure
9
PowerShell Automation engine Command Shell and scripting language.NET based DO NOT NEED TO LEARN.NET PROGRAMMING RTW November 2006 2 million+ downloads CTP 2 for Version 2 available since May
10
PowerShell – Key Features CmdletsProvidersExtensiblePipeline
11
The Difference is OBJECTS! Get-Process | Where { $_.handles –gt 500 } | Sort handles | Format-Table Get-Process Cmdlet Common Windows PowerShell Parser Windows PowerShell Pipeline Processor Where Cmdlet Sort Cmdlet Format Cmdlet
12
Script Types Text.NET “interpretation” of the traditional Unix scripting model COM WSH/VBScript style scripting.NET Manipulate any native.NET object Commands PowerShell cmdlets emitting objects
13
DataTypes Flat File – CSV etc.NETXMLWMIADSIADO.NETSQL
14
PowerShell adoption Microsoft Exchange 2007 Windows Server 2008 SC Data Protection Manager SC Operations Manager 2007 Compute Cluster SQL Server 2008 OCS Resource Kit Third party Special Operations Software Quest AD cmdlets PowerGUIPowerGadgetsSdmsoftware IBM Websphere MQ PowerShell Community Extensions
15
Issues Default install mode won’t run scripts Set-ExecutionPolicy No file association Can’t automatically run scripts or double click No remoting – coming in V2 Can use.NET and WMI Current working directory is NOT on PATH.\myscript.ps1 Does not load all.NET assemblies Use [Reflection.Assembly]::LoadWithPartialName(" Microsoft.SqlServer.Smo ") Slow load?
16
AliasesPlease: Do not use aliases in scripts or posts to forums, blogs or Do not use aliases in scripts or posts to forums, blogs or articles etc articles etc
17
PowerShell Basics Demo Interactive
18
POWERSHELL VERSION 2
19
PowerShell V2 It is a CTP!! It will change!! !!!!! DO NOT USE IN PRODUCTION !!!!!!! Remoting Background jobs Script Cmdlets Transactions – depend on provider Registry only Debugging Graphical PowerShell
20
Win Remote Management Jobs and Remoting Windows Remote Management Service WinRM WS-Management protocol Local AND remote machines Run as administrator Configure AND use
21
PowerShell RunSpaces Remoting Local or remote machine Creates PowerShell session Persistent connection Speeds response
22
PowerShell Jobs Asynchronous PSJob – get, receive, remove, start, stop, wait Start-Job or -AsJob Local or remote Receive-Job View results (& delete job ?)
23
PowerShell Remoting Need PowerShell local and remote Invoke-Command or some cmdlets Use a runspace Can work with jobs
24
Script cmdlet Script not compiled Variation on function Works on pipeline Cannot associate help file See get-help about_scriptcmdlet*
25
WMI Instruments to access management information Common Information Model V2 Windows 2000 onwards Namespaces and classes Local and remote machines
26
WMI in VBScript strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_Service",,48) For Each objItem in colItems Wscript.Echo "-----------------------------------" Wscript.Echo "Win32_Service instance" Wscript.Echo "-----------------------------------" Wscript.Echo "Name: " & objItem.Name Wscript.Echo "Status: " & objItem.Status Next
27
WMI in PowerShell Get-WMIObject Win32_Service | Select-Object Name, Status | Format-List Or even: gwmi win32_Service | select name, status | fl
28
WMI WMI Type Accelerators [WMI][WMIClass][WMISearcher] Improved support in V2 Get-WMIObject improved Invoke-WMIMethodSet-WMIInstanceRemove-WMIObject
29
PowerShell V2 issues CTP = changing Scripts may break Check release notes Cmdlet name clashes Especially with PSCX More complicated New keywords e.g. data
30
PowerShell V2 Demo
31
Break
32
ADMINISTERING IIS 7
33
IIS 7 and PowerShell Microsoft.Web.Administration New WMI provider root\webadministration Need PowerShell V2 for remote access Can use through.NET PowerShell provider Not remotely Remoting
34
IIS 7:.NET Like any managed code assembly M.W.A requires explicit load into PowerShell before use Do not forget to save your changes calling CommitChanges() No access to remote machines Credentials issues
35
IIS 7: WMI Needs packet privacy MUST give credentials to access remotely V1 Get-WMIObject cannot access V2 can Cannot create new objects remotely Alternatives: Access WMI via.NET Use remoting
36
IIS 7: WMI IIS 7.0 introduced a new WMI namespace called “WebAdministration” in Vista, and it was enhanced in SP1 and Windows Server 2008 WebAdministration is greatly simplified compared to IIS 6.0 WMI namespace (“MicrosoftIISv2”) WebAdministration is tightly integrated with IIS 7.0 configuration system, but provides more traditional set of objects: Site, Application, ApplicationPool, in addition to configuration sections
37
IIS 7: PowerShell provider Download from www.iis.net www.iis.net Provider and cmdlets Currently CTP 2 IIS and Config file admin
38
IIS 7: Legacy support The following options can be installed for IIS 7.0: IIS 6 Scripting Tools IIS 6 WMI Compatibility IIS Metabase Compatibility The goal behind these options is to allow existing ABO, ADSI, WMI code to continue to work on IIS 7.0 Legacy scripts can only update legacy settings Settings introduced for IIS 7.0 require the use of new APIs
39
Using IIS 7 through PowerShell Demo
40
ADMINISTERING SQL SERVER
41
SMO SQL Server Management Objects Programmatic management of SQL Server Extends and supersedes DMO DOES NOT support compatibility level 60, 65 i.e. SQL 7 and above!
42
SMO Object Model Server object is top level Partial Object Hierarchy Database File group Stored Procedure TableLogins Linked Server Settings
43
SMO usage examples Backup and Restore Database Object search Index sizes Database, and log file sizes Database defragmentation ManageDatabaseTables Database Users SQL Server information
44
SMO and PowerShell SMO part of SQL Server tool set WILL work against remote machines PowerShell does NOT load SMO assemblies Create server object as start point
45
Using SMO in PowerShell ## This uses the SMO assemblies to retrieve SQL Server version information ## ## load SMO assemblies ## use $null to prevent display of assembly load information $null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") $null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") $null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") clear-host
46
Using SMO in PowerShell ## read input file and test for version Import-Csv servers.csv | Foreach { $svr = $_.Server ## set SMO variable $Server = New-Object Microsoft.SqlServer.Management.Smo.Server($svr) $Server = New-Object Microsoft.SqlServer.Management.Smo.Server($svr) ## get version information $ver = $Server.Information.Properties | Where {$_.name -eq "VersionString"} $ver = $Server.Information.Properties | Where {$_.name -eq "VersionString"}
47
Using SMO in PowerShell switch ($ver.Value.SubString(0,9)) { "9.00.1399" {Write-Host $svr "SQL Server 2005 RTM"; break} "9.00.1399" {Write-Host $svr "SQL Server 2005 RTM"; break} "9.00.2047" {Write-Host $svr "SQL Server 2005 SP1"; break} "9.00.2047" {Write-Host $svr "SQL Server 2005 SP1"; break} "9.00.3042" {Write-Host $svr "SQL Server 2005 SP2"; break} "9.00.3042" {Write-Host $svr "SQL Server 2005 SP2"; break} default {Write-Host $svr "version cannot be determined"; break} default {Write-Host $svr "version cannot be determined"; break} }}
48
SQL Server 2008 Provider Auto install of PowerShell v1 on Windows Server 2008 PowerShell Provider SQL Server as a file system! Object management NOT data access Separate Shell SqlServerProviderSnapinSqlServerCmdletSnapin Can invoke from Management Studio
49
SQL Server 2008 Provider Based on SMO SQLServerSQLSQLPolicy Collection names are case sensitive! Databases SQL Agent supports PowerShell job steps
50
SQL Server 2008 Provider Specially compiled Shell Cannot add other PowerShell functionality Get\Remove\Add-PSSnapin not present New-Item not supported – can’t create! Remove-Item is supported Custom formatters Use Get-Item. to see system databases
51
SQL Server 2008 Provider SqlServerCmdletSnapin cmdlets Evaluate-Policy Invoke-Sqlcmd -Query "SELECT * FROM syslogins"
52
SQL Server 2008 Provider SqlServerProviderSnapin cmdlets Convert-UrnToPath Converts a SQL Server Management Object Uniform Resource Name (URN) to a SQL Server provider path. Encode-SqlName "Table:Test" returns the string "Table%3ATest". Decode-SqlName "Table%3ATest" returns "Table:Test".
53
Using SQL Server through PowerShell Demo
54
Summary/Call to Action PowerShell can be used to administer large and growing parts of your environment Now available at: www.microsoft.com/downloads www.microsoft.com/downloads Search for PowerShell Try it, Deploy it, Use it, Share
55
For More Information User group: http://www.get-psuguk.org.uk My Blog http://richardsiddaway.spaces.live.com Get-PSUGUK UK PowerShell User Group
56
Books
57
Books
58
Name: Richard Siddaway Email: Rsiddaway@centiq.co.uk Any Questions? TechEd EMEA: Ask the Experts
59
Reference Slides
60
Scripting with COM Access existing instrumentation Bind to COM objects $fso = New-Object -ComObject Scripting.FileSystemObject $m = [System.Runtime.InteropServices.Marshal] $word = $m::GetActiveObject("Word.Application") Invoke methods/access properties $fso.GetDrive(“C:”) $fso.VolumeName = “System Drive” Understand/extend instrumentation Extend and discover properties/methods Update-TypeData Office.Word.Types.ps1xml $fso | Get-Member Manipulate and format results Define and import custom formating Update-FormatData Office.Word.Format.ps1xml $word.RecentFiles | Sort name | Format-Table Allows more simpler/more powerful COM scripts because of utilities and formatting
61
Scripting with WMI PowerShell provides native WMI support Get-WmiObject Allows for inspection of WMI namespace Get-WmiObject –list [-Namespace xx] Get-WmiObject –Class xx –Namespace xx –Property xxx – Filter xxx –ComputerName xxx –Credential xxx Native language support [WMI] “\\JPSDESK10\root\cimv2:Win32_Process.Handle="0“ \\JPSDESK10\root\cimv2:Win32_Process.Handle="0 [WMICLASS] "ROOT\cimv2:WIN32_PROCESS" [WMISEARCHER]"select * from Win32_process WHERE Name = 'calc.exe'"
62
Scripting with.NET PowerShell provides native access to any.NET class Create any object [reflection.assembly]::LoadWithPartialName("System.Wind ows.Forms") $d = New-Object System.DateTime 2006,12,25 Access Properties/Invoke Methods $d.DayOfWeek$d.AddDays(-30) Access Statics [DateTime]::Now[DateTime]::IsLeapYear(2006) Allows admins to easily access and leverage a huge API set because of scriptability, utilities and formatting
63
Scripting with XML PowerShell provides native XML support Native datatype $x=[xml]"<a><b><c>TEST</c></b></a>“ $b =[xml](type c:\i386\mssecure.xml) Native syntax to access “data” view of properties $b.BulletinDataStore.Bulletins.Bulletin[0] Access to XML methods $b.BulletinDataStore.SelectNodes(“//Patch”) XML properties available through PSBase property $b.BulletinDataStore.PSBase.innerXml
64
Scripting with Text Invoke existing tools Existing command run directly after variables are expanded Harvest data from existing tools Parse output into variables using text utilities. Pipe data to SELECT and use –FIRST and –LAST Select-String Select-String Dir | Select-String Dir | Select-String [DateTime]”12/25/2006 7:00” ([DateTime]”12/25/2006 7:00”).AddDays(-30) Use functions/scripts to wrap the commands and convert output to objects or provide standard syntax Safely process text Use CLR types via Windows PowerShell to safely parse text [URI]” http://blogs.msdn.com/powershell/archive/2006/04/25/583234.aspx” Allows admins to get 2-10x more power out of existing commands because of scriptability
65
Active Directory AD cmdlets http://www.quest.com/activeroles- server/arms.aspx http://www.quest.com/activeroles- server/arms.aspxPowerGUI http://www.powergui.org Special Operations Software http://www.specopssoft.com/ SDMSoftware http://www.sdmsoftware.com/freeware.php
66
IIS 7 The following walkthroughs are available on the www.iis.net web site: www.iis.net PowerShell An Introduction to Windows PowerShell and IIS 7.0 http://www.iis.net/go/1212 http://www.iis.net/go/1212 Writing PowerShell Command-lets for IIS7 http://www.iis.net/go/1211 http://www.iis.net/go/1211 AppCmd Getting Started with AppCmd in IIS 7.0 http://www.iis.net/go/1222 http://www.iis.net/go/1222 Command Line Administration with IIS7 – AppCmd http://www.iis.net/go/954 http://www.iis.net/go/954
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.