PowerShell for Windows Admins


August 27, 2011  3:18 PM

Msndis class



Posted by: Richard Siddaway
Network

The MSNdis class from the root\wmi namespace calls the MSNdis_CoHardwareStatus and MSNdis_CoDriverVersion classes. On my Windows 7 system it tries to call something else but I get a “Get-WmiObject : Not supported” error. Neither of these classes seems to have much in the way of documentation available.

Rather than calling MSNdis and getting a lot of WMI objects it would be better to combine the output. There are a set of MSNdis-co* classes

gwmi -Namespace root\wmi -List *ndis_co* | sort name

MSNdis_CoDriverVersion
MSNdis_CoHardwareStatus
MSNdis_CoLinkSpeed
MSNdis_CoMacOptions
MSNdis_CoMediaConnectStatus
MSNdis_CoMediaInUse
MSNdis_CoMediaSupported
MSNdis_CoMinimumLinkSpeed
MSNdis_CoReceivePduErrors
MSNdis_CoReceivePdusNoBuffer
MSNdis_CoReceivePdusOk
MSNdis_CoTransmitPduErrors
MSNdis_CoTransmitPdusOk
MSNdis_CountedString
MSNdis_CoVendorDescription
MSNdis_CoVendorDriverVersion
MSNdis_CoVendorId

Lets see what we can find out about them

 

 

function get-adaptercoinfo {            
param(            
 [string]$computer="."            
)            
Get-WmiObject -Namespace root\wmi -Class MSNdis_CoHardwareStatus `
-ComputerName $computer |            
foreach {            
                
  $drv = Get-WmiObject -Namespace root\wmi  `
  -Class MSNdis_CoDriverVersion -ComputerName $computer `
  -Filter "InstanceName='$($_.InstanceName)'"            
            
  New-Object -TypeName PSobject -Property @{            
    Computer = $_.__SERVER            
    Adapter = $_.InstanceName            
    Active = $_.Active            
    HardwareStatus = $_.NdisCoHardwareStatus            
    DriverVersion = $drv.NdisCoDriverVersion             
  }            
}            
}

Start with the MSNdis_CoHardwareStatus and match to MSNdis_CoDriverVersion  on instance name (associators don’t seem to be created for these classes). Put the appropriate properties into an object and output.

The output is best displayed like this

get-adaptercoinfo | ft -a

August 26, 2011  12:59 PM

European PowerShell Deep Dive–presenting



Posted by: Richard Siddaway
PowerShell

I was notified last night that my submission to the Deep Dive was successful and I will be presenting. There will be a good number of PowerShell MVPs attending. This will be the PowerShell event of the year in Europe. The one in April in the USA was brilliant – this will be at least as good.

Event details from http://blogs.msdn.com/b/powershell/archive/2011/08/02/extending-discounted-registration-amp-session-proposal-deadline.aspx


August 25, 2011  12:46 PM

Network Adapter details



Posted by: Richard Siddaway
Network

In this post -http://itknowledgeexchange.techtarget.com/powershell/linking-the-network-card-to-the-registry-settings/  – I showed how to get the NetLuidIndex by linking the network card to the matching registry settings.  There is another way to get some that same information.

function get-adapter {            
param(            
 [string]$computer="."            
)            
Get-WmiObject -Namespace root\wmi -Class MSNdis_EnumerateAdapter `
-ComputerName $computer |            
foreach {            
 $nic = Get-WmiObject -Namespace root\wmi -Class MSNdis_EnumerateAdapterEx `
-ComputerName $computer -Filter "InstanceName = '$($_.InstanceName)'"            
            
$header = $nic.EnumerateAdapter.Header            
            
 New-Object -TypeName PSobject -Property @{            
  Computer = $_.__SERVER            
  Adapter = $_.InstanceName            
  Device = $_.DeviceName            
  Active = $_.Active            
  Index = $($nic.EnumerateAdapter.IfIndex)            
  NetLuid = $($nic.EnumerateAdapter.NetLuid)            
  Revision = $header.Revision            
  Size = $header.Size            
  Type = $header.Type            
 }            
}            
}

Use the MSNdis_EnumerateAdapter  class to get the adapters and match to the MSNdis_EnumerateAdapterEx  class on the Instance name. Not sure what size and type signify but they are identical on all adapters on my Windows 7 machines.

 

NOTE: The Index property is NOT the same as the DeviceId/Index property on the Win32_NetworkAdapter\Win32_NetworkAdapterConfiguration classes.


August 24, 2011  2:25 PM

Testing the network speed



Posted by: Richard Siddaway
Network

In the last post I showed how to use the MSNdis_LinkSpeed class. Digging into the class information I found a suggestion that the speed returned was in Kbps. The answers didn’t look right especially when they suggested my wireless network was running at 540Mbps!

At bit of experimentation produced this

function test-linkspeed{            
param(            
 [string]$computer="."            
)            
            
Get-WmiObject -Namespace root\wmi -Class MSNdis_LinkSpeed `
-ComputerName $computer |             
foreach {            
 $nic = Get-WmiObject -Class Win32_NetworkAdapter `
 -ComputerName $computer -Filter "Name = '$($_.InstanceName)'"            
            
New-Object -TypeName PSObject -Property @{            
 Name = $_.InstanceName            
 NdisLinkSpeed = $_.NdisLinkSpeed            
 TestNdis = $_.NdisLinkSpeed / 10000            
 Speed = $nic.Speed            
 Test = $nic.Speed / 1000000            
} | select Name, NdisLinkSpeed, TestNdis, Test, Speed            
            
}            
            
}

Use the MSNdis_LinkSpeed class as before. Pipe into foreach and get the corresponding Win32_NetworkAdapter class. I know that the speed reported there is in bits per second. Create an object that shows the speeds reported by the two classes, convert Win32_NetworkAdapter speed to Mbps and play around with the divisor for Ndis speed until it matches. Turns out MSNdis_LinkSpeed reports in 100s of bits per second.

Odd but fun


August 24, 2011  2:15 PM

Network Link Speed



Posted by: Richard Siddaway
Network

The root\wmi Namespace contains a class that just returns the link speed of your network card. Quick and easy to use function.

function get-linkspeed{            
param(            
 [string]$computer="."            
)            
Get-WmiObject -Namespace root\wmi -Class MSNdis_LinkSpeed `
-ComputerName $computer |             
select InstanceName, @{N="Speed(Mbps)"; E={$_.NdisLinkSpeed/10000}}            
            
}

The documentation I could find suggested that the NdisLinkSpeed property returned Kbps but that gave some odd results. Experimentation and a bit more research suggested I needed to divide by 10000.


August 22, 2011  3:05 PM

TechEd Australia PowerShell conference



Posted by: Richard Siddaway
PowerShell v2

Tuesday August 30th 2011 at 9am EST there is a PowerShell conference at TechEd Australia. 

Details from

http://powershelldownunder.com/featured/teched2011/

The sessions will be available via Live meeting please register at above URL


August 22, 2011  1:03 PM

Ping a subnet



Posted by: Richard Siddaway
Network

I recently needed to find which IP addresses were active on a subnet to track down a printer. Quickest way was to ping them.

 

function ping-subnet {            
param (            
 [parameter(Mandatory=$true)]            
 [ValidatePattern("\b\d{1,3}\.\d{1,3}\.\d{1,3}\b")]            
 [string]$subnet            
)            
            
0..255 | foreach {             
             
 $address = "$subnet.$_"            
            
 if (Test-Connection -ComputerName $address -Count 1 -Quiet){            
    Test-Connection -ComputerName $address -Count 1            
 }            
            
}            
}

 

Put the subnet in as a parameter. Then work through 0 – 255 building an address from the subnet. use test-connection in quiet mode and if it responds do a 1 count test-connection (ping) to get the result.

Not necessarily the quickest but it was quick to throw together and it works


August 21, 2011  12:24 PM

Create a process on a remote machine



Posted by: Richard Siddaway
PowerShell v2, WMI

We can use the [wmiclass] to create a process but it doesn’t allow us to set the credentials. We can get round that by using a bit of .NET code. [wmiclass] is as accelerator for System.Management.ManagementClass so we go back to basics

function new-process {            
param (            
 [string]$computer="localhost",            
 [string]$procpath="C:\Program Files\Internet Explorer\iexplore.exe"            
)            
            
$conopt = New-Object System.Management.ConnectionOptions             
            
switch ($computer ) {            
 "."         {break}            
 "localhost" {break}            
 "$env:COMPUTERNAME" {break}            
 default {            
           $cred = Get-Credential            
           $conopt.UserName = $cred.UserName            
           $conopt.SecurePassword = $cred.Password            
         }            
}            
$conopt.EnablePrivileges = $true            
            
$scope = New-Object System.Management.ManagementScope             
$scope.Path = "\\$computer\root\cimv2"             
$scope.Options = $conopt             
            
$path = New-Object System.Management.ManagementPath            
$path.ClassName = "Win32_Process"              
            
$proc = New-Object System.Management.ManagementClass($scope, $path, $null)             
            
$proc.Create($procpath)             
}

 

The computer name and path to the exe we want to run are given as parameters. We create the System.Management.ConnectionOptions. If we are  targeting a remote machine we can add the credentials (doesn’t work for local machine). The switch simplifies the coding of avoid local machine

 

The scope and management path (name space and class) are set and then we create a new instance of the class. We can then use the Create method to create the process.


August 21, 2011  11:01 AM

Ethernet Errors



Posted by: Richard Siddaway
Network

While we are looking at the MSNdis_Ethernet* classes we should consider these three that pick up potential problems

MSNdis_EthernetReceiveErrorAlignment
MSNdis_EthernetOneTransmitCollision
MSNdis_EthernetMoreTransmitCollisions

 

If all is well we will get an answer of zero for each of them – no errors and no collisions. Its a bit awkward working through each class in turn and we get a lot of “noise” returned so the following function concatenates the results.

function test-etherneterror {            
param(            
 [string]$computer="."            
)            
Get-WmiObject -Namespace root\wmi -Class MSNdis_EthernetReceiveErrorAlignment `
-ComputerName $computer |            
foreach {            
                
  $one = Get-WmiObject -Namespace root\wmi  `
  -Class MSNdis_EthernetOneTransmitCollision -ComputerName $computer `
  -Filter "InstanceName='$($_.InstanceName)'"            
            
  $more = Get-WmiObject -Namespace root\wmi  `
  -Class MSNdis_EthernetMoreTransmitCollisions -ComputerName $computer `
  -Filter "InstanceName='$($_.InstanceName)'"            
            
  New-Object -TypeName PSobject -Property @{            
  Computer = $_.__SERVER            
  Adapter = $_.InstanceName            
  ReceiveError = $_.NdisEthernetReceiveErrorAlignment            
  OneCollision = $one.NdisEthernetOneTransmitCollision            
  MoreCollision = $more.NdisEthernetMoreTransmitCollisions            
 }            
}            
}

Start by getting the members of the MSNdis_EthernetReceiveErrorAlignment  class. Loop through them and get the related MSNdis_EthernetOneTransmitCollision  and MSNdis_EthernetMoreTransmitCollisions classes.

 

Create an object for output


August 20, 2011  12:29 PM

Powershell and WMI–MEAP Chapters 12 and 13



Posted by: Richard Siddaway
Books, PowerShell, WMI

Chapters 12 and 13 have been added to the early access program.

 

Chapter 12: Internet Information Server (IIS) may not be the first application that comes to mind when we think of using WMI to administer remote servers, but it provides a lot of functionality. You’ll learn to administer the web server itself, websites, applications, and application pools.

Chapter 13: Configuring a new server is a task that occurs on a regular basis in most organizations. All of the activities required to configure a new server take time. You can use PowerShell functions to perform these tasks remotely so you don’t need to spend time accessing the server directly.

The code for all chapters is available for download.

 

The MEAP is available from http://www.manning.com/siddaway2/


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: