• No results found

103Importing, exporting, and converting objects

In document PowerShell in Depth, 2nd Edition (Page 132-135)

Working with objects

103Importing, exporting, and converting objects

Jeffery,IT,Syracuse Richard,IT,London Greg,Custodial,Denver

You can now run the following command to bring that data into the shell as objects:

PS C:\> Import-Csv .\data.csv

Name Department City ---- --- ---- Don IT Las Vegas Jeffery IT Syracuse Richard IT London Greg Custodial Denver

As you can see, PowerShell does the work of interpreting the CSV file. At the start of this chapter, we explained that “rows” and “columns” in a spreadsheet become “objects” and “properties” when they’re made into objects, and that’s exactly what Import-CSV has done. You can then manipulate those objects as you’ve manipulated others:

PS C:\> Import-Csv .\data.csv | where { $psitem.Department -eq "IT" } |

➥ Sort Name

Name Department City ---- --- ---- Don IT Las Vegas Jeffery IT Syracuse Richard IT London

How cool is that? Everything in PowerShell is geared to make working with objects easy. By getting the shell to convert other data structures into objects, you get to work with that stuff more easily.

Now for a quick look at HTML. The only cmdlet here is ConvertTo-HTML; for some reason, there’s no Export-HTML, so you’ll generally have to redirect the output to a file on your own. There’s also no Import or ConvertFrom option here; it’s a one-way trip to HTML. As with the CSV format, make sure you’re only converting simple property values. No nested objects. Here’s the example, and figure 7.2 shows the results.

PS C:\> Get-Service | Where { $_.Status -eq "Stopped" } |

➥ ConvertTo-HTML -Property Name,Status,DisplayName |

➥ Out-File Stopped.html

NOTE The ConvertTo-HTML cmdlet has many more uses for its many differ- ent parameters. We’ll make heavy use of them toward the end of the book, in chapter 33 on creating reports.

Finally, a quick word on the CliXML format: It’s XML. It’s a simple XML that Power- Shell understands natively. It’s a great way to persist objects over time, such as creating a snapshot of some objects for later examination. We’re going to use it in the next sec- tion for that purpose.

7.10

Comparing objects

The last cmdlet you’ll learn in this chapter is Compare-Object, which has an alias named Diff. You’re going to use it in conjunction with Export-CliXML and Import- CliXML to perform a cool, and incredibly useful, trick.

Do you do configuration change reporting in your environment? Many organiza- tions do, and PowerShell can make it easy. You start by creating a baseline, or refer- ence file, that represents the way you want things to be configured. For example:

PS C:\> Get-Process | Export-CliXML proc-baseline.xml

That takes a snapshot of the currently running processes and puts it into PowerShell’s

XML format, in an external file. CliXML is better than CSV for this task, because XML

can represent deeply nested data, whereas CSV can only represent a single, flat level of data. Let’s say you do this on a server, where the processes that are running should be pretty fixed. If new processes crop up over time, you’ll definitely want to know about it. So, you’ll come along in a month or so and see what’s new. The following is a one- line PowerShell command:

PS C:\> Compare-Object -ReferenceObject (Import-CliXML .\proc-baseline.xml)

➥ -DifferenceObject (Get-Process) -Property Name Figure 7.2 Viewing converted-to-HTML data in Internet Explorer

105 Summary Name SideIndicator ---- --- calc => mspaint => notepad => svchost =>

A blank result set would have been good news—what the heck is going on here?

MSPaint running on a server? You clearly need to have a group meeting about proper uses for servers.

Here’s what you need to do:

Run Compare-Object.

The first parameter is –ReferenceObject, which is your baseline. To provide

the baseline data, use a parenthetical command that imports your baseline data from the XML file. The entire contents of that XML file are converted into objects, and those become the values for the parameter.

■ The second parameter is –DifferenceObject, which is what you want to com- pare the reference to. You have the current process objects as the values for the parameter, again by using a parenthetical command.

■ The properties of a process are always changing: Memory, processor, and so forth are always different. So you don’t want to compare those values, which Compare-Object would normally do. Instead, use the –Property parameter to tell it to only look at one property. That property is Name, which won’t ever change during a process’s lifetime.

■ The results include a “side indicator.” It’s a little arrow, and if it points right, it means the difference set has something (in this case, the current processes) that doesn’t exist in the reference set. A left-pointing arrow means the oppo- site—a process existed in the baseline but doesn’t currently exist.

We’ve seen companies build scripts that are little more than dozens, or even hundreds, of those Compare-Object commands, each one comparing a different baseline to some portion of the existing configuration. They’ll even pipe the output to an HTML file, and then email the file (as an attachment, using Send-MailMessage) to someone.

7.11

Summary

Well, we covered a lot. The goal of this chapter was to introduce you to the idea of objects and to show you some of the core PowerShell cmdlets that manipulate objects. We dare say that you’ll use these commands all the time, whether you’re working with Windows, Active Directory, Exchange Server, SQL Server, SharePoint Server, VMware, Citrix, or anything else that’s manageable with PowerShell. The skills you learned in this chapter, and the ones you’ll learn in the next couple of chapters, are as funda- mental to PowerShell as the mouse is to Windows itself. We covered a lot of ground, so be prepared to come back to this chapter to refresh your memory any time you need to and be sure to read any help topics we eferenced.

106

In document PowerShell in Depth, 2nd Edition (Page 132-135)