Saving and Importing Data
The following articles provide a brief introduction to saving and/or importing data by using Windows PowerShell; these tasks include such things as reading and writing to text files as well as saving data in HTML or XML format. As is so often the case with Windows PowerShell, the code snippets found in these articles can either be included within a Windows PowerShell script or typed directly into the Windows PowerShell console.
Using the Add-Content Cmdlet
Appending Data to a Text File
One use of the Add-Content cmdlet is to append data to a text file. For example, this command adds the words “The End” to the file C:\Scripts\Test.txt:Add-Content c:\scripts\test.txt "The End"
Add-Content c:\scripts\test.txt "`nThe End"
- `0 -- Null
- `a -- Alert
- `b -- Backspace
- `n -- New line
- `r -- Carriage return
- `t -- Horizontal tab
- `' -- Single quote
- `" -- Double quote
Write-Host `a
$A = Get-Date; Add-Content c:\scripts\*.log $A
Add-Content Aliases |
---|
|
Using the Select-String Cmdlet
Checking for the Existence of a String Value
What can you do with the Select-String cmdlet? Well, one thing you can do is determine whether or not a specific string value exists in a text file. For example, suppose the file C:\Scripts\Test.txt is a log file that contains the following information:Operation succeeded, 5/1/2006 Operation succeeded, 5/2/2006 Operation failed, 5/3/2006 Operation succeeded, 5/4/2006 Operation succeeded, 5/5/2006
Get-Content c:\scripts\test.txt | Select-String "Failed" -quiet
Operation failed, 5/3/2006
Get-Content c:\scripts\test.txt | Select-String "Failed" -quiet -casesensitive
Using the Tee-Object Cmdlet
Display Data and Save That Data with One Command
The Tee-Object cmdlet enables you to display data in the Windows PowerShell window and to save that same data to a text file, all with a single command. For example, this command uses the Get-Process cmdlet to retrieve information about all the processes currently running on the computer, then uses Tee-Object to simultaneously display the data on-screen and save that data to the file C:\Scripts\Test.txt:Get-Process | Tee-Object -file c:\scripts\test.txt
Tee-Object Aliases |
---|
|
Using the Clear-Content Cmdlet
Erasing the Contents of a File
The Clear-Content cmdlet enables you to erase the contents of a file without deleting the file itself. For example, suppose you run this command:Clear-Content c:\scripts\test.txt
Did we hear someone ask if wildcard characters can be used with Clear-Content? You bet; this command erases the contents of any file in C:\Scripts whose file name starts with the letter E:
Clear-Content c:\scripts\e*
Clear-Content c:\scripts\test.xls
Clear-Content c:\scripts\test.doc
Clear-Content Aliases |
---|
|
Using the ConvertTo-Html Cmdlet
Saving Data as an HTML File
No offense to the console window or a text file, but sometimes it’s hard to beat HTML as an output device (for one thing, HTML gives you more formatting options and flexibility). Fortunately the ConvertTo-Html cmdlet makes it very easy to view Windows PowerShell output in a Web page. For example, this command uses the Get-Process cmdlet to retrieve information about all the processes running on the computer. The output from Get-Process is piped to the ConvertTo-Html cmdlet, which creates an HTML table out of that data. In turn, that table is piped to the Set-Content cmdlet, which saves the information as a Web page (C:\Scripts\Test.htm). That sounds like a lot of work, yet the command is as simple as this:Get-Process | ConvertTo-Html | Set-Content c:\scripts\test.htm
Good point: the table is a bit unwieldy; that’s because the large number of properties returned by Get-Process results in a corresponding large number of columns in the table. This revised command limits the properties displayed in the table to Name, Path, and FileVersion:
Get-Process | ConvertTo-Html name,path,fileversion | Set-Content c:\scripts\test.htm
Our simplified Web page looks like this:
Much better.
But not perfect. By default the title assigned to the Web page is the less-than-scintillating HTML Table. With that in mind, this command uses the -title parameter to change the window title to Process Information:
Get-Process | ConvertTo-Html name,path,fileversion -title "Process Information" | Set-Content c:\scripts\test.htm
We’re definitely on the right track now.
Let’s do one last thing. As you can see, the only information we have on our Web page is the data table. If you’d like to preface the table with some explanatory information then simply include the -body parameter followed by the text you’d like to appear on the page:
Get-Process | ConvertTo-Html name,path,fileversion -title "Process Information" -body "Information about the processes running on the computer." | Set-Content c:\scripts\test.htm
We should also point out that information configured using the -body parameter is HTML; that means the parameter can include any valid HTML tags. Want to display your preface using the <H2> style? Then just include the <H2> and </H2> tags:
Get-Process | ConvertTo-Html name,path,fileversion -title "Process Information" -body "<H2>Information about the processes running on the computer.</H2>" | Set-Content c:\scripts\test.htm
If you’ve ever wondered why the Scripting Guys aren’t Web page designers, well, consider the preceding screenshot Exhibit A. But now that you know how ConvertTo-HTML works you can do better.
Using the Get-Content Cmdlet
Reading a Text File
What can you do with the Get-Content cmdlet? Well, one thing you can do is quickly read and display the contents of a text file. For example, this command displays the contents of the file C:\Scripts\Test.txt:Get-Content c:\scripts\test.txt
Hey, we never said it was exciting, just that it was useful.
How useful? Here’s a simple, but telling example. Suppose you have a text file (C:\Scripts\Test.txt) that contains the names of two or more computers. You’d like to use WMI to retrieve information about the BIOS installed on each of those computers. Here you go:
Get-Content c:\scripts\test.txt | Foreach-Object {Get-Wmiobject -computername $_ win32_bios}
SMBIOSBIOSVersion : 68DTT Ver. F.0D Manufacturer : Hewlett-Packard Name : EPP runtime BIOS - Version 1.1 SerialNumber : CND60723S7 Version : HP - 22110520 SMBIOSBIOSVersion : A03 Manufacturer : Dell Computer Corporation Name : Phoenix ROM BIOS PLUS Version 1.10 A03 SerialNumber : HTVNX41 Version : DELL - 7
ForEach-Object {Get-Wmiobject -computername $_ win32_bios}
Confused? Try it on your set of computers and see what happens.
Because Get-Content automatically creates an array consisting of each line in the file, that means you can also use the Measure-Object cmdlet to easily count the number of lines in the file, a task people like to do on a regular basis:
Get-Content c:\scripts\test.txt | Measure-Object
Count : 124 Average : Sum : Maximum : Minimum : Property :
Get-Content c:\scripts\test.txt -totalcount 5
Get-Content c:\scripts\test.txt | Select-Object -last 5
Get-Content Aliases |
---|
|
No comments:
Post a Comment