• No results found

The first step of ETS enhancement is to determine which object type you want to display better. You may frequently use Get-WmiObject to get information from the WMI service, but you're not happy with the way PowerShell displays these objects:

Get-WmiObject Win32_Processor __GENUS : 2 __CLASS : Win32_Processor __SUPERCLASS : CIM_Processor __DYNASTY : CIM_ManagedSystemElement __RELPATH : Win32_Processor.DeviceID="CPU0" __PROPERTY_COUNT : 48

__DERIVATION : {CIM_Processor, CIM_LogicalDevice, CIM_LogicalElement, CIM_Managed SystemElement} __SERVER : TOBIASWELTNE-PC __NAMESPACE : root\cimv2 __PATH : \\TOBIASWELTNE-PC\root\cimv2:Win32_ Processor.DeviceID="CPU0" AddressWidth : 32 Architecture : 9 Availability : 3

Caption : x64 Family 6 Model 15 Stepping 6 ConfigManagerErrorCode : ConfigManagerUserConfig : CpuStatus : 1 CreationClassName : Win32_Processor CurrentClockSpeed : 1000 CurrentVoltage : 12 DataWidth : 64

Description : x64 Family 6 Model 15 Stepping 6 DeviceID : CPU0

ErrorCleared : ErrorDescription : ExtClock : Family : 1

InstallDate : L2CacheSize : 4096 L2CacheSpeed : L3CacheSize : 0 L3CacheSpeed : 0 LastErrorCode : Level : 6 LoadPercentage : Manufacturer : GenuineIntel MaxClockSpeed : 2167

Name : Intel(R) Core(TM)2 CPU T7400 @ 2.16GHz NumberOfCores : 2 NumberOfLogicalProcessors : 2 OtherFamilyDescription : PNPDeviceID : PowerManagementCapabilities : PowerManagementSupported : False ProcessorId : BFEBFBFF000006F6 ProcessorType : 3 Revision : 3846 Role : CPU SocketDesignation : U1 Status : OK StatusInfo : 3 Stepping : 6 SystemCreationClassName : Win32_ComputerSystem SystemName : TOBIASWELTNE-PC UniqueId : UpgradeMethod : 8

Version : Modell 15, Stepping 6 VoltageCaps :

First, find out what type of object is returned by the command:

$object = Get-WmiObject Win32_Processor | Select-Object -first 1

$object.GetType().FullName

System.Management.ManagementObject

This shows you that you need an ETS enhancement for objects of the type

System.Management.ManagementObject. Next, take a look at this object's properties and select one that you want the ETS to convert into text. For example, DeviceID, Name, and ProcessorID. Then, formulate the definition of the object in XML. In the TableHeaders area, set column headers, and in the TableRowEntries area, set object properties.

<Configuration> <ViewDefinitions> <View>

<Name>CustomView</Name> <ViewSelectedBy>

<TypeName>System.Management.ManagementObject</TypeName> </ViewSelectedBy>

<TableControl> <TableHeaders>

<TableColumnHeader> <Label>Name</Label> <Width>12</Width> </TableColumnHeader> <TableColumnHeader>

<Label>Description</Label> <Width>30</Width> </TableColumnHeader> <TableColumnHeader> <Label>ID</Label> </TableColumnHeader> </TableHeaders> <TableRowEntries> <TableRowEntry> <TableColumnItems> <TableColumnItem>

<PropertyName>DeviceID</PropertyName> </TableColumnItem>

<TableColumnItem>

<PropertyName>Description</PropertyName> </TableColumnItem>

<TableColumnItem>

<PropertyName>ProcessorID</PropertyName> </TableColumnItem> </TableColumnItems> </TableRowEntry> </TableRowEntries> </TableControl> </View> </ViewDefinitions> </Configuration>

Store this XML code in a file called Win32_Processor.format.ps1xml.Thhen, use Update-FormatData

to read it into the ETS:

Update-FormatData Win32_Processor.format.ps1xml

Now, the result will be much easier to understand when you output Win32_Processorobjects again:

Get-WmiObject Win32_Processor

Name Description ID ---- --- --

CPU0 x64 Family 6 Model 15 Stepp... BFEBFBFF000006F6

However, in this particular instance a mishap occurred. When you acquire other WMI objects, these will now also be displayed in the format that you just defined:

Name Description ID ---- --- -- Remote Admin Default share Default share Remote IPC Default share

The reason has to do with special features of the WMI. It returns all WMI objects in a

System.Management.ManagementObject type.

$object = Get-WmiObject Win32_Service | Select-Object -first 1

$object.GetType().FullName

System.Management.ManagementObject

So, the ETS didn't make a mistake. Instead, the culprit is the WMI as for WMI objects (and only for these), ETS enhancements must be more specific since the type name alone is not enough. That's why WMI objects are assigned to additional object types that you can find in the PSTypeNames

property:

$object = Get-WmiObject Win32_Processor | Select-Object -first 1

$object.PSTypeNames System.Management.ManagementObject#root\cimv2\Win32_Processor System.Management.ManagementObject System.Management.ManagementBaseObject System.ComponentModel.Component System.MarshalByRefObject System.Object

The object name that is specific to Win32_Processor objects is called

System.Management.ManagementObject#root\cimv2\Win32_Processor. So, you would have to specify this object name in your ETS enhancement so that the enhancement applies only to

Win32_Processor WMI objects:

<Configuration> <ViewDefinitions> <View> <Name>CustomView</Name> <ViewSelectedBy> <TypeName>System.Management.ManagementObject#root \cimv2\Win32_Processor</TypeName> </ViewSelectedBy> <TableControl> <TableHeaders> <TableColumnHeader> (...)

Modify your enhancement accordingly, and read it again with Update-FormatData. You can safely ignore the resulting error message. After updating, your enhancement will be valid only for

Summary

PowerShell uses a pipeline for all command entries, which feeds the results of the preceding

command directly into the subsequent command. The pipeline is active even when you enter only a single command because PowerShell always automatically adds the Out-Default cmdlet at the pipeline's end so that it always results in a two-member instruction chain.

Single command results are passed as objects. The cmdlets shown in Table 5.1 can filter, sort, compare, measure, expand, and restrict pipeline elements. All cmdlets accomplish this on the basis of object properties. In the process, the pipeline distinguishes between sequential and streaming modes. In streaming mode, command results are each collected, and then passed in mass onto the next command. Which mode you use depends solely on the pipeline commands used. Output

cmdlets dispose of output. If you specify none, PowerShell automatically uses Out-Host to output the results in the console. However, you could just as well send results to a file or printer.

All output cmdlets convert objects into readable text while formatting cmdlets are responsible for conversion. Normally, formatting cmdlets convert only the most important, but if requested, all objects into text. The Extended Type System (ETS) helps convert objects into text. The ETS uses internal records that specify the best way of converting a particular object type into text. If an object type isn't in an ETS internal record, the ETS resorts to a heuristic method, which is guided by,

among other things, how many properties are contained in the unknown object.

In addition to traditional output cmdlets, export cmdlets store objects either as comma-separated lists that can be opened in Excel or serialized in an XML format. Serialized objects can be

comfortably converted back into objects at a later time. Because when exporting, in contrast to outputting, only plain object properties, without cosmetic formatting, are stored so that no formatting cmdlets are used.

CHAPTER 6.

Using Objects

Using Objects

PowerShell always works with objects. Whenever you output objects into the PowerShell console, PowerShell automatically converts the rich objects into readable text. In this chapter, you will learn what objects are and how to get your hands on PowerShell objects before they get converted to simple text.

Topics Covered:

• Objects = Properties + Methods • Creating a New Object • Adding Properties • Adding Methods

• Properties: What an Object "Is" • Properties Containing Objects

• Read-Only and Read-Write Properties

• Table 6.1: Properties of the RawUI object • Property Types

• Listing All Properties

• Methods: What an Object "Can Do" • Eliminating "Internal" Methods

• Get_ and Set_ Methods • Standard Methods

• Table 6.2: Standard methods of a .NET object • Calling a Method

• Call Methods with Arguments

• Which Arguments are Required? • Low-Level Functions

• Several Method "Signatures"

• Playing with PromptForChoice • Working with Real-Life Objects

• Storing Results in Variables • Using Object Properties

• PowerShell-Specific Properties

• Table 6.3: Different property types • Using Object Methods

• Different Method Types

• Table 6.4: Different types of methods • Using Static Methods

• Table 6.5: Mathematical functions from the [Math] library • Finding Interesting .NET Types

• Converting Object Types • Using Static Type Members

• Using Dynamic Object Instance Members • Listing Assemblies

• EndFinding Interesting Classes (Types) • Looking for Methods

• Using Constructors • New Objects by Conversion

• Loading Additional Assemblies: Improved Internet Download • Using COM Objects

• Which COM Objects Are Available? • How Do You Use COM Objects? • Summary