WMIObject -list (parameter to enumerate possible objects) WMIObject | Get-Member (discover which properties suit our mission)
PowerShell: More flexible than Ipconfig. (Where clause to filter the output) Incidentally, remember that you can run Ipconfig from inside PowerShell.
Guy's Advice
Either work
through my learning progression by starting with Objective 1 (recommended), or else if you are in a hurry, cut to the chase and head for Objective 3
PowerShell: More flexible than Ipconfig.
Prepare to run cmdlets with this PowerShell command:
set-ExecutionPolicy RemoteSigned
Copy the code below into a text file.
Save the file with a .ps1 extension, for example network.ps1
In PowerShell, navigate to where you saved network.ps1
Issue this command: .\network (dot backslash filename)
cls Get-WmiObject win32_share
Learning Points
Note 1: Because PowerShell is so simple, yet so efficient, you need far fewer commands than for a corresponding VBScript. Indeed, the bare minimum to get
started, and display all the WMI object is these 8 letters with a dash in the middle: gwmi -list # gwmi is an alias for Get-wmiobject
Note 2: Observe the (|) pipe symbol. You will use this hundreds of times in PowerShell, what
happens is the output of the wmiobject list is pumped into the where clause, which filters the entries for those that contain the word network. My thinking was gwmi-list produces too many objects.
Note 3: I deliberately don't use many aliases in my
scripts, but ft
(format-Table) is useful for controlling the display of PowerShell's output. Do try the script with and without the last word, name.
This script identifies the properties available for the object: Win32_NetworkAdapterConfiguration. Here below is a classic example of the Get-Member construction.
Note 1: Observe how -class homes in on the object that we are interested in, namely Win32_NetworkAdapterConfiguration.
Note 2: If you wished to manipulate the
TCP/IP settings, then you could see what methods are available; substitute 'Method' for the last word, 'Property'. Alternatively, you could omit the -Membertype phrase altogether.
Guy Recommends: SolarWinds' Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
Note 1: The properties that I have chosen are not important. This is just a case in point; it would make my day if you substituted properties that you researched from Example 2: Get-Member, for example, .DefaultGateway or .IPSubnet instead of .MacAddress.
Note 2: My old friend 'Barking' Eddie thought that my printer needed cleaning when he saw ` in my script.
Actually this character (`), found at the top left of the keyboard, is called a backtick. What the backtick does is tell PowerShell - this command continues on the next line. Just to be clear, this character corresponds to ASCII 096, and is not a comma!
Note 3: I chose the ForEach construction, to loop though all the network adapters. In PowerShell in general, and ForEach in particular, the type of brackets are significant; (ellipses for the
condition) {curly for the block command}.
Note 4: Most of my scripts employ the Where clause to filter the output. In this case I wanted to discard any virtual network cards.
Note 5: I almost forgot, $strComputer controls which computer you are analysing. Again, it would make my day if altered "." to the hostname of another machine on your network.
»
Summary of
PowerShell: More flexible than Ipconfig
The aim of this script is to show that PowerShell can not only mimic Ipconfig, but exceed its capabilities. In particular that PowerShell could customise the
TCP/IP properties in the output, and also interrogate other machines on the network.
See More Windows PowerShell Examples of Real-life Tasks
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.