Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



PowerShell Share Set-Acl

PowerShell Share

Share.

Topics for PowerShell Share

Our Mission

Enumerate file shares on a server.

PowerShell Objectives

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.

Objective 1) - List WMI Objects

Here is a cmdlet which identifies all the Network WMI objects.

Preparation

Download PowerShell from Microsoft's site.  One interesting point is that there are different versions of PowerShell for XP, Windows Server 2003 and Vista.PowerShell copy and paste

Method 1 (Quick)

  • Copy the code into memory
  • Launch PowerShell
  • Right-click the PowerShell symbolPowerShell and Ipconfig
  • Edit --> Paste
  • Press enter to execute the code
  • See screenshot to the right

Method 2 (Best)

  • 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.

Objective 2) - WMIObject | Get-Member (Discover which properties to use in our mission)

This script identifies the properties available for the object: Win32_NetworkAdapterConfiguration.  Here below is a classic example of the Get-Member construction. 

CLS
$colItems = Get-wmiobject -class "Win32_NetworkAdapterConfiguration"
$colItems | Get-Member -Membertype Property

Learning Points

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 ToolFree Download of Solarwinds  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:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Objective 3) - PowerShell: More flexible than Ipconfig

Here is the main script illustrating PowerShell's ability to display information about your TCP/IP properties.

Cls
$strComputer = "."
$colItems = Get-wmiobject -class "Win32_NetworkAdapterConfiguration" `
-computername $strComputer | Where{$_.IpEnabled -Match "True"}
foreach ($objItem in $colItems) {
write-host "MAC Address : " $objItem.MACAddress
write-host "IPAddress : " $objItem.IPAddress
write-host "IPAddress : " $objItem.IPEnabled
write-host "DNS Servers : " $objItem.DNSServerSearchOrder
Write-host ""
}

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

PowerShell Tutorials  • PowerShell Examples  • IpConfig  • PowerShell NetSh

Monitor Performance - PowerShell  • Get-Counter  • PowerShell temp

PowerShell WOL (Wake-on-Lan)  • Services   • Windows PowerShell

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: