James Boother Blog: INTRODUCTION TO POWERSHELL
AGENDA A brief history lesson Don’t be scared everything’s familiar What’s new in 2.0? Scripting IDE’s Profiles Cmdlets and Variables Providers Resources
IN THE BEGINNING…
AFTER YEARS OF RESEARCH POSH WAS BORN
HOW DO I GET POWERSHELL INSTALLED? PowerShell 2 Pre-Installed on Windows 7 and Windows Server 2008 R2 Can be installed on Windows XP and Windows Server 2003 & 2008 Download from - Windows Management Frameworkhttp://support.microsoft.com/kb/968929
YOU CAN USE FAMILIAR COMMANDS Demo
WHAT’S NEW IN VERSION 2? Remoting Integrated Scripting Environment Modules Advanced functions Background jobs Eventing Script internationalisation Script debugging New cmdlets
SCRIPTING IDES Notepad++ Microsoft’s Interactive Scripting Environment (Part of PowerShell 2) PowerGui Visual Studio VSX – Adam Driscoll
NOTEPAD ++
MSFT INTERACTIVE SCRIPTING ENVIRONMENT
POWERGUI.ORG
VISUAL STUDIO VSX
GETTING HELP Get-Help commandlet F1 in ISE Autocomplete in PowerGui & VSX Visual Search on Bing
POWERSHELL PROFILES A PowerShell script that is run by the PowerShell host on launch I’ve created a global profile to be run by all users and all hosts Create C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 Why? Add Visual Studio Environment Variables Add SQL Server Environment Variables Load Dlls into memory
CMDLETS Main building block of PowerShell Scripts Mini Commands that perform one action Common naming convention: Verb then Noun e.g.: Get-Help, Get-Object, Remove-Item Actually.NET Classes that can easily be created if new functionality is required The output of one Cmdlet can be piped into further cmdlets using the | operator Destructive CmdLets have –WhatIf parameter
REALTIME LOG FILE MONITORING Demo
VARIABLES Defined by a name prceeded by a dollar sign (“$”) When working in the pipeline (“$_”) is the current item A single equals character is used for assignment e.g. $a = 1 or $a = $b Equality test with expressions such as –eq –ne –gt –ge –lt –le –like –and -or Strings in double quotes automatically replace variables with their value. To prevent this from happening use single quotes ‘Some test $no replacement here” If you want to include carriage returns format your string like this
PROVIDERS Providers are.net libraries. The SQL Server team created a provider ProviderDescription Alias ProviderProvides access to the Windows PowerShell aliases and their values Certificate ProviderProvides read-only access to X509 certificate stores and certificates Environment ProviderProvides access to the Windows environment variables FileSystem ProviderProvides access to files and directories Function ProviderProvides access to the functions defined in Windows PowerShell Registry ProviderProvides access to the system registry keys and values Variable ProviderProvides access to Windows PowerShell variables and their values WSMan ProviderProvides access to WSMan configuration information
NAVIGATING A DATABASE WITH SQL PROVIDER Demo
WMI SCRIPT Full access to WMI from within Powershell:
DELETING OLD BACKUP FILES
A FEW SQL MAINTENANCE SCRIPTS
SCHEDULING SCRIPTS SQL Server Agent or windows Task Scheduler
SUMMARY Powerful object based scripting engine Backwards compatible so you can start from what you already know Full access to File System; Registry; WMI; COM;.NET Objects and SQL Server Simple to schedule using Windows Scheduled Tasks or SQL Jobs Extensible through.NET
WEB RESOURCES Master-PowerShell | With Dr Tobias Weltner The Scripting Centre PowerShell owners manual PowerShell team blog Adam Driscoll – Visual Studio PowerGUI VSX - Setting SQL Environment Variables Powershell-outside-of-SQLPS.aspx
BOOKS
QUESTIONS If you have any questions now’s your chance…
THANK YOU Thank you for attending Automating admin tasks with PowerShell if you have any further questions please see my contact details here: Blog: Happy scripting!