Automating admin tasks with powershell

Download Report

Transcript Automating admin tasks with powershell

INTRODUCTION TO POWERSHELL
James Boother
Email: [email protected]
Twitter: @jimmyboo
Blog: http://www.boother.co.uk
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 http://support.microsoft.com/kb/968929 - Windows Management
Framework
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
•
Provider
Description
Alias Provider
Provides access to the Windows PowerShell aliases and their values
Certificate Provider
Provides read-only access to X509 certificate stores and certificates
Environment Provider
Provides access to the Windows environment variables
FileSystem Provider
Provides access to files and directories
Function Provider
Provides access to the functions defined in Windows PowerShell
Registry Provider
Provides access to the system registry keys and values
Variable Provider
Provides access to Windows PowerShell variables and their values
WSMan Provider
Provides access to WSMan configuration information
Providers are .net libraries. The SQL Server team created a provider
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 http://powershell.com/cs/blogs/ebook/default.aspx
•
The Scripting Centre http://technet.microsoft.com/en-gb/scriptcenter/dd742419.aspx
•
PowerShell owners manual http://technet.microsoft.com/en-us/library/ee221100.aspx
•
PowerShell team blog http://blogs.msdn.com/b/powershell/
•
Adam Driscoll – Visual Studio PowerGUI VSX http://visualstudiogallery.msdn.microsoft.com/01516103-d487-4a7e-bb40-c15ec709afa3/
•
Setting SQL Environment Variables
•
http://sqlblogcasts.com/blogs/martinbell/archive/2009/09/06/Using-SQL-Server-inPowershell-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:
• Email: [email protected]
•
Twitter: @jimmyboo
• Blog: http://www.boother.co.uk
•
Happy scripting!