PowerShell for Windows Admins


May 30, 2011  7:36 AM

PowerShell UG–June 2011 reminder

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Don’t forget to add the June meeting of the UK PowerShell User group to your calendar.  Its June 21 at 7.30pm BST. Details available from

http://msmvps.com/blogs/richardsiddaway/archive/2011/05/10/powershell-uk-user-group-june-meeting.aspx

May 29, 2011  2:57 AM

Manning Deal of the Day–29 May

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Today, for one day only, get 50% off PowerShell and WMI MEAP or MEAP+ebook.

 

The code is dotd0529cc when you order from www.manning.com

 

The same code can be used for PowerShell in Action – second edition and PowerShell in Practice


May 27, 2011  12:17 PM

root\wmi–MSBatteryClass

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A number of the classes in root\wmi return results from more than one class. That sounds odd but it can be explained by an example.

The namespace contains a number of classes related to the battery in laptops

gwmi -Namespace root\wmi -List *battery*

MSBatteryClassEvent
BatteryStatusChange
BatteryTagChange
MSBatteryClass
BatteryStaticData
BatteryRuntime
BatteryCycleCount
BatteryTemperature
BatteryStatus
BatteryFullChargedCapacity

 

We’ll ignore the event and change classes for now.  If we pick out the MSBattery class we get information from a number of other classes returned – MSBattery is a super class.

PS> gwmi -Namespace root\wmi -Class MSBatteryClass | select __class

__CLASS
——-
BatteryCycleCount
BatteryFullChargedCapacity
BatteryStaticData
BatteryRuntime
BatteryStatus

 

Be aware that the runtime property returned by BatteryRuntime doesn’t respond to calculations you may find on the Internet when your OS is Windows 7

Battery status is a useful class to determine if you are on battery or external power & if the battery is charging


May 26, 2011  12:50 PM

root\wmi – speeding the testing

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Testing the individual classes in root\wmi is a pain – so its time for some brute force.  I’ll select a group of classes and test the selection

gwmi -Namespace root\wmi -List system* | fw

 

I can then iterate through them calling get-wmiobject.

This is a command line activity so aliases are OK

gwmi -Namespace root\wmi -List system* | foreach {gwmi -Namespace root\wmi -Class $_.Name}

 

I could do this for all classes but it could become a bit difficult sorting out results if I get a lot of them.  We’ll see where this goes.


May 26, 2011  12:42 PM

root\wmi – MS_SystemInformation

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Continuing our exploration of the murky jungle that is the root\wmi namespace we find a number of classes related to system configuration

gwmi -Namespace root\wmi -List *system* | fw

 

Of theses the only one I could get a respnse from on Windows 7 or Windows 2008 R2 was MS_SystemInfo.

gwmi -Namespace root\wmi -Class MS_SystemInformation

Active                 : True
BaseBoardManufacturer  : Wistron
BaseBoardProduct       : 303C
BaseBoardVersion       : 08.48
BiosMajorRelease       : 15
BiosMinorRelease       : 52
BIOSReleaseDate        : 12/23/2008
BIOSVendor             : Hewlett-Packard
BIOSVersion            : F.34
ECFirmwareMajorRelease : 255
ECFirmwareMinorRelease : 255
InstanceName           : Root\mssmbios000_0
SystemFamily           : 103C_5335KV
SystemManufacturer     : Hewlett-Packard
SystemProductName      : HP G60 Notebook PC
SystemSKU              : NF300EA#ABU
SystemVersion          : F.34

 

It seems to combine some information from

Win32_ComputerSystem

Win32_BaseBoard

Win32_Bios


May 25, 2011  1:17 PM

root\wmi – ProcessorBiosInfo

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The root\wmi namespace contains a massive number of classes. Unfortunately there appears to be very little documentation available for these classes. Another issue is that many of classes do not appear to return anything (at least on my Windows 7 system – further testing is required).

In this smalls series I intend to dig through some of these classes and see if we can find anything useful

There are a number of classes associated with the processor:

PS> Get-WmiObject -Namespace ‘root\wmi’ -List *Processor* | fw

ProcessorCStateEvent                ProcessorPerfStateEvent
ProcessorThrottleStateEvent         ProcessorAcpiCsdDependency
MSProcessorClass                    ProcessorBiosInfo
ProcessorBiosTStates                ProcessorStatus
ProcessorAcpiCsd                    ProcessorAcpiTsd
ProcessorBiosCStates                ProcessorAcpiCst
ProcessorAcpiXpss                   ProcessorAcpiTssState
ProcessorAcpiCstState               ProcessorAcpiTsdDependency
ProcessorPerformance                ProcessorAcpiXpssState
ProcessorAcpiTss

 

Out of this list the ProcessorBiosInfo works on Windows 7

PS> Get-WmiObject -Namespace ‘root\wmi’ -Class ProcessorBiosInfo

Active           : True
ApicId           : 0
InstanceName     : ACPI\AuthenticAMD_-_x86_Family_17_Model_3_-_AMD_Athlon_Dual-Core_QL-62\_0_0
NtNumber         : 0
PBlk             : 4112
PBlkLen          : 6
Pct              : System.Management.ManagementBaseObject
ProcessorId      : 0
Pss              : System.Management.ManagementBaseObject

 

Active and Instance name are obvious. The NtNumber is the OS processor Id

PBlk refers to the processor control block – address and length

 

The Pct and Pss objects can be drilled down into

 

$cpu = Get-WmiObject -Namespace ‘root\wmi’ -Class ProcessorBiosInfo
$cpu[0]
$cpu[0].Pct
$cpu[0].Pct.Control
$cpu[0].Pct.Status
$cpu[0].Pss
$cpu[0].Pss.State

Follow these through to see what may be useful.

We can see everything like this

001
002
003
004
005
006
007
008
009
010
Get-WmiObject -Namespace ‘root\wmi’ -Class ProcessorBiosInfo |
foreach {
 $_ | select * -ExcludeProperty __*
 
 $_.Pct.Control | select * -ExcludeProperty __*
 $_.Pct.Status | select * -ExcludeProperty __*
 
 $_.Pss.State | select * -ExcludeProperty __*

}

 

The class description just returns the following

ACPI Bios Processor Information


May 22, 2011  4:35 AM

New PAM version

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve added the 0.6 release of the PowerShell Admin Modules to codeplex – http://psam.codeplex.com/

This release adds two modules

PAMADSNAPSHOT – functions for work with AD snapshots in Windows 2008 & 2008 R2

PAMLOAD – loads all of the PAM modules in one command

Import-Module PAMLOAD –Force

 

Further releases are planned during the year


May 21, 2011  3:01 AM

Testing network connectivity

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

One of the standard troubleshooting tasks when investigating a problem is deciding if the machine can communicate on the network. The approach is usually

  • ping the loop back address to check TCP/IP is working
  • ping the machines own address
  • ping the default gateway
  • ping other servers

This means running ipconfig to discover some of the information and  then running pings

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
function test-networkconnectivity {
[CmdletBinding()]
param()

$nic = Get-WmiObject Win32_NetworkAdapterConfiguration `
-Filter "DHCPEnabled = $true AND IPEnabled = $true"

Write-Verbose "TCP/IP Stack"
Test-Connection -ComputerName 127.0.0.1

Write-Verbose "Local Address"
Test-Connection -ComputerName $nic.IPAddress[0]

Write-Verbose "Default Gateway"
Test-Connection -ComputerName $nic.DefaultIPGateway

Write-Verbose "DNS Server"
foreach ($address in $nic.DNSServerSearchOrder){
Test-Connection -ComputerName $address}

}

 

We can simplify this action.  Use WMI to get the data (I’m assuming we are doing this on a client) from the DHCP enabled NIC. I added the filter for IPEnabled to filter out BlueTooth adapters.

We can then use Test-Connection to perform the pings.  The various results are labelled accordingly if we use the –verbose switch

test-networkconnectivity -Verbose


May 19, 2011  1:22 PM

PowerShell in Action

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The second edition of Bruce Payette’s PowerShell in Action is available. It  can be ordered from http://www.manning.com/payette2/.  The electronic version includes a free electronic version of the first edition.

This is “THE” book on the PowerShell language – why it works the way it does and how some of the design decisions were reached.  I’ll post a full review later.

BE WARNED – This is NOT a book for beginners to PowerShell.


May 19, 2011  12:31 PM

Method definitions

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

When we are dealing with .NET objects we have methods and properties to deal with. Properties are easy.

lets create a simple object

$str = "QWERTYUIOP"

 

put our string object into get-member to see the properties

$str | Get-Member -MemberType property

 

In this case we get one property.

Methods we can get like this

$str | Get-Member -MemberType method

 

and we find there are 33 of them on a string object.  Some of the methods can be used in different ways i.e. have different definitions. For instance the substring method has a couple of definitions

PS> $str.substring.OverloadDefinitions
string Substring(int startIndex)
string Substring(int startIndex, int length)

 

When we look at the output of get-member for a method such as Replace we get this

Replace          Method     string Replace(char oldChar, char newChar), string Replace(string oldValue, string newVa…

Ideally we want to be able to see all of the definitions.  We could use

$str | Get-Member -MemberType method | Format-Table –wrap

 

but its not easy to read.  If you want to dig into the method definitions try this

 

001
002
003
004
005
006
007
008
009
010
011
012
013
function get-methoddefinitions {
 [CmdletBinding()]
 param ($obj)
 
 $obj | Get-Member -MemberType method | select name |
 foreach {
   $_.Name
  
   $cmd = ‘$obj.’ + "$($_.Name).Overloaddefinitions"
   Invoke-Expression -Command $cmd 
   ""
  } 
}

 

We can use string substitution to get the method name into the string and then run it with Invoke-Expression.  Note how we use single quotes on the first part of the string to prevent substitution.  Our output for the replace method becomes

Replace
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)

which is easy to read. 

The function could be extended to accept a method name to avoid displaying everything.


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: