PowerShell2.0之Windows排错(五)查看硬件问题


硬件问题并不一定都和硬件有关,只要工作负荷在设计的范围内,大部分电子设备都可以使用相当长的一段时间。为了硬件正常工作,需要安装相应的驱动程序。硬件厂商会为其驱动程序添加数字签名,添加数字签名的驱动都是厂商经过大量测试后通过的,可使设备高效运转的驱动;未经签名的驱动程序可能是导致硬件问题的主要原因。

为了检查硬件是否运行厂商认证的驱动程序,创建名为“CheckSignedDeviceDrivers.ps1”的脚本,其代码如下:

param(

$computer="localhost",

[switch]$unsigned,

[switch]$full,

[switch]$help

)

function funline ($strIN)

{

$num = $strIN.length

for($i=1 ; $i -le $num ; $i++)

{ $funline += "=" }

Write-Host -ForegroundColor green $strIN

Write-Host -ForegroundColor darkgreen $funline

}

function funHelp()

{

$helpText=@"

DESCRIPTION:

NAME: CheckSignedDeviceDrivers.ps1

Displays a listing of device drivers that are

and whether they are signed or not

PARAMETERS:

-computer the name of the computer

-unsigned lists unsigned drivers

-full lists Description, driverProviderName,

Driverversion,DriverDate, and infName

-help prints help file

SYNTAX:

CheckSignedDeviceDrivers.ps1 -computer munich -unsigned

Displays a listing of all unsigned drivers

on a computer named munich

CheckSignedDeviceDrivers.ps1 -unsigned -full

Displays a listing of all unsigned drivers on local

computer. Lists Description, driverProviderName,

Driverversion,DriverDate, and infName of the driver

CheckSignedDeviceDrivers.ps1 -computer munich -full

Displays a listing of all signed drivers

a computer named munich. Lists Description, driverProviderName,

Driverversion,DriverDate, and infName of the driver

CheckSignedDeviceDrivers.ps1 -help ?

Displays the help topic for the script

"@

$helpText

exit

}

if($help){ "Obtaining help …" ; funhelp }

if($unsigned)

{ $filter = "isSigned = ‘false’" ; $mode = "unsigned" }

ELSE

{ $filter = "isSigned = ‘true’" ; $mode = "signed" }

$property = "Description", "driverProviderName", `

"Driverversion","DriverDate","infName"

$wmi = Get-WmiObject -Class Win32_PnPSignedDriver `

-computername $computer -property $property -filter $filter

funline("There are $($wmi.count) $mode drivers isted below:")

if($full)

{

format-list -InputObject $wmi -property `

$property

}

ELSE

{

format-table -inputobject $wmi -Property description

}

其中通过param语句声明了4个参数,-computer用于指定操作的目标计算机;-unsigned是个switch参数,用于返回未曾签名的驱动的查询结果;-full参数列出有问题的驱动程序的详细信息;-help参数用于显示帮助文件。如果存在$unsigned变量,则将字符串“isSigned=’false’”指定给$filter变量,该变量用于为Get-WmiObject cmdlet提供-filter参数。随后在$mode变量中保存状态信息,用于指定WMI查询的类型;如果不存在$unsigned变量,则检查带签名的驱动程序。

通过数组方式指定相应的属性名,创建变量$property并传递给Get-WmiObject cmdlet查询Win32_PnPSignedDriver WMI类;另外还使用Count属性统计符合条件的驱动程序数量。如果没有签名的驱动程序,则输出之前计数器的值为空,而不是0。该脚本的执行结果如图8所示。

image

图8 执行结果

需要强调的是由于Windows Vista和Windows Server 2008与Windows XP硬件驱动的WMI管理类存在差别,所以该脚本仅显示在Windows Vista和Windows Server 2008系统。

作者: 付海军
版权:本文版权归作者所有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
个人网站: http://txj.lzuer.com/


发表回复