Categories

Monthly Archives: September 2008

PowerShell in Practice

Chapter 5 - user accounts (local and AD) is now available on MEAP - http://www.manning.com/siddaway/

 

Share this post :

 

Technorati Tags:

 

W2KSG: Service Pack

Continuing our look at Operating Systems - what about Service Pack information

Listing 8.8

Get-WmiObject -Class Win32_OperatingSystem | Format-List ServicePackMajorVersion, ServicePackMinorVersion

In this one I'm using Format-List to perform the selection as well as the display.  We could make it simpler if we used the -property parameter on get-wmiobject but that also displays the WMI info such as class, genus etc, etc

 

Share this post :

 

Technorati Tags: ,

W2KSG: Operating System

The other area we have to dig into when inventorying systems is the OS.  WMI has a class just for this

PS> Get-WmiObject -Class Win32_OperatingSystem

SystemDirectory : C:\Windows\system32
Organization    :
BuildNumber     : 6001
RegisteredUser  : admin
SerialNumber    : 55041-037-8508545-71489
Version         : 6.0.6001

This doesn't give us everything we might wish for.  The default formatter is a play here in that it decides what should be displayed if you don't choose.  To see what is available try

Get-WmiObject -Class Win32_OperatingSystem | Select *

which will display all properties.  If we want to be a bit more selective lets try

Listing 8.7

Get-WmiObject -Class Win32_OperatingSystem | Select BootDevice, BuildNumber,BuildType, Caption, Codeset, CountryCode, Debug, InstallDate, NumberofLicensedUsers, Organization, OSLanguage, OSProductSuite, OSType, Primary, RegisteredUser, SerialNumber, Version

These scripts illustrate one of the most difficult aspects of WMI - knowing what is available.

 

Share this post :

 

Technorati Tags: ,

W2KSG: Inventory Hardware

Most of the hardware connected to your system has a matching WMI class - cd, sound, keyboard, network adapter, printers and usb for example.  To find the appropriate WMI classes use

Get-WmiObject -List *networkadapter

Or similar

If you want to investigate the pointing devices try

Listing 8.4

Get-WmiObject -Class Win32_PointingDevice | Select HardwareType, NumberofButtons, Status, PNPDeviceId | Format-List

Oddly enough all the different devices report 0 butons!

It was pointed out on an earlier script that I could have effectively combined the select and format-list by changing the script to

Get-WmiObject -Class Win32_PointingDevice | Format-List HardwareType, NumberofButtons, Status, PNPDeviceId

The reason I don't do this automatically is that I usually leave the formatting until the end when I see what the default formatter produces and I find is easier (lazier :-) ) to just add format-list on the end.  Also if I want to put the script into PowerGUI not having the format-list makes life easier.

 

Share this post :

 

Technorati Tags: ,

W2KSG: BIOS Information

Previously we looked at retrieving computer system information using WMI.  We looked at the BIOS information in passing.  There is a lot more information held in the BIOS

Listing 8.2

Get-WmiObject -Class Win32_Bios | Select -Property BuildNumber, CurrentLanguage, InstallableLanguages, Manufacturer, Name, PrimaryBIOS, ReleaseDate, SerialNumber, SMBIOSBIOSVersion, SMBIOSMajorVersion, SMBIOSMinorVersion, SMBIOSPresent, Status, Version, BiosCharacteristics

Using the Win32_Bios class we just select the properties we want to examine.  This is 1 line. The original took 20 lines.

If you want to expand the BiosCharacteristics to see the integer values then you can use

Get-WmiObject -Class Win32_Bios | Select -ExpandProperty BiosCharacteristics

 

Share this post :

 

Technorati Tags: ,

W2KSG: Retrieving System Information

I am going to jump on a bit in the Scripting Guide and skip to chapter 8 - Computer Assets.  (Chapter 7 is working with AD which we will return to later). Chapter 8 uses WMI to work with your computer systems.  In these examples we are working with the local machine but we can use the computername parameter to work with remote machines.

The section of the Scripting Guide we are dealing with can be found at

Script Center Home > Microsoft Windows 2000 Scripting Guide > Scripting Solutions for System Administration > Computer Assets > Computer Assets Overview  Retrieving System Information

Lisiting 8.1

"Operating System"
Get-WmiObject -Class Win32_OperatingSystem | Select Name, Version,ServicePackMajorVersion, ServicePackMinorVersion, Manufacturer, WindowsDirectory, Locale, FreePhysicalMemory, TotalVirtualMemorySize, FreeVirtualMemory | Format-List

"Computer System"
Get-WmiObject -Class Win32_ComputerSystem | Select Name, Manufacturer, Model, CurrentTimeZone, TotalPhysicalmemory | Format-List

"Processor"
Get-WmiObject -Class Win32_Processor | Select Architecture, Description | Format-List

"Bios"
Get-WmiObject -Class Win32_Bios | Select Version | Format-List

We can use WMI to retrieve information on the Operating System, Computer, processor and Bios.  Each of these WMI classes has a host of other information.  Investigate the options and you can easily generate a script to document your systems. Note I have used 4 lines of PowerShell to duplicate the 33 lines on VBScript in the Guide's example.

 

Share this post :

 

Technorati Tags: ,

PowerShell reading SQL Server logs

Thanks to Dmitry - http://dmitrysotnikov.wordpress.com/2008/09/26/more-sql-cmdlets/ -  and Jacob - http://mspowershell.blogspot.com/2008/09/read-transactionlog-of-sql-server-from.html - for pointing out the PowerShell cmdlets in Quest's SQL Lite Speed.  You can read the transaction log!!

Download a trial version from http://www.quest.com/litespeed-for-sql-server/

 

Share this post :

 

Technorati Tags: ,

W2KSG: Folders Creation date

WMI has a class Win32_Directory for working with folders on a machine. We can use this class to find all folders created this month.

Listing 6.22

$d = "'" + (Get-WmiObject -Class Win32_Directory -Filter "name='c:\\'").ConvertFromdateTime([datetime]"09/01/2008") + "'"
Get-WmiObject -Class Win32_Directory -Filter "CreationDate > $d" | Select Name, CreationDate

We start by creating a date in WMI format.  We use the Win32_Directory WMI class and filter on the root folder for speed. Note that we have to use \\ instaed of \ when dealing with file paths in WMI.  We then use the ConvertFromDateTime method to put the required date into WMI format.  The date will look like this

'20080901000000.000000+060'

Note that we have put ' ' round the date.  This is needed for the correct formatting of the WMI query.

We then use Win32_Directory again and filter based on dates greater than our test date i.e. folders created this month.

The really great thing about this is that we could use -computername to specify a remote machine. We could modify the script to read a file of machine names and test our servers for new folders.

 

Share this post :

 

Technorati Tags: ,

 

W2KSG: Time Zones

The WMI section of the scripting guide gives a very simple method of determining the time zone on your machine

Listing 6.20

Get-WmiObject Win32_TimeZone | Format-List

Bias      : 0
SettingID :
Caption   : (GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London

Bias returns the offset from GMT and the caption returns the time zone.  Information on daylight saving settings is also available - try get-member on it!

 

Share this post :

 

Technorati Tags: ,

PowerShell poll

Sapien are running a poll - http://blog.sapien.com/index.php/2008/09/23/powershell-production-poll/ - asking if you are using PowerShell in production. 

It may be a self selecting group that is answering but the results are interesting and pleasing

 

Share this post :

 

Technorati Tags: