PowerShell for Windows Admins


January 24, 2010  3:39 PM

Remember Tuesday

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tuesday 26 January 7:30 GMT

PowerShell User group meeting on Windows 2008 R2 AD cmdlets

Details from

http://msmvps.com/blogs/richardsiddaway/archive/2010/01/06/uk-user-group-meeting-jan-2010.aspx

Technorati Tags: ,

January 23, 2010  6:54 AM

Disk Methods Part 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

We have seen how to pull information about our disks – what about working with them. As a quick recap on the objects that PowerShell deals with – properties give us the information and methods enable us to do stuff. So what stuff can we do?

We can find out from the documentation or we can use

Get-WmiObject -Class Win32_LogicalDisk | Get-Member -MemberType method

Name
—-
Chkdsk
Reset
SetPowerState

Get-WmiObject -Class Win32_DiskPartition | Get-Member -MemberType method

Name
—-
Reset
SetPowerState

Get-WmiObject -Class Win32_DiskDrive | Get-Member -MemberType method

Name
—-
Reset
SetPowerState

For conciseness I’ve only shown the Name of the method.

In reality the Reset and SetPowerState methods are not available because they are derived from the CIM* parent class. We’ll leave that for another day.

Chkdsk on the Logical Disk sounds familiar – lets have a look at that one

$disk = Get-WmiObject -Class Win32_LogicalDisk -Filter “DeviceId=’G:'”

$disk.ChkDsk($false, $true, $true, $false, $false, $false)

The meaning of the parameters is as follows:
FixErrors – Indicates what should be done to errors found on the disk. If true, then errors are fixed. The default is false.
VigorousIndexCheck – If true, a vigorous check of index entries should be performed. The default is true.
SkipFolderCycle – If true, the folder cycle checking should be skipped. The default is true.
ForceDismount – If true, the drive should be forced to dismount before checking. The default is false.
RecoverBadSectors –If true, the bad sectors should be located and the readable information should be recovered from these sectors. The default is false.
OKToRunAtBootUp – If true, the chkdsk operation should be performed at next boot up time, in case the operation could not be performed because the disk is locked at time this method is called. The default is false.

Now we have seen how to run this manually we need to turn it into a script.


January 19, 2010  2:26 PM

Mapping Physical Drives to Logical Drives Part 3

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Following on from last time we will extend our script to pull the information for logical drives.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
Get-WmiObject -Class Win32_DiskDrive | foreach {
    “`n {0} {1}” -f $($_.Name), $($_.Model)

    $query = “ASSOCIATORS OF {Win32_DiskDrive.DeviceID='” `
     + $_.DeviceID + “‘} WHERE ResultClass=Win32_DiskPartition”
    
    Get-WmiObject -Query $query | foreach {
        “”
        “Name : {0}” -f $_.Name
        “Description : {0}” -f $_.Description
        “PrimaryPartition : {0}” -f $_.PrimaryPartition
   
        $query2 = “ASSOCIATORS OF {Win32_DiskPartition.DeviceID='” `
        + $_.DeviceID + “‘} WHERE ResultClass=Win32_LogicalDisk”
           
        Get-WmiObject -Query $query2 | Format-List Name,
        @{Name=“Disk Size (GB)”; Expression={“{0:F3}” -f $($_.Size/1GB)}},
        @{Name=“Free Space (GB)”; Expression={“{0:F3}” -f $($_.FreeSpace/1GB)}}
   
    }
}

When we get the data for the partitions (line 7) we now put that into a foreach loop. The name, description and if its a primary partition are dumped. I’m using a string to write it because we will get a formatting error if we try to use format-list with two different objects in the same script.

A query to get the associators of the partition (as long as its a logical drive) is created and run. The results from that are displayed using the calculated fields we have seen earlier.

That concludes our look at getting information about disks. Next we’ll look at the methods these WMI classes make available – in other words what can we do with them

Technorati Tags: ,,


January 17, 2010  5:44 AM

Mapping Physical drives to logical drives part 2

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

We have seen how to get physical disk information and how to get the associations between WMI classes.  We’ll now start to put this together.

001
002
003
004
005
006
007
008
009
010
Get-WmiObject -Class Win32_DiskDrive | foreach {
    “`n {0} {1}” -f $($_.Name), $($_.Model)

    $query = “ASSOCIATORS OF {Win32_DiskDrive.DeviceID='” `
     + $_.DeviceID + “‘} WHERE ResultClass=Win32_DiskPartition”
    
    Get-WmiObject -Query $query | 
    Format-List Name, Description, PrimaryPartition

}

We start by getting the physical disk information – this is just on the local computer for now but we can easily extend this to remote machines.

For each physical drive we write out the Name and model. The –f operating performs a .NET string formatting operation on the two inputs on the right hand side and maps them into the field definitions on the left hand side.

`n causes a new line to be thrown (actually get two due to PowerShell’s internals but a gap is what we want)

We build a WMI query  to get the associated partitions for a given physical drive. Notice that use the WHERE statement in the query to restrict the class that is returned.

The partitions that are returned are listed together with their type and if it is a primary partition.

Put a USB drive in your machine and run this to see a good test.

Next job is to add the logical drive information – see if you can work it out before my next post.

Technorati Tags: ,,


January 16, 2010  4:14 AM

WQL

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I mentioned WQL last time.  if you aren’t aware of it WQL is the WMI Query Language or SQL for WMI. It is actually a subset of SQL.

We will see it used directly in queries such as the one we use to find the ASSOCIATORS and indirectly in filter statements when using Get-WmiObject.

Full reference for WQL is available at

http://msdn.microsoft.com/en-us/library/aa394606(VS.85).aspx

Technorati Tags: ,


January 15, 2010  3:49 PM

Mapping physical disks to logical drives

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

We have looked at physical disks and logical disks. We know there is a relationship between them.

disk drive – partition – logical disk

In WMI this maps to.

Win32_DiskDrive – Win32_DiskPartition – Win32_LogicalDisk.DeviceID

We have two classes that provide the links

Win32_DiskDriveToDiskPartition and Win32_LogicalDiskToPartition which we can use like this

Get-WmiObject -Class Win32_DiskDriveToDiskPartition | Select Antecedent, Dependent

Get-WmiObject -Class Win32_LogicalDiskToPartition | Format-List Antecedent, Dependent

but we then have to match the information ourselves.  if we look at the information for the C: drive

Antecedent : \\RSLAPTOP01\root\cimv2:Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE0"
Dependent  : \\RSLAPTOP01\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #1"

Antecedent : \\RSLAPTOP01\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #1"
Dependent  : \\RSLAPTOP01\root\cimv2:Win32_LogicalDisk.DeviceID="C:"

we can see that we get antecedent and dependent pairs – we can access these associations directly using a WQL query to find the ASSOCIATORS.

PS> Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=’\\.\PHYSICALDRIVE0′} " | Select __CLASS -Unique

__CLASS
——-
Win32_PnPEntity
Win32_ComputerSystem
Win32_DiskPartition
Win32_PhysicalMedia

Shows us that we have a direct association between the Win32_DiskDrive class and the Win32_DiskPartition class. Next we have to work out how to get this information for all drives and all partitions

Technorati Tags: ,,


January 12, 2010  2:41 AM

Disks Part 4: Mount points

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

We have seen how to discover our physical and logical disks.  In a quick digression I want to cover Mount Points.  These allow us to mount the volume into an empty folder in the file system rather than having a new drive. They are useful when we have lots of volumes and need to conserve drive letters.

001
002
003
004
005
006
007
008
009
010
Get-WmiObject -Class Win32_MountPoint | 
where {$_.Directory -like ‘Win32_Directory.Name="C:\\Data*"’} | 
foreach {
    $vol = $_.Volume
    Get-WmiObject -Class Win32_Volume | where {$_.__RELPATH -eq $vol} | 
    Select @{Name="Folder"; Expression={$_.Caption}}, 
    @{Name="Size (GB)"; Expression={"{0:F3}" -f $($_.Capacity / 1GB)}},
    @{Name="Free (GB)"; Expression={"{0:F3}" -f $($_.FreeSpace / 1GB)}},
    @{Name="%Free"; Expression={"{0:F2}" -f $(($_.FreeSpace/$_.Capacity)*100)}}
}

Our win32_MountPoint class only gives us directory and volume ID information.  if we want more we have to link to the Win32_Volume class.

As we know where we have mounted our volumes we can filter on that folder – note the use of c:\\data  – the \\ is a WMI syntax to escape the \.

For each mount point we find a volume with the correct ID.  I’ve use __RELPATH in the filter as it takes the same syntax as the ID in win32_mountpoint so we don’t need to do any processing.  Once we have our volume we can format the data in the way we have seen previously.

Technorati Tags: ,,


January 8, 2010  1:44 PM

Disks Part 3

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

We have seen how to use Win32_DiskDrive to look at our physical disks but this isn’t the whole story. We also need to consider logical disks or volumes

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
$dtype = DATA {
ConvertFrom-StringData -StringData @’
0 = Unknown
1 = No Root Directory
2 = Removable Disk
3 = Local Disk
4 = Network Drive
5 = Compact Disk
6 = RAM Disk
‘@

}

$media = DATA {
ConvertFrom-StringData -StringData @’
11 = Removable media other than floppy
12 = Fixed hard disk media
‘@

}

Get-WmiObject -Class Win32_LogicalDisk | 
Format-List DeviceID, Compressed, Description,
@{Name=“Drive Type”; Expression={$dtype[“$($_.DriveType)”]}},
@{Name=“Media Type”; Expression={$media[“$($_.MediaType)”]}},
FileSystem, 
@{Name=“Disk Size (GB)”; Expression={“{0:F3}” -f $($_.Size/1GB)}},
@{Name=“Free Space (GB)”; Expression={“{0:F3}” -f $($_.FreeSpace/1GB)}},
SupportsDiskQuotas,
SupportsFileBasedCompression,
VolumeName,
VolumeSerialNumber

 

 

We have a class called Win32_LogicalDisk for that. The properties are named so they are self explanatory. There are a couple of lookups to replace numeric values with text and a couple of calculations.

When you run this notice that we get information for CD drives as well – but we don’t on the physical disks. One of the nice pieces of consistency in WMI!

The problem we face next is how to link our physical disk information with our logical disk information i.e. which logical disks are on which physical disk.

Technorati Tags: ,,


January 5, 2010  1:58 PM

Disks Part 2

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Moving swiftly on from last time we can expand the information returned on our disks

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
## Configuration Manager Error Code
$cmec = DATA {
ConvertFrom-StringData -StringData @’
0 = Device is working properly.
1 = Device is not configured correctly.
2 = Windows cannot load the driver for this device.
3 = Driver for this device might be corrupted, or the system may be low on memory or other resources.
4 = Device is not working properly. One of its drivers or the registry might be corrupted.
5 = Driver for the device requires a resource that Windows cannot manage.
6 = Boot configuration for the device conflicts with other devices.
7 = Cannot filter.
8 = Driver loader for the device is missing.
9 = Device is not working properly. The controlling firmware is incorrectly reporting the resources for the device.
10 = Device cannot start.
11 = Device failed.
12 = Device cannot find enough free resources to use.
13 = Windows cannot verify the device’s resources.
14 = Device cannot work properly until the computer is restarted.
15 = Device is not working properly due to a possible re-enumeration problem.
16 = Windows cannot identify all of the resources that the device uses.
17 = Device is requesting an unknown resource type.
18 = Device drivers must be reinstalled.
19 = Failure using the VxD loader.
20 = Registry might be corrupted.
21 = System failure. If changing the device driver is ineffective, see the hardware documentation. Windows is removing the device.
22 = Device is disabled.
23 = System failure. If changing the device driver is ineffective, see the hardware documentation.
24 = Device is not present, not working properly, or does not have all of its drivers installed.
25 = Windows is still setting up the device.
26 = Windows is still setting up the device.
27 = Device does not have valid log configuration.
28 = Device drivers are not installed.
29 = Device is disabled. The device firmware did not provide the required resources.
30 = Device is using an IRQ resource that another device is using.
31 = Device is not working properly. Windows cannot load the required device drivers.
‘@

}

Get-WmiObject -Class Win32_DiskDrive | 
Format-List DeviceID, Status, 
@{Name=“Configuration Manager Error Code”; Expression={$cmec[“$($_.ConfigManagerErrorCode)”]}},
Index, InterfaceType, 
Partitions, BytesPerSector, SectorsPerTrack, TracksPerCylinder,
TotalHeads, TotalCylinders, TotalTracks, TotalSectors,
@{Name=“Disk Size (GB)”; Expression={“{0:F3}” -f $($_.Size/1GB)}}

Most of this script is taken up with defining the configuration error codes!

The other aspects are similar to our OS script.  Now we can know what our physical disks are like we need to look at the logical disks and incorporate that information to get the full story.

Once we have finished with this series on disks I’ll post the scripts so that they can be downloaded.

Technorati Tags: ,,


January 3, 2010  8:36 AM

Disks Part 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Disk storage and everything associated with that is a big part of administering our servers. We need to be able to discover, and where necessary, work with the storage system. WMI gives us a number of classes for working with disks

PS> Get-WmiObject -List Win32_*disk* | select name

Name
—-
Win32_LogicalDisk
Win32_MappedLogicalDisk
Win32_DiskPartition
Win32_DiskDrive
Win32_LogicalDiskRootDirectory
Win32_DiskQuota
Win32_LogonSessionMappedDisk
Win32_LogicalDiskToPartition
Win32_DiskDrivePhysicalMedia
Win32_DiskDriveToDiskPartition
Win32_PerfFormattedData_PerfDisk_LogicalDisk
Win32_PerfRawData_PerfDisk_LogicalDisk
Win32_PerfFormattedData_PerfDisk_PhysicalDisk
Win32_PerfRawData_PerfDisk_PhysicalDisk

PS> Get-WmiObject -List Win32_*volume* | select name

Name
—-
Win32_VolumeChangeEvent
Win32_Volume
Win32_VolumeQuota
Win32_VolumeQuotaSetting
Win32_VolumeUserQuota
Win32_ShadowVolumeSupport
Win32_ShadowDiffVolumeSupport

PS> Get-WmiObject -List Win32_*drive* | select name

Name
—-
Win32_DiskDrive
Win32_FloppyDrive
Win32_TapeDrive
Win32_CDROMDrive
Win32_DiskDrivePhysicalMedia
Win32_DiskDriveToDiskPartition

Note that the last list has been edited to exclude those classes to work with drivers

Our starting point has to be the physical drives.

PS> Get-WmiObject -Class Win32_DiskDrive

Partitions : 3
DeviceID   : \\.\PHYSICALDRIVE0
Model      : ST9250320AS ATA Device
Size       : 250056737280
Caption    : ST9250320AS ATA Device

As with the Operating System there is a lot of information behind this – details from http://msdn.microsoft.com/en-us/library/aa394132(VS.85).aspx

We’ll dig further into disks in the next series of posts

Technorati Tags: ,,


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: