330 likes | 535 Views
Techstravaganza 2010 PowerShell 101. Dmitry Kagansky Solutions Architect - Quest Software (Public Sector) dmitry.kagansky@quest.com http://www.idmwizard.com/ . Introduction & Administrative Items. Agenda Dmitry Kagansky PowerShell 101 Aaron PowerShell and Server Management Barry Gerdsen
E N D
Techstravaganza 2010PowerShell 101 Dmitry Kagansky Solutions Architect - Quest Software (Public Sector) dmitry.kagansky@quest.com http://www.idmwizard.com/
Introduction & Administrative Items • Agenda • Dmitry Kagansky • PowerShell 101 • Aaron • PowerShell and Server Management • Barry Gerdsen • PowerShell and Active Directory • PowerGUI • PowerShell Scripting & Debugging • Expectations
What is PowerShell? • Blah blah blah – you’ve heard it all before • However, these are some things to keep in mind • It is Object Oriented • What? • It is extensible • “Snap-ins” is the word of the day • It is baked into Windows • It is mandatory for some products • Exchange • It’s now up to version 2.0!
Unified and Standardized • Exchange • Get-Mailbox –Database 'VIP' | • Set-Mailbox –MaxReceiveSize 'unlimited' • Active Directory • Get-QADUser –City 'Aliso Viejo' | • Set-QADUser -PhoneNumber '949.754.8000' • VMware • Get-VM -Name web* | Set-VM –MemoryMB 1024
Where to begin? • “Blue is the new black” • The console
So how did I start? • I stumbled around and tried things • First new word • cmdlets • The only cmdlet I can reliably remember • Get-Help • save it for the lab • cmdlet format • Verb-noun • Aliases
cmdlets • Verb-Noun • No ‘real’ standard to separate out separate vendor cmdlets (Quest uses Verb-QNoun but another snap-in could break this) • I lied; I can also remember: • Get-Command
How It Works Collection of objects Get-Something filtering, etc. Collection of objects Do-Something
PowerShell for Reporting • Get-* to retrieve information on most objects • Sorting, Filtering, Grouping • Tables, Lists • Output to XML, CSV, HTML • On the fly calculations
Perfect for Bulk Operations • Bulk changes • Provisioning from csv • Snapshots • Learn to love the pipe • | • (and its red-headed step-children, the redirectors)
Natively Supports Modern Technology • HTML Reporting • CSV • XML • Web Services Get-QADUser | ConvertTo-Html –Property Name, Title
Natively Supports Modern Technology • HTML Reporting • CSV • XML • Web Services Get-Mailbox | Export-CSV c:\out.csv
Natively Supports Modern Technology • HTML Reporting • CSV • XML • Web Services $data = [XML] Get-Content c:\data.xml $data.chapter
Natively Supports Modern Technology • HTML Reporting • CSV • XML • Web Services $zip = New-WebServiceProxy –uri ` http://www.webservicex.net/uszip.asmx?WSDL
Variables, Arrays, and Hash Tables • Variables - allows us to store single bits of information • $firstName • Arrays - allows us to store information in a sequentially numbered index • $strComputers[x] • Hash Table - allows us to store in key/value pairs • (like Arrays but referenceable by name) • $strEmployeeID[“Dmitry K”]
Special Variables • $_: Contains the current pipeline object, used in script blocks, filters, and the where statement • $Args: Contains an array of the parameters passed to a function • $Error: Contains objects for which an error occurred while being processed in a cmdlet • $Home: Specifies the user’s home directory • $PsHome: The directory where the Windows PowerShell is installed • I give up – just run: • _ Get-Help about_automatic_variables
Conditional Logic • Comparing data • if/else/elseif If (this –eq boring) {surf-web} else {pay-attention}
Looping • Looping is the key to batch work • Common loop constructs • do while: - Script block executes as long as condition value = True • while: Similar to “do while” • do until: Script block executes until the condition value = True • for: Script block executes a specified number of times • foreach: Executes script block for each item in a collection or array
Output & Feedback • Lots of ways to do it • Write-Host • Out-File • Redirection • Lots of ways . . . . Get-Command *export* Get-Command *out* Get-Command *write*
Dissecting some code • From http://www.idmwizard.com/2009/05/19/delete-that-gpo-using-a-quest-product-of-course/ • Follow it with me
Dissecting some code import-csv deletelist.csv | % {& Delete-QGPO.ps1 $_.GPOName $_.GPMServer}
Dissecting some code ##################################################### # In an ideal world, this would be a cmdlet called: # Delete-QGPO GPOName [-GPMServer] ##################################################### # define the GPO name, which is what people # will probably know it as – this # can be an argument to a script later $gpoName = $args[0]; # Which GPM Server to export from $GPMHostname = $args[1]; $GPMPort = 40200; #####################################################
Dissecting some code & 'C:\Program Files\Quest Software\Quest Group Policy Manager\QGPMInit.ps1' -computerName $GPMHostname
Dissecting some code $foundGPO = $false ; # loop through all the objects in the data set and find the policy we want foreach($currentGPO in $VCManager.GetControlledObjects("GPO") | Where-Object {$_.Name -eq $gpoName}) { $foundGPO = $true; # check out the GPO so we can edit it # you can discard the contents # returned since we want a previous version $VCManager.Delete($currentGPO.VCId, "Deleting GPO - bye bye"); Write-Output “deleting GPO $gpoName"; }
Dissecting some code if ($foundGPO -eq $false) { Write-Output "GPO $gpoName not found”; }
Dissecting some code: The whole script ##################################################### # In an ideal world, this would be a cmdlet called: # Delete-QGPO GPOName [-GPMServer] [-GPMPort] ##################################################### # define the GPO name, which is what people will probably know it as – this # can be an argument to a script later $gpoName = $args[0]; # Which GPM Server to export from $GPMHostname = $args[1]; $GPMPort = 40200; ##################################################### & 'C:\Program Files\Quest Software\Quest Group Policy Manager\QGPMInit.ps1' -computerName $GPMHostname $foundGPO = $false ; # loop through all the objects in the data set and find the policy we want foreach($currentGPO in $VCManager.GetControlledObjects("GPO") | Where-Object {$_.Name -eq $gpoName}) { $foundGPO = $true; # check out the GPO so we can edit it # you can discard the contents returned since we want a previous version $VCManager.Delete($currentGPO.VCId, "Deleting GPO - bye bye"); Write-Output “deleting GPO $gpoName"; } if ($foundGPO -eq $false) { Write-Output "GPO $gpoName not found”; }
Dissecting some code: The whole script $gpoName = $args[0]; $GPMHostname = $args[1]; $GPMPort = 40200; & 'C:\Program Files\Quest Software\Quest Group Policy Manager\QGPMInit.ps1' -computerName $GPMHostname $foundGPO = $false ; foreach($currentGPO in $VCManager.GetControlledObjects("GPO") | Where-Object {$_.Name -eq $gpoName}) { $foundGPO = $true; $VCManager.Delete($currentGPO.VCId, "Deleting GPO - bye bye"); Write-Output “deleting GPO $gpoName"; } if ($foundGPO -eq $false) { Write-Output "GPO $gpoName not found”; }
Remember this? - Unified and Standardized • Exchange • Get-Mailbox –Database 'VIP' | • Set-Mailbox –MaxReceiveSize 'unlimited' • Active Directory • Get-QADUser –City 'Aliso Viejo' | • Set-QADUser -PhoneNumber '949.754.8000' • VMware • Get-VM -Name web* | Set-VM –MemoryMB 1024
PowerShell 2.0 – What’s new • See http://support.microsoft.com/kb/968929 for details • An update of not just PoSh but the entire Windows Management Framework • Remoting • run commands on one or more remote computers from a single computer • Integrated Scripting Environment • run interactive commands and edit and debug scripts in a graphical environment. • Modules • partition and organize Windows PowerShell code in self-contained, reusable units. • Advanced functions • Advanced functions are functions that have the same capabilities and behavior as cmdlets but not written in compiled C#. • Background jobs • allows for running a command or expression asynchronously and "in the background" without interacting with the console. • Eventing • adds support for listening, forwarding, and acting on management and system events. • Script internationalization • enables scripts to display messages that is specified by the UI culture setting on the user's computer. • Script debugging • New features were added to Windows PowerShell that let you do more during debugging. • New cmdlets • introduces over 100 built-in cmdlets.
Additional Info Special user group pricing on PowerGUI Pro ($99) • http://www.quest.com/TechStrav2010
Resources • MSDN PowerShell Blog • http://blogs.msdn.com/powershell/ • Good Starting Tutorial • http://www.powershellpro.com/powershell-tutorial-introduction/ • PowerGUI Community • http://www.powergui.org • Free Tools from Quest • Look for PowerGUI and the AD cmdlets at: • http://www.quest.com/free-tools