Windows 7 Resource Kit- P10

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:50

0
54
lượt xem
2
download

Windows 7 Resource Kit- P10

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'windows 7 resource kit- p10', công nghệ thông tin, hệ điều hành phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: Windows 7 Resource Kit- P10

  1. FIgURE 13-4 The dir command produces a directory listing of files and folders . To create a directory, you can use the md command and supply the name of the directory you need to create . As soon as a directory is created, you can create a text file by using the redirection arrows to capture the results of a command, such as the dir command that was used earlier . These results are shown in Figure 13-5 . FIgURE 13-5 To create a new directory, use the md command . No feedback is displayed in Windows PowerShell when creating a file by redirection . The text file that was created in the previous command is shown in Figure 13-6 . Introduction to Windows PowerShell Scripting CHapTER 13 403 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  2. FIgURE 13-6 The text file of a directory listing created by using the redirection operator The last thing that might have to be done is to delete a text file and a folder . To do this, you use the del command (the Windows PowerShell alias for the Remove-Item cmdlet) to delete both the file and the folder . The first thing that you might need to do is to change your working directory to the C:\HsgTest folder that was created earlier in this chapter via the md command (see Figure 13-5) . To do this, you use the cd command . After you are in the directory, you can obtain another directory listing by using the dir command . Next, you use the del command to delete the Directory .txt file . As shown in Figure 13-7, the file name is preceded by the “ .\” charac- ters . This means that you are interested in the file in the current directory . When you type the first few letters of the file name and press the Tab key, “ .\” is added to the file name automati- cally as the complete file name is expanded . This enables you to avoid typing the complete file name . The feature, known as a tab expansion, is a great time saver . FIgURE 13-7 Use the del command to delete a file or a folder . 404 CHapTER 13 Overview of Management Tools Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  3. Using the pipeline to Read Text Files A common scripting task faced by IT professionals is reading text files . This usually involves using a script similar to the SearchTextFileForSpecificWord .vbs script . In the SearchTextFileForSpecificWord .vbs script, you create an instance of the Scripting.FileSystemObject, open the file, and store the resulting TextStream object in the file variable . You then use the Do…Until…Loop statement to work your way through the text stream . Inside the loop, you read one line at a time from the text stream . As soon as you find a specific line, you use the InStr statement to see whether you can find a specific word . If it does, you display the sen- tence to the screen . The SearchTextFileForSpecificWord .vbs script is shown here . SearchTextFileForSpecificWord.vbs filepath = "C:\fso\testFile.txt" word = "text" set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.OpenTextFile(filepath) Do Until file.AtEndOfStream line = file.ReadLine If InStr(line, word) Then WScript.Echo line End If Loop The technique of using the ReadLine method is very efficient, and it is the recommended way to work with large files from within VBScript . The other way of reading content from a text file in VBScript is the ReadAll method . The problem with using the ReadAll method is that it stores the contents of a text file in memory . This is not a problem if the file is small, but for a large file, it consumes a large amount of memory . In addition to the memory consumption issue, if you plan on working with the file one line at a time, which is one of the main reasons for reading a text file, you now have to figure out artificial methods to work your way through the file . With the ReadLine method and the TextStream object, you stream the file and it never is stored in memory . The TextStream object from VBScript is similar to pipelining in Windows PowerShell . With Windows PowerShell, you do not have to write a script to do the same thing that the SearchTextFileForSpecificWord .vbs script does . You can, in fact, perform the operation in just three lines of code, as shown here . PS C:\> $filepath = "C:\fso\TestFile.txt" PS C:\> $word = "test" PS C:\> Get-Content -Path $filepath | ForEach-Object {if($_ -match $word){$_}} When you run these commands, you will see the output shown in Figure 13-8 . Introduction to Windows PowerShell Scripting CHapTER 13 405 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  4. FIgURE 13-8 Script-like commands can be typed directly into the Windows PowerShell console . Before you go any further, examine TestFile .t xt in Figure 13-9 . This will give you a better idea of what you are working with . FIgURE 13-9 TestFile .txt contains several lines of text . The first two lines that were typed into the Windows PowerShell console assign string values to variables . This serves the same purpose as the first two lines of the SearchTextFileForSpecificWord .vbs script . The last line typed in the Windows PowerShell console is actually two separate commands . The first one reads the contents of the text file . This is the same as creating an instance of the Scripting.FileSystemObject, opening the text file by using the Do…While…Loop construction, and calling the ReadLine method . Here is the Get-Content command . Get-Content -Path $filepath The results of the Get-Content cmdlet are pipelined to the ForEach-Object cmdlet . The ForEach-Object cmdlet enables you to work inside the pipeline to examine individual lines as they come across the pipe . The variable $_ is an automatic variable that is created when you are working with a pipeline . It is used to enable you to work with a specific item when it is located on the pipeline . In VBScript, you used the If…Then…End If construction . In Windows PowerShell, you use an If(…){…} construction . The two serve the same purpose, however— decision making . In VBScript, the condition that is evaluated goes between the If and the Then statement . In Windows PowerShell, the condition that is evaluated goes between parentheses . In VBScript, the action that is taken when a condition is matched goes between the Then and the End If statements . In Windows PowerShell, the action that is matched goes between a pair of braces . In VBScipt, you used the Instr function to look inside the sentence to see whether a match could be found . In Windows PowerShell, you use the –match operator . In VBScript, you use the Wscript.Echo command to display the matching sentence to the screen, and in Windows PowerShell, you only need to call the $_ variable and it is displayed automatically . 406 CHapTER 13 Overview of Management Tools Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  5. Of course, you do not have to use the Get-Content cmdlet if you do not want to, because Windows PowerShell has a cmdlet called Select-String, which will look inside a text file and retrieve the matching lines of text . The three lines of code seen earlier can therefore be short- ened to this one-line command . PS C:\> Select-String -Path C:\fso\TestFile.txt -Pattern "text" The results of this command are shown in Figure 13-10 . FIgURE 13-10 The Select-String cmdlet reads a file and searches content at the same time . diReCt FRoM tHe SoURCe Command Output James O’Neill, Evangelist Developer and Platform Group S omething that takes some getting used to in Windows powerShell is that any- thing that powerShell generates is treated as output (and the possible input to a later command in a pipeline) unless you explicitly say you want to do something else with it. Thus, you never need to use an echo, print, or write command. Windows powerShell does have commands to do these things, although many of them are redundant. Write-Host is useful to force something to go to the console without be- ing redirected. In other words, an external command like TaskList.exe generates text and sends it to standard output as part of the command. a cmdlet like Get-process returns .NET process objects. Windows powerShell loads formatting information from pS1XML files, and when it has no other instructions, it checks to see whether there is known formatting to apply to the object and uses that to send output to standard output. Sometimes that standard formatting won’t work, and you want to apply your own formatting. Windows powerShell can output objects to comma- separated variable (CSV) files or convert them to HTML tables, which can save a lot of programming effort, but the most commonly used commands are Format-List and Format-Table. One of the things that you will really like to do with Windows PowerShell is to use the formatting cmdlets . There are three formatting cmdlets that are especially helpful . They are listed here, in the reverse order in which you will use them: Introduction to Windows PowerShell Scripting CHapTER 13 407 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  6. n Format-Wide n Format-Table n Format-List Consider the Format-Wide cmdlet . Format-Wide is useful when you want to display a single property across multiple columns . This might happen because you want to have a list of all process names that are currently running on the workstation . Such a command would resemble the following . PS C:\> Get-Process | Format-Wide -Property name –AutoSize The first thing you do is use the Get-Process cmdlet to return all the processes that are running on the computer . You next pipe the process objects to the Format-Wide cmdlet . You use the –property parameter to select the name of each process, and you use the –autosize parameter to tell Format-Wide to use as many columns as possible in the Windows Power- Shell console without truncating any of the process names . You can see the results of this command in Figure 13-11 . FIgURE 13-11 The Format-Wide cmdlet displays a single property . If you are interested in displaying between two and four properties from the processes, you can use the Format-Table cmdlet . The command might resemble the following . PS C:\> Get-Process | Format-Table -Property Name, Path, Id –AutoSize You first use the Get-Process cmdlet and then you pipeline the process objects to the Format-Table cmdlet . You select three properties from the process objects: name, path, and Id . The Format-Table cmdlet also has an –autosize parameter exactly as the Format-Wide cmdlet does . This helps to arrange the columns in such a way that you do not waste space inside the console . As shown in Figure 13-12, because of the length of some paths to process executables, the –autosize parameter had no effect in this example, and the ID column was removed . As a best practice, you always should include the parameter when you are unsure what the output will actually resemble . 408 CHapTER 13 Overview of Management Tools Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  7. FIgURE 13-12 The Format-Table cmdlet makes it easy to create tables . The format cmdlet that you will use the most is Format-List, because it is the best way to display lots of information . It is also a good way to see what kind of data might be returned by a particular command . Armed with this information, you then determine whether you want to focus on a more select group of properties and perhaps output the data as a table or just leave it in a list . When you use the Format-List cmdlet, you will usually use the wildcard * to select all the properties from the objects . Here is an example of obtaining all the property information from all your processes . PS C:\> Get-Process | Format-List -Property * This command displays information that scrolls off the display . A small sampling of the information is shown in Figure 13-13 . Introduction to Windows PowerShell Scripting CHapTER 13 409 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  8. FIgURE 13-13 The Get-Process cmdlet displays process information . There is so much information that all the properties and their values for a single process will not fit on a single screen . When you work with the Format-List cmdlet, if you want to look through all the data, you can pipeline the information to the more function . This works in the same manner as the more command does in the command shell . If you use shortcut names, or aliases, you have a very compact command at your disposal . As shown here, gps is an alias for the Get-Process cmdlet . The fl command is an alias for Format-List . Because the first parameter of the Format-List cmdlet is the –property parameter, you can leave it out of the command . You then pipeline the results to more, which will cause the information to be displayed one page at a time . This command is shown here . PS C:\> gps | fl * | more additional pipeline Techniques The use of the pipeline is a fundamental Windows PowerShell technique . It is, therefore, important to examine different ways to use the pipeline . In this section, you will examine the use of the pipeline to avoid positional errors . You will also see how to use the pipeline to filter result sets and make decisions on the data that crosses the pipeline . 410 CHapTER 13 Overview of Management Tools Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  9. Use the pipeline to avoid positional Errors If you want to obtain information about the Notepad process (assuming that Notepad is actu- ally running), you use the Get-Process cmdlet, as seen here . Get-Process Notepad You do not have to specify the name parameter if you do not want to because the name parameter is the default with Get-Process . You can, of course, type the name parameter and obtain information about the Notepad process as shown here . PS C:\> Get-Process -name notepad Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 47 2 976 3512 59 0.10 3960 notepad To stop the Notepad process, you use the Stop-Process cmdlet . If, however, you are not used to using the name parameter with the Get-Process cmdlet, you will receive a surprise when you try the same syntax with Stop-Process . The result of this is seen here . PS C:\> Stop-Process notepad Stop-Process : Cannot bind parameter 'Id'. Cannot convert value "notepad" to type "System.Int32". Error: "Input string was not in a correct format." At line:1 char:13 + Stop-Process
  10. FIgURE 13-14 Using the pipeline simplifies parameter complications . You can use wildcard characters to identify processes . This technique can be both danger- ous and useful . Here is an example of using wildcard characters to simplify finding all the Notepad processes . PS C:\> Get-Process note* Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 47 2 976 3464 59 0.05 2056 notepad 47 2 976 3488 59 0.09 3292 notepad You can then pipeline the result to the Stop-Process cmdlet and stop all the instances of the Notepad process that are running on the computer, as seen here . Get-Process note* | Stop-Process An example of working with processes by using wildcard characters is seen in Figure 13-15 . FIgURE 13-15 By using wildcard characters, it is easy to identify processes . Using the wildcard characters can be dangerous if you are not careful, however . An example of such a dangerous command is seen in the following code, which would obtain a list of all the processes that are running on the computer and pipeline them to the Stop-Process cmdlet . This will stop every process that is running on the computer, which for most operat- ing systems will cause the computer to shut down (on Windows Vista and later versions, this command must be run by someone with administrative rights) . Get-Process * | Stop-Process Of course, if you want to shut down the operating system, it is best to use the shutdown method from the Win32_OperatingSystem WMI class . 412 CHapTER 13 Overview of Management Tools Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  11. Use the pipeline to Filter Results Suppose you have several instances of Notepad that are running . One instance has been run- ning for a while and has consumed more CPU time than the other processes . You can obtain this information as seen here . PS C:\> Get-Process notepad Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 47 2 976 3452 59 0.10 2688 notepad 49 2 1160 3936 60 1.13 3984 notepad Whereas you could definitely use the process ID, 3984 in this example, to stop the process that is using the most CPU time, you may not want to type two separate commands (or per- haps you want to stop a process automatically if it is using too much CPU time) . Instead, you can pipeline the results of the first query to the Where-Object cmdlet . You can use Where, the alias for Where-Object, to reduce some typing that is required for this command without sacrificing any readability . If you were not worried about readability, you could use gps as an alias for the Get-Process cmdlet, and you could use ? as the alias for the Where-Object . As you become more proficient with Windows PowerShell, you might decide you like using the aliases for the different cmdlet names . If you are curious about which cmdlets have aliases defined for them, you can use the Get-Alias cmdlet to find aliases . You will need to specify the –definition parameter when you use the command . The command to discover aliases for the Get-Process cmdlet is seen here . PS C:\> Get-Alias -Definition Get-Process CommandType Name Definition ----------- ---- ---------- Alias gps Get-Process Alias ps Get-Process The short command is shown here . PS C:\> gps notepad | ? { $_.cpu -gt 1 } Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 47 2 1316 4080 60 1.38 2420 notepad The way you generally type the command is to spell out Get-Process . (You use Tab completion to spell it out . Therefore, you only have to type get-P and then press the Tab key .) The Where-Object cmdlet is used to filter the process objects as they come across the pipeline . Each instance of a process with the name of Notepad is returned by the Get-Process cmdlet . As the process comes across the pipeline, the $_ automatic variable represents the current process object on the pipeline . This enables you to examine the properties of the Introduction to Windows PowerShell Scripting CHapTER 13 413 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  12. process object . Inspect the amount of CPU time that is being used by the process to see whether it exceeds 1 . If it does, the filter will enable the process object to continue . The ex- ample here displays basic information about the process on the console . PS C:\> Get-Process notepad | Where { $_.cpu -gt 1 } Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 49 2 1160 3936 60 1.13 3984 notepad If you are not sure which properties are available for you to use in the Where-Object filter, you can use the Get-Member cmdlet . If you select the properties, you will eliminate the meth- ods . This command is seen here . PS C:\> Get-Process | Get-Member -MemberType property However, you will also miss the ScriptProperty and the AliasProperty properties . To make sure that you can find the other properties that were added by the Windows PowerShell team, use a wildcard in front of the MemberType property . The CPU property is one that was added by the Windows PowerShell team . It is a ScriptProperty property, and the code is seen here . PS C:\> Get-Process | Get-Member -MemberType *property TypeName: System.Diagnostics.Process Name MemberType Definition ---- ---------- ---------- Handles AliasProperty Handles = Handlecount Name AliasProperty Name = ProcessName NPM AliasProperty NPM = NonpagedSystemMemorySize PM AliasProperty PM = PagedMemorySize VM AliasProperty VM = VirtualMemorySize WS AliasProperty WS = WorkingSet __NounName NoteProperty System.String __NounName=Process BasePriority Property System.Int32 BasePriority {get;} Container Property System.ComponentModel.IContainer C... EnableRaisingEvents Property System.Boolean EnableRaisingEvents... ExitCode Property System.Int32 ExitCode {get;} ExitTime Property System.DateTime ExitTime {get;} Handle Property System.IntPtr Handle {get;} HandleCount Property System.Int32 HandleCount {get;} HasExited Property System.Boolean HasExited {get;} Id Property System.Int32 Id {get;} MachineName Property System.String MachineName {get;} MainModule Property System.Diagnostics.ProcessModule M... MainWindowHandle Property System.IntPtr MainWindowHandle {get;} MainWindowTitle Property System.String MainWindowTitle {get;} 414 CHapTER 13 Overview of Management Tools Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  13. MaxWorkingSet Property System.IntPtr MaxWorkingSet {get;s... MinWorkingSet Property System.IntPtr MinWorkingSet {get;s... Modules Property System.Diagnostics.ProcessModuleCo... NonpagedSystemMemorySize Property System.Int32 NonpagedSystemMemoryS... NonpagedSystemMemorySize64 Property System.Int64 NonpagedSystemMemoryS... PagedMemorySize Property System.Int32 PagedMemorySize {get;} PagedMemorySize64 Property System.Int64 PagedMemorySize64 {get;} PagedSystemMemorySize Property System.Int32 PagedSystemMemorySize... PagedSystemMemorySize64 Property System.Int64 PagedSystemMemorySize... PeakPagedMemorySize Property System.Int32 PeakPagedMemorySize {... PeakPagedMemorySize64 Property System.Int64 PeakPagedMemorySize64... PeakVirtualMemorySize Property System.Int32 PeakVirtualMemorySize... PeakVirtualMemorySize64 Property System.Int64 PeakVirtualMemorySize... PeakWorkingSet Property System.Int32 PeakWorkingSet {get;} PeakWorkingSet64 Property System.Int64 PeakWorkingSet64 {get;} PriorityBoostEnabled Property System.Boolean PriorityBoostEnable... PriorityClass Property System.Diagnostics.ProcessPriority... PrivateMemorySize Property System.Int32 PrivateMemorySize {get;} PrivateMemorySize64 Property System.Int64 PrivateMemorySize64 {... PrivilegedProcessorTime Property System.TimeSpan PrivilegedProcesso... ProcessName Property System.String ProcessName {get;} ProcessorAffinity Property System.IntPtr ProcessorAffinity {g... Responding Property System.Boolean Responding {get;} SessionId Property System.Int32 SessionId {get;} Site Property System.ComponentModel.ISite Site {... StandardError Property System.IO.StreamReader StandardErr... StandardInput Property System.IO.StreamWriter StandardInp... StandardOutput Property System.IO.StreamReader StandardOut... StartInfo Property System.Diagnostics.ProcessStartInf... StartTime Property System.DateTime StartTime {get;} SynchronizingObject Property System.ComponentModel.ISynchronize... Threads Property System.Diagnostics.ProcessThreadCo... TotalProcessorTime Property System.TimeSpan TotalProcessorTime... UserProcessorTime Property System.TimeSpan UserProcessorTime ... VirtualMemorySize Property System.Int32 VirtualMemorySize {get;} VirtualMemorySize64 Property System.Int64 VirtualMemorySize64 {... WorkingSet Property System.Int32 WorkingSet {get;} WorkingSet64 Property System.Int64 WorkingSet64 {get;} Company ScriptProperty System.Object Company {get=$this.M... CPU ScriptProperty System.Object CPU {get=$this.Total... Description ScriptProperty System.Object Description {get=$th... FileVersion ScriptProperty System.Object FileVersion {get=$th... Path ScriptProperty System.Object Path {get=$this.Main... Product ScriptProperty System.Object Product {get=$this.M... ProductVersion ScriptProperty System.Object ProductVersion {get=... Introduction to Windows PowerShell Scripting CHapTER 13 415 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  14. Use the pipeline to Take action As soon as you have the filter working correctly and see that it is returning the results you are interested in obtaining, you can just pipeline the resulting process object to the Stop-Process cmdlet . This action is shown here . PS C:\> Get-Process notepad | Where { $_.cpu -gt 1 } | Stop-Process The ability to add pipelines together by feeding the results of one pipeline into another pipeline, as shown earlier, is how you harness the real power of Windows PowerShell . This is a new concept for people who have a background working with graphical user interface (GUI) tools, but it is something that people have done for years at the command line . The big dif- ference for them is that Windows PowerShell passes objects through the pipeline, not merely text . Working with Cmdlets One of the exciting benefits of using Windows PowerShell and learning how to use the built-in cmdlets is that it frees you from worrying about all the details . You may know that Windows PowerShell is built on the Microsoft .NET Framework, but you do not have to worry about .NET Framework programming . If you are interested in working with files and folders, you can use cmdlets to provide this functionality . You therefore avoid writing .NET Framework code . Filtering Cmdlet Output If you want to produce a listing of all the folders and the date when each folder was modified, you could use the FileSystemObject and write a VBScript that is similar to the List- FoldersAndModifiedDates .vbs script . You will notice that you first create an instance of the FileSystemObject and store it in the objFSO variable . You then return a folder object by using the GetFolder method to connect to the root of the C drive . Next, you return a folder col- lection by calling the SubFolders method . You then walk through the collection by using the For…Each …Next statement and display both the name of the folder and the date the folder was changed . The ListFoldersAndModifiedDates .vbs script is seen here . listFoldersAndModifiedDates.vbs Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder("C:\") Set colFOlders = objFolder.SubFolders For Each subFolder In colFOlders WScript.Echo subFolder.Name, subFolder.DateLastModified Next 416 CHapTER 13 Overview of Management Tools Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  15. In Windows PowerShell, you can obtain a collection of files and folders by using the Get- ChildItem cmdlet . When you use the Get-ChildItem cmdlet without supplying any values for the parameters, it returns a list of all the files and folders in the root directory . This is seen in Figure 13-16 . FIgURE 13-16 The Get-ChildItem cmdlet returns a directory listing of the root drive when you use it without parameters . To return a listing of only directories, you have to determine a way to separate the directo- ries from the files that are returned by the default use of the Get-ChildItem cmdlet . There are actually several ways to do this, but they all involve pipelining the results of the Get-ChildItem cmdlet to the Where-Object cmdlet . Most of the time, you can examine the column headings in the display results to find a property that you can use with the Where-Object cmdlet to create a filter for your command . The default column headings used with the Get-ChildItem cmdlet are Mode, LastWriteTime, Length, and Name . Of the four, the Mode column will be of the most use, because it has a d in the first position if the item is a directory . You use the Get-ChildItem cmdlet to retrieve the file and folder objects from the root drive . Then you pipeline the objects to the Where-Object cmdlet . Inside the script block (which is delineated by a pair of braces) for the Where-Object cmdlet, you use the $_ automatic variable to ex- amine each object as it comes across the pipeline . The property that you are interested in is the mode property . You use the –like operator to perform a wildcard match of any value that begins with the letter d and is followed by any other value . The command to list directories on the root drive is seen here . PS C:\> Get-ChildItem | Where-Object { $_.mode -like 'd*' } Introduction to Windows PowerShell Scripting CHapTER 13 417 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  16. The results of the list directory command are seen in Figure 13-17 . FIgURE 13-17 By using wildcard characters, you can separate directories from files . If you want to replicate the output from the ListFoldersAndModifiedDates .vbs script exact- ly, you have to pass the results further down the pipeline so that you can reduce the informa- tion that is returned . You can use the Select-Object cmdlet to choose only the name and the LastWriteTime properties . When you use the Select-Object cmdlet to select certain properties, the object that is returned is a custom object that contains only the properties that you select and the methods that are common to all Windows PowerShell objects . By piping the output of Select-Object into the Get-Member cmdlet, the members of the newly created custom object are shown here . TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- Equals Method System.Boolean Equals(Object obj) GetHashCode Method System.Int32 GetHashCode() GetType Method System.Type GetType() ToString Method System.String ToString() LastWriteTime NoteProperty System.DateTime LastWriteTime=8/17/2008 1:23:10 PM Name NoteProperty System.String Name=19287a2cfb60a3bbcca7 418 CHapTER 13 Overview of Management Tools Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  17. Understanding Cmdlet Output Objects It is important to understand the object that is returned by a cmdlet so that you can perform additional processing on the object if you want to do so . The Get-ChildItem command, which lists the name and last write time of all the directories on the root drive, is shown here . This code is a single command that is broken at the pipeline character for readability . PS C:\> Get-ChildItem | Where-Object { $_.mode -like 'd*' } | Select-Object -Property Name, LastWriteTime The results of the Get-ChildItem command are shown in Figure 13-18 . FIgURE 13-18 You can reduce the information returned from a command by using the Select-Object cmdlet . You can reduce the typing without sacrificing any of the readability of the command by using dir as the alias for Get-ChildItem, Where as the alias for Where-Object, and Select as the alias for Select-Object . You can also omit the –property parameter, because it is the default parameter for the Select-Object cmdlet . The revised command is shown here . PS C:\> dir | where { $_.mode -like 'd*'} | select name, lastwritetime Another way to produce a listing of the name and the last write time of each directory in the root directory is to send the output to the Format-Table cmdlet, as illustrated here . PS C:\> Get-ChildItem | Where-Object { $_.mode -like 'd*' } | Format-Table -Property Name, LastWriteTime The output produced by using the Format-Table cmdlet is almost the same as the output produced by using the Select-Object cmdlet . This is seen in Figure 13-19 . Introduction to Windows PowerShell Scripting CHapTER 13 419 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  18. FIgURE 13-19 By using the Format-Table cmdlet, you can create almost the same results as the output produced by the Select-Object cmdlet . The problem with using Format-Table to format your output is that if you have to do any- thing else to the data, you are left with a series of five different format objects that are basi- cally useless for additional data manipulation . Depending on what you are trying to achieve, even the custom Windows PowerShell object that is created by the Select-Object cmdlet will cause you problems . As a best practice, you should always perform all data manipulation before sending your object to an output cmdlet . At this point, you have one last thing that you can do easily in your pipeline—send the output to a text file . The easiest way to do this is to use the >> redirection operator as shown here (once again, the single command is broken at the pipeline character for readability) . PS C:\> Get-ChildItem | Where-Object { $_.mode -like 'd*' } | Format-Table -Property Name, LastWriteTime >> c:\fso\directoryFile.txt The text file that is produced by the redirection operator maintains the format that is dis- played on the console . This is seen in Figure 13-20 . FIgURE 13-20 The redirection operator maintains formatting seen on the Windows PowerShell console . 420 CHapTER 13 Overview of Management Tools Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  19. This concludes our overview of using Windows PowerShell to simplify working with direc- tories and files . Scripting Fundamentals In its most basic form, a Windows PowerShell script is a collection of Windows PowerShell commands, such as the following . Get-Process notepad | Stop-Process You can put that command into a Windows PowerShell script and run it directly as it is written . The StopNotepad .ps1 script is shown in Figure 13-21 . FIgURE 13-21 StopNotepad .ps1 script seen in Notepad To create a Windows PowerShell script, you only have to copy the command in a text file and save the file by using a .ps1 extension . If you double-click the file, it will open with the graphical version of Windows PowerShell . The graphical version of Windows PowerShell is called Windows PowerShell ISE . Running Windows powerShell Scripts To run the script, if you are running Windows XP or Windows Server 2003, you can open the Windows PowerShell console and drag the file to the console . In Windows Vista, the capabil- ity of dragging to a command line was removed due to potential security implications . To replace it, Windows Vista introduced a very helpful command that you can use instead: the Copy As Path command . You hold down the Shift key, right-click the PS1 file, and select Copy As Path from the action menu shown in Figure 13-22 . Introduction to Windows PowerShell Scripting CHapTER 13 421 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  20. FIgURE 13-22 Windows Vista introduced the Copy As Path command to simplify working with long paths inside the Windows PowerShell console . Windows 7 has fixed dragging and dropping to the console, and it keeps the Copy As Path action as well, giving you the best of both worlds . Now you are ready to run your first script . To do this, copy the path of the script, right-click inside the Windows PowerShell console to paste the path of your script there, and press Enter . You just printed out a string that repre- sents the path of the script as seen here . PS C:\> "C:\BestPracticesBook\StopNotepad.ps1" C:\BestPracticesBook\StopNotepad.ps1 diReCt FRoM tHe SoURCe Expressions and Paths James O’Neill, Evangelist Developer and Platform Group W indows powerShell can execute commands and evaluate “expressions.” For example, 2 + 2 is an expression, and so is $Host (the value of the variable named host). "Hello world" is a common example of an expression, and 1..100 (num- bers from 1 to 100) is also an expression. If you enter an expression in Windows powerShell, powerShell will work out its value and pass it on—so an expression on its own just generates output to the console, but it can be piped into a command. This poses a problem when you try to execute something like "C:\program Files (x86)\Internet Explorer\iexplore.exe". Without the quotation marks, Windows powerShell uses the spaces to break up the path and understand it; with the quotation marks, powerShell thinks it is a string constant. To tell Windows powerShell to execute a string as a command, prefix it with an ampersand (&). 422 CHapTER 13 Overview of Management Tools Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Đồng bộ tài khoản