PS C:\> gwmi win32_networkadapterconfiguration -filter "description like '%intel%'" | gm in Visual Basic .NET

Generator Data Matrix in Visual Basic .NET PS C:\> gwmi win32_networkadapterconfiguration -filter "description like '%intel%'" | gm

PS C:\> gwmi win32_networkadapterconfiguration -filter "description like '%intel%'" | gm
Print ECC200 In VB.NET
Using Barcode creator for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET applications.
www.OnBarcode.com
Recognizing Data Matrix ECC200 In Visual Basic .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Working with bunches of objects, one at a time
PDF-417 2d Barcode Creation In Visual Basic .NET
Using Barcode creator for .NET Control to generate, create PDF417 image in VS .NET applications.
www.OnBarcode.com
Encoding Code 3/9 In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Code39 image in .NET applications.
www.OnBarcode.com
Right near the top of the resulting list, I should see the method that I m after: EnableDHCP():
UCC.EAN - 128 Drawer In VB.NET
Using Barcode generation for .NET framework Control to generate, create EAN / UCC - 13 image in VS .NET applications.
www.OnBarcode.com
Encode Data Matrix In Visual Basic .NET
Using Barcode creation for .NET Control to generate, create ECC200 image in .NET applications.
www.OnBarcode.com
TypeName: System.Management.ManagementObject#root\cimv2\Win32_NetworkAd apterConfiguration Name ---DisableIPSec EnableDHCP EnableIPSec EnableStatic MemberType ---------Method Method Method Method Definition ---------System.Management.ManagementB... System.Management.ManagementB... System.Management.ManagementB... System.Management.ManagementB...
Code128 Printer In VB.NET
Using Barcode generator for Visual Studio .NET Control to generate, create USS Code 128 image in Visual Studio .NET applications.
www.OnBarcode.com
Create Royal Mail Barcode In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create British Royal Mail 4-State Customer Code image in Visual Studio .NET applications.
www.OnBarcode.com
The next step a lot of PowerShell newcomers try is to pipe the configuration objects to the method:
Scan Data Matrix 2d Barcode In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
ECC200 Drawer In C#.NET
Using Barcode printer for VS .NET Control to generate, create ECC200 image in .NET applications.
www.OnBarcode.com
PS C:\> gwmi win32_networkadapterconfiguration -filter "description like '%intel%'" | EnableDHCP()
PDF417 Generation In Java
Using Barcode drawer for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
UPC-A Supplement 5 Encoder In Java
Using Barcode generator for Java Control to generate, create UPC A image in Java applications.
www.OnBarcode.com
Sadly, that won t work. You can t pipe objects to a method; you can only pipe to a cmdlet. EnableDHCP isn t actually a PowerShell cmdlet. Rather, it s an action that s directly attached to the configuration object itself. The old, VBScript-style approach would look a lot like the VBScript example I showed you at the start of this chapter, but with PowerShell you can do something simpler. Although there s no batch cmdlet called Enable-DHCP, there is a generic cmdlet called Invoke-WmiMethod. This cmdlet is specially designed to accept a batch of WMI objects, such as my Win32_NetworkAdapterConfiguration objects, and to invoke one of the methods attached to those objects. So here s the command I would run:
EAN / UCC - 14 Encoder In VS .NET
Using Barcode creator for Reporting Service Control to generate, create GS1 128 image in Reporting Service applications.
www.OnBarcode.com
Barcode Generation In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
PS C:\> gwmi win32_networkadapterconfiguration -filter "description like '%intel%'" | Invoke-WmiMethod -name EnableDHCP
Paint PDF 417 In None
Using Barcode generator for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com
Read Universal Product Code Version A In Visual Basic .NET
Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
There are a few things to keep in mind:
Scanning Data Matrix ECC200 In Visual C#
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Draw Code39 In Visual C#
Using Barcode drawer for Visual Studio .NET Control to generate, create Code 39 Extended image in .NET framework applications.
www.OnBarcode.com
The method name isn t followed by parentheses The method name isn t case-sensitive Invoke-WmiMethod can only accept one kind of WMI object at a time. In this case, I m only sending it Win32_NetworkAdapterConfiguration objects, so it will work fine. It s okay to send it more than one object (that s the whole point, in fact), but all of the objects have to be of the same type.
Make Linear Barcode In VS .NET
Using Barcode generator for ASP.NET Control to generate, create Linear 1D Barcode image in ASP.NET applications.
www.OnBarcode.com
UPC A Reader In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
The output of Invoke-WmiMethod can be a little confusing. WMI always produces a result object, and it has a lot of system properties (whose names start with two underscore characters). In my case, the command produced this:
__GENUS __CLASS __SUPERCLASS __DYNASTY __RELPATH __PROPERTY_COUNT : : : : : : 2 __PARAMETERS __PARAMETERS 1
The WMI way: invoking WMI methods
__DERIVATION __SERVER __NAMESPACE __PATH ReturnValue __GENUS __CLASS __SUPERCLASS __DYNASTY __RELPATH __PROPERTY_COUNT __DERIVATION __SERVER __NAMESPACE __PATH ReturnValue : {} : : : : 0 : : : : : : : : : : : 2 __PARAMETERS __PARAMETERS 1 {}
The only useful information here is the one property that doesn t start with two underscores: ReturnValue. That number tells me the result of the operation. A Google search for Win32_NetworkAdapterConfiguration turns up the documentation page, and I can then click through to the EnableDHCP method to see the possible return values and what they mean. Figure 13.1 shows what I discovered. Zero appears to mean success, while 84 says that IP isn t enabled on that adapter configuration, so DHCP can t be enabled. But which bit of the output went with which of my two network adapter configurations It s difficult to tell, because the output
Looking up return values for a WMI method s results
Working with bunches of objects, one at a time
doesn t tell you which specific configuration object produced it. That s unfortunate, but it s the way WMI works. Invoke-WmiMethod will work for most situations where you have a WMI object that has a method that you want to execute. It works great when querying WMI objects from remote computers too. My basic rule is, If you can get to something by using Get-WmiObject, then Invoke-WmiObject can execute its methods.
13.4 The backup plan: enumerating objects
Unfortunately, I have run across a few situations where Invoke-WmiObject couldn t execute a method it kept returning weird error messages. I ve also run into cases where I have a cmdlet that can produce objects, but there is no batch cmdlet to which I can pipe those objects to take some kind of action. In either case, you can still perform whatever task you wanted to perform, but you ll have to fall back on the old VBScript-style approach of instructing the computer to enumerate the objects and perform your task against one object at a time. There are two ways to accomplish this in PowerShell: one is using a cmdlet, and the other is using a scripting construct. We ll focus on the first way in this chapter, and I ll save the second way for chapter 21, which dives into PowerShell s built-in scripting language. As an example of how to do this, I m going to use the Win32_Service WMI class. Specifically, I m going to use the Change() method. This is a complex method that can change several elements of a service at once. Figure 13.2 shows its online
Copyright © OnBarcode.com . All rights reserved.