
Windows PowerShell Simple commands, powerful administration Don Jones It’s been a long time coming, but Windows great language that has been frequent­ ly misused to create malicious scripts. PowerShell is almost ready to launch. That VBScript isn’t going away either, but you’ll likely find that Windows Power­ means it’s time for Windows administrators Shell is easier to use for many differ­ ent tasks. to start taking notice. Windows PowerShell You can do most anything with Win­ dows PowerShell that you would have offers what is perhaps the easiest and most done with Cmd.exe. For example, you can run ipconfig and you’ll get the flexible way to automate a wide variety same familiar output. But Windows PowerShell introduces a whole new of administrative tasks, making you more set of commands that aren’t external executable files. These cmdlets (pro­ efficient and more effective in your work. nounced ‘command­lets’) are built right into Windows PowerShell. (For a But even more importantly, Micro­ there are few reasons to continue us­ look at some of the most useful cmd­ soft is building the graphical admin­ ing Cmd.exe. lets to get you started using Windows istrative consoles of products like Using Windows PowerShell isn’t PowerShell, see the sidebar ‘Top 10 Exchange Server 2007 and System all that different than using Cmd.exe cmdlets to start using immediately’.) Center 2007 on top of Windows – except that Windows PowerShell is, All cmdlets are named with a stan­ PowerShell. This means you’ll be able well, more powerful. Like Cmd.exe, dard verb­noun format, making them to perform almost any administrative Windows PowerShell has a built­in easy to learn and remember. For in­ task from within Windows Power­ scripting language, although it is much stance, running the Get­Command Shell. Microsoft plans to do the same more flexible than Cmd.exe’s primitive cmdlet will list all the available cmdlets. with the administrative capabilities of batch language. How flexible? With Perhaps the most useful cmdlet for an more products over time. Thus, Win­ Windows PowerShell, you can auto­ administrator is Get­WMIObject. Say dows PowerShell could eventually mate extremely complex tasks with a you want to find out what service pack become the first all­purpose tool for language that includes only about a Server2 is running. Simply run: administering nearly any Microsoft half­dozen built­in keywords. Get-WMIObject Win32_OperatingSystem –Property server product. To help you get start­ Now that I’ve mentioned scripting, ServicePackMajorVersion –Computer Server2 ed, I’ll discuss Windows PowerShell I should probably touch upon securi­ regularly in this new column. Be sure ty. Windows PowerShell benefits from To discover this same information us­ to download a copy of the software at: what Microsoft has learned about se­ ing VBScript, you would have to write microsoft.com/powershell curity over the past decade plus. By several lines of code. Other cmdlets let default, Windows PowerShell won’t you work with services (Start­Service, Power and simplicity run scripts; it can only be used inter­ Stop­Service, and so forth), process­ As the name implies, Windows actively to run individual commands. es (Stop­Process and others), files PowerShell is a shell, not unlike the If you do enable scripting, you can di­ (Rename­Item, Copy­Item, Remove­ Command Prompt (Cmd.exe) that’s rect Windows PowerShell to run only been around since Windows NT 3.1. digitally signed scripts. All of this is to This column is based on a prerelease Cmd.exe isn’t going away, but with the help ensure that Windows PowerShell version of Windows PowerShell. All availability of Windows PowerShell, doesn’t become the next VBScript – a information herein is subject to change. 68 To get your FREE copy of TechNet Magazine subscribe at: www.microsoft.com/uk/technetmagazine 68_71_PowerShell_UK_desFIN.indd 68 27/3/07 14:04:26 Item, Move­Item, for example), and Administrators can you can save the computer name into much more. Many of these cmd­ a String variable and use the Replace lets even have shortcut names, called now work directly method to replace backslashes with aliases. In the case of Get­WMIObject, empty strings, as shown here: you can just type gwmi. Running Get­ with rich objects [string]$c = Read-Host “Enter computer name” Alias will provide you with a list of $c = $c.Replace(“\”,””) Get-WMIObject Win32_OperatingSystem these shortcut names. –Property ServicePackMajorVersion right within a text­ –Computer $c Why object oriented matters based shell The value for the –Computer pa­ Built on the Microsoft .NET Frame­ rameter has been provided in the $c work, Windows PowerShell is com­ variable. That variable was initially cre­ pletely object­oriented. Usually, only a PowerShell so capable. Using this char­ ated as a string, so it picked up all the software developer would get excited acter, you can pass (or pipe) objects functionality of the .NET Framework about that, but in this case, the object from one cmdlet to another, allowing String type, including the Replace orientation results in a huge time sav­ each to further refine the results, for­ method. Of course, learning about all ings for administrators. This is because mat them for display, and so on. This of these capabilities will take a while, administrators can now work directly mechanism works because every cmd­ but you should find them easy to with rich objects right within a text­ let returns one or more objects, rather pick up through examples. Windows based shell. Consider this example: than pure text, giving subsequent cmd­ PowerShell itself helps to simplify Get-Process | Sort-Object pm –desc | Select-Object –first 10 lets the full object to work with. learning. For example, if you type $c = This is just a single line with three The use of objects in Windows $c. (don’t forget the full stop) and press different cmdlets separated by pipes PowerShell is pervasive, all the way Tab, Windows PowerShell will display (more on this in a moment). The first down to its variables. And you don’t Clone(), the first method of the String cmdlet retrieves all running process­ have to declare variables up front; you type. If you keep pressing Tab, Windows es and then passes, or pipes, those can just start using them by placing a PowerShell will cycle through all the objects to Sort­Object. This second dollar sign ($) before a variable name. available methods. Essentially, when cmdlet sorts on each process object’s While it’s not required, you can also you do this, Windows PowerShell is pm (meaning Physical Memory) prop­ tell Windows PowerShell what type of showing you what it knows how to do erty, placing them into descending or­ data you want to put into the variable. with a String! der. The sorted collection of process That lets Windows PowerShell map Here’s a tougher task. Read a list of objects is then piped to Select­Object, the variable to one of the extreme­ computer names from a file, with one which picks the first 10 and displays ly powerful .NET Framework types, name per line, and show each comput­ them. The result? This simple line dis­ giving you a lot of additional built­in er’s service pack number. In VBScript, plays the top 10 physical memory con­ functionality. For example, suppose this task would require a dozen or sumers on the machine, as shown in you want to prompt for a comput­ more lines of code. In Cmd.exe, you Figure 1. This is an extremely efficient er name and retrieve the service pack would have to use a complicated batch way to take a quick look when doing version from that computer, but you file. In Windows PowerShell, this task some troubleshooting. don’t know if the person typing in takes just one line: The use of pipes (the vertical line the computer name will include two Get-Content “c:\computers.txt” | foreach { $_; gwmi Win32_ character typically located on the backslashes (such as \\Server2). Since OperatingSystem -prop ServicePackMajorVersion -comp $_ } backslash key on US keyboards) is an you know that the Get­WMIObject The Get­Content cmdlet reads the essential part of what makes Windows cmdlet doesn’t need the backslashes, contents of C:\Computers.txt. Each line of the file becomes an object in its own right. This collection of objects – computer names, that is – is piped to the foreach command, which is real­ ly just an alias for the ForEach­Object cmdlet. The commands inside the curly braces are repeated once for each Figure 1 Using a object that is piped in – for this exam­ simple cmdlet to ple, that means they run once for each troubleshoot computer name. The special $_ vari­ TechNet Magazine April 2007 69 68_71_PowerShell_UK_desFIN.indd 69 27/3/07 14:04:26 running. Once you close the shell, the function vanishes. You can copy the Top 10 cmdlets to start using immediately function into your Windows Power­ Get-Command retrieves a list of all available cmdlets. Shell profile, which is a sort of auto­ Get-Help displays help information about cmdlets and concepts. run script that executes each time Get-WMIObject retrieves management information by using WMI. Windows PowerShell starts. Doing so Get-EventLog retrieves Windows event logs. would make the function available in Get-Process retrieves a single or list of active processes. every Windows PowerShell window Get-Service retrieves a Windows service. you open. Or, if you want, you can Get-Content reads in text files, treating each line as a child object. make the function into a standalone Add-Content appends content to a text file.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages4 Page
-
File Size-