Monday, January 9, 2012

Creating Your Own Get-Driver Tool using PowerShell

 

PowerTip of the Day, from PowerShell.com:

 

Creating Your Own Get-Driver Tool

Some code snippets are really valuable, so you should turn them into functions to keep around as new PowerShell commands. Here's a sample function:

function Get-Driver {

  param(

    $Keyword = '*'

  )

  $col1 = @{Name='File Name'; Expression={ Split-Path $_.Path -Leaf } }

  driverquery.exe /v /FO CSV |

    ConvertFrom-CSV |

    Select-Object 'Display Name',  'Start Mode', 'Paged Pool(bytes)', $col1 |

    Where-Object { ($_ | Out-String -Width 300) -like "*$Keyword*" }

}

You now have a function called Get-Driver which lists you all drivers. With just one parameter, you can also easily filter the driver list:

PS> Get-Driver mount

 

Display Name                     Start Mode          Paged Pool(bytes)          File Name

------------                     ----------          -----------------      ---------

Mount Point Manager Boot                         65.536                                         mountmgr.sys

WIMMount                                Manual                    4.096                                           wimmount.sys

 

 

PS> Get-Driver disable

 

Display Name                     Start Mode          Paged Pool(bytes)            File Name

------------                     ----------          -----------------            ---------

CD/DVD File Syst... Disabled               69.632                                         cdfs.sys

Crcdisk filter D... Disabled               4.096                                            crcdisk.sys

udfs                                           Disabled              180.224                                       udfs.sys

Winsock IFS Driver      Disabled              12.288                                          ws2ifsl.sys

It is a grep-like filter feature which returns all drivers that have your keyword in one of its properties.

 

Thanks

Finding More Driver Information using PowerShell

 

PowerTip of the Day, from PowerShell.com:

 

Finding More Driver Information

In a previous tip you learned how you can convert raw CSV data from a console application such as driverquery.exe into real PS objects. Let's play some more.

Since the CSV data only returns the complete path to a driver (which is too long to display without being truncated), let's add a "calculated property". That's a hash table with two keys: Name and Expression.

The script block in Expression takes the long path name and extracts just the file name. Now we get valuable driver information that - due to its object nature - can easily be sorted as well:

PS> $col1 = @{Name='File Name'; Expression={ Split-Path $_.Path -Leaf } }

PS> driverquery.exe /v /FO CSV | ConvertFrom-CSV | Select-Object 'Display Name',

 'Start Mode', 'Paged Pool(bytes)', $col1 | Sort-Object 'Display Name'

 

Display Name                   Start Mode                            Paged Pool(bytes   File Name

------------                   ----------                            -----------------        ---------

ACPI-Energieanze... Manual                                    4.096                                         acpipmi.sys

adp94xx                                   Manual                                    0                                                   adp94xx.sys

adpahci                                   Manual                                    0                                                   adpahci.sys

adpu320                                   Manual                                    0                                                   adpu320.sys

Agere Systems So... Manual                                    20.480                                    agrsm64.sys

aliide                                     Manual                                    0                                                   aliide.sys

AMD K8 Processor... Manual                                    28.672                                     amdk8.sys

AMD Processor Dr... Manual                                    28.672                                     amdppm.sys

amdide                                     Manual                                    0                                                   amdide.sys

amdsata                                  Manual                                    0                                                   amdsata.sys

(...)

 

Thanks

Finding Driver Information using PowerShell

 

PowerTip of the Day, from PowerShell.com:

driverquery.exe returns all kinds of information about installed drivers, but the information seems a bit useless at first:

PS> driverquery.exe /V

 

Module Name     Display Name                  Description                   Driver Type         Start Mode State          St

============ ================  ================== ============= ========== =======  ==

1394ohci           OHCI-konformer 1394-Ho OHCI-konformer 1394-Ho Kernel    Manual         Stopped     OK
20.11.2010 11:44:56    C:\Windows\system32\drivers\1394ohci.sys         4.096

This console application does support a parameter called /FO CSV. This formats the information as a comma-separated list:

PS> driverquery.exe /v /FO CSV

"Module Name","Display Name","Description","Driver Type","Start Mode","State","S

tatus","Accept Stop","Accept Pause","Paged Pool(bytes)","Code(bytes)","BSS(bytes

)","Link Date","Path","Init(bytes)"

"1394ohci","OHCI-konformer 1394-Hostcontroller","OHCI-konformer 1394-Hostcontrol

ler","Kernel ","Manual","Stopped","OK","FALSE","FALSE","4.096","200.704","0","20

.11.2010 11:44:56","C:\Windows\system32\drivers\1394ohci.sys","4.096"

Now here's the scoop: Powershell can not only read the results from console application. When the output is CSV, it can even convert the raw text automagically into objects. So, with just one line, you get tremendous information about drivers (thanks to Stephen Owen for pointing us to this):

PS> driverquery.exe /v /FO CSV | ConvertFrom-CSV | Select-Object 'Display Name',

 'Start Mode', 'Paged Pool(bytes)', Path

 

Display Name                     Start Mode               Paged Pool(bytes)       Path

------------                     ----------                -----------------      ----

OHCI-konformer 1... Manual                         4.096                                       C:\Windows\syste...

Microsoft ACPI-T... Boot                              90.112                                     C:\Windows\syste...

ACPI-Energieanze... Manual                         4.096                                       C:\Windows\syste...

(...)

 

Thanks

Creating Multiline Strings

 

PowerTip of the Day, from PowerShell.com:

You probably know what this line produces:

'Hello' * 12

Right, you get 12 times the string you submitted. If you wanted new lines instead, a minor adjustment will do the job:

,'Hello' * 12

The comma puts the string into an array, and when you multiply arrays, you get additional array elements. Simply use Out-String to turn that into a single multi-line string:

PS> $text = ,'Hello' * 12

PS> $text.GetType().FullName

System.Object[]

PS> $text.Count

12

PS> $text = ,'Hello' * 12 | Out-String

PS> $text.GetType().FullName

System.String

PS> $text

Hello

Hello

Hello

(...)

 

Thanks

Adding New Lines to Strings

 

PowerTip of the Day, from PowerShell.com:

 

In a previous tip you learned that text arrays can easily be multiplied. The same is true for assignment operators such as +=. When you apply this operator to a string, it appends a text:

PS> $text = "Hello"

PS> $text += "World"

PS> $text

HelloWorld

When you apply it to a string array, it adds new lines:

PS> $text = @()

PS> $text += "Hello"

PS> $text += "World"

PS> $text

Hello

World

PS> $text | Out-String

Hello

World

So, to construct multiline text throughout your script, start with  @() to create an empty array, then add all the lines to this array using +=. When you are done, pipe the array to Out-String to get one multiline string. Easy.

 

Thanks

Enumerating Registry Keys

 

Enumerating Registry Keys

To enumerate all subkeys in a Registry key, you might be using a line like this:

PS> Dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Select-Object -expand PSPath

Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

It will return the PowerShell-style path including the provider name, not just the Registry path. If you used the -Name parameter, on the other hand, you just get the subkey names and no path at all:

PS> Dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -Name

AddressBook

Connection Manager

(...)

The easiest way to get the true Registry path is Resolve-Path:

PS> Resolve-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*

Path

----

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

The resulting paths still use PowerShell drives. To get the native Registry paths, try this:

PS> Resolve-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object -ExpandProperty ProviderPath

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

Thanks

Use Out-GridView Requirements

 

PowerTip of the Day, from PowerShell.com

 

Out-GridView is a great way to present results in a “mini-Excel” sheet:

Get-Process | Out-GridView

However, Out-GridView has two requirements:.NET Framework 3.5.1 and the built-in script editor ISE must both be installed. ISE is not installed by default on Windows Servers. So, if you want  to use Out-GridView on server products, you will need to make sure you install the ISE feature.

On a Server 2008 R2, you could enable ISE by using PowerShell:

Import-Module ServerManager

Add-WindowsFeature PowerShell-ISE

Thanks