CHAPTER 1 RI AL What Is PowerShell, and Why Do You Need It? MA TE HERE ARE THE TOPICS COVERED IN THIS CHAPTER: X WHY POWERSHELL? 2 D Overview of PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 TE The Power Behind PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 What About the Learning Curve? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 GH X WHAT’S NEW IN POWERSHELL 2.0? 7 RI PowerShell in the Enterprise .
CHAPTER 1 I T professionals have been looking for ways to automate and perform tasks in a consistent manner for years. There have been many techniques and technologies — from simple batch files to third-party tools — to accomplish the tasks. Some IT professionals have gone the extra step and learned developer languages, such as Visual Basic or JavaScript, to give their scripts more power. A majority of these tools were not integrated into the Microsoft environment.
In other words, your environment was running smoothly, you were busy, and you had no time to learn the language. You may have decided to wait to see whether there would be a version 2 and whether Microsoft was really serious about this language. Well, here we are with version 2, and PowerShell is getting better than ever. Microsoft and communities such as http://powershellcommunity.
CHAPTER 1 • W H AT I S P O W E R S H E L L , A N D W H Y D O YO U N E E D I T ? ▶ Do you need to collect all the critical and error events from the event logs of all your servers? F I G U R E 1.1 This is PowerShell. F I G U R E 1. 2 This is PowerShell too. PowerShell can do that. Once you learn the language, you should be able to perform these tasks faster than you have in the past.
as the registry, certificate store, and Windows Management Instrumentation (WMI). WMI is a core technology for Windows system administration, because it exposes a wide range of information in a uniform manner. PowerShell includes a cmdlet that allows you to interface with WMI objects, enhancing your ability to do real work. But isn’t PowerShell just a command-line tool? Yes, it is a command-line tool, but in most cases PowerShell can accomplish all the tasks that graphic management tools can.
CHAPTER 1 • W H AT I S P O W E R S H E L L , A N D W H Y D O YO U N E E D I T ? In many cases, a majority of the built-in roles and services (such as IIS or Active Directory) that you may run on your Windows Server 2008 R2 server have PowerShell providers and cmdlets to manage them. For example, the PowerShell Provider for Internet Information Services (IIS) 7.5 allows you to easily automate routine and complex IIS 7.
The formatting for commands that use the .NET Framework, COM objects, and WMI are slightly different from other scripting technologies, but in general those commands are simpler in PowerShell. If you are not familiar with scripting techniques, the base set of cmdlets is easy to learn, as you’ll see throughout this book. PowerShell provides an intuitive scripting language specifically designed for day-to-day administrative of servers.
CHAPTER 1 • W H AT I S P O W E R S H E L L , A N D W H Y D O YO U N E E D I T ? One of the new features in PowerShell 2.0 that allows IT administrators throughout the world to use PowerShell more easily is called internationalization. Internationalization enables PowerShell scripts to display messages in the language specified by the UI language setting on the user’s computer. Under the hood, this features queries the operating system of the user to determine what language is being used.
example, the following cmdlet would create and enter a new PowerShell session on Server2: Enter-PSSession -ComputerName Server2 Your results will look similar to Figure 1.4. F I G U R E 1. 4 Remote session Another key addition to PowerShell 2.0 is the ability to create and run background jobs. After you start a background job, you are returned almost immediately to your interactive PowerShell session.
CHAPTER 1 • W H AT I S P O W E R S H E L L , A N D W H Y D O YO U N E E D I T ? PowerShell with a GUI There was no built-in GUI in PowerShell 1.0, so you had only the command console for your PowerShell session. There were third-party tools you could use, such as PowerGUI (http://powergui.org/index.jspa). With PowerShell 2.0, Microsoft added new features to take advantage of the GUI.
debugging tools with PowerShell. The new debugging features allow you set breakpoints on the following: ▶ Lines ▶ Columns ▶ Variables ▶ Commands If you are using the debugger with your scripts, you can step into, over, and out of the scripts, and you can even display the call stack, often with a single keystroke. There are cmdlets to work with the debugger. You can also display the values of variables and run standard commands in the debugger. The ISE makes it easy to interact with the debugger.
CHAPTER 1 • W H AT I S P O W E R S H E L L , A N D W H Y D O YO U N E E D I T ? with the Get-PSBreakpoint cmdlet for any of your PowerShell scripts. Figure 1.8 shows an example of a debugging session from the PowerShell console. F I G U R E 1. 8 Debugging session in PowerShell To learn more about the ISE, see Chapter 2 and Chapter 5. Chapter 2 shows you how to install the ISE, which may not be installed by default. Chapter 5 shows how to use this tool when working with PowerShell scripts.
F I G U R E 1. 9 Out-Gridview 13 What Is PowerShell, and Why Do You Need It? POWE R S H E LL HAS SOM E TH I N G FO R E VE RYO N E CHAPTER 1 PowerShell Has Something for Everyone PowerShell has something for everyone, from IT professionals to developers to the casual scripter. PowerShell is a tool that can save you time and show you a new way to automate a task that was previously difficult or impossible. Unlocking PowerShell to meet your needs always starts with the basics.
CHAPTER 1 • W H AT I S P O W E R S H E L L , A N D W H Y D O YO U N E E D I T ? PowerShell needs to be installed on any system you want to be able to manage with PowerShell. PowerShell can be installed on many Microsoft operating systems (including XP mode on Windows 7). There are third-party PowerShell add-ons for non-Microsoft operating systems. Knowing what systems you want to manage allows you to determine which is the best path to deploy PowerShell.
them have their own set of PowerShell commands and functions to perform a variety of tasks specific to the particular role or feature. Beginning with basic installation of the roles and features on your server, PowerShell can be used to perform these functions for your full and core Windows Server 2008 R2 servers. Performing and scheduling a task such as a backup can be quickly created in a PowerShell script and tied to the Task Scheduler.
CHAPTER 1 • W H AT I S P O W E R S H E L L , A N D W H Y D O YO U N E E D I T ? that developers can work with, making it that much more powerful and beneficial in your workplace. PowerShell is another development platform you can use to automate many tasks via code. After you have the foundational knowledge presented throughout this book, looking at the programmatic side of PowerShell will allow you to take PowerShell to another level.
relatively easy to work with GUI technologies in PowerShell, there are also some tools to make this easy process even easier. Appendix F explores creating GUIs in PowerShell. EXERCISE 1: INVENTORY YOUR SCRIPTS 17 What Is PowerShell, and Why Do You Need It? POWE R S H E LL HAS SOM E TH I N G FO R E VE RYO N E CHAPTER 1 Take an inventory of the tasks you are currently using scripts to perform.