PowerShell2.0之Windows排错(三) 检查设备驱动
2011-01-18 15:51 @天行健中国元素 阅读(1856) 评论(0) 编辑 收藏 举报设备驱动和服务功能类似,可以自动运行并提供一定功能。只是设备驱动更接近于硬件底层,并不像服务那样容易发现和检查。设备驱动一旦出现问题,往往伴随某种设备功能的失灵,所以对于系统管理员来说检查设备驱动也很重要。
创建用于检查硬件驱动的脚本CheckDeviceDrivers.ps1,其代码如下:
param($computer="localhost", $a="h", [switch]$help)
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: CheckDeviceDrivers.ps1
Displays a listing of system drivers that are set to
automatic, manual, boot, system or all drivers
PARAMETERS:
-computer The name of the computer
-a(ction) < a(ll), r(unning), s(topped), b(oot),m(anual), au(to), sy(stem), h(elp) >
-help prints help file
SYNTAX:
CheckDeviceDrivers.ps1 -computer WebServer -a b
Displays a listing of all device drivers that are set to start on boot on a computer named WebServer
CheckDeviceDrivers.ps1 -a auto
Displays a listing of all device drivers on local computer set to start up automatically
CheckDeviceDrivers.ps1 -computer WebServer -a m
Displays a listing of all device drivers
that are set to start manually on a computer named WebServer
CheckDeviceDrivers.ps1 -help ?
Displays the help topic for the script
"@
$helpText
exit
}
if($help){ "Obtaining help ..." ; funhelp }
switch($a)
{
"a" {
"Retrieving all device drivers"
$filter = "started = 'true' or started = 'false'"
}
"r" {
"Retrieving all running device drivers"
$filter = "started = 'true'"
}
"s" {
"Retrieving all stopped device drivers"
$filter = "started = 'false'"
}
"b" {
"Retrieving boot device drivers"
$filter = "startmode = 'boot'"
}
"m" {
"Retrieving manual device drivers"
$filter = "startmode = 'manual'"
}
"au" {
"Retrieving auto device drivers"
$filter = "startmode = 'auto'"
}
"sy" {
"Retrieving system device drivers"
$filter = "startmode = 'system'"
}
"h" {
"You need to specify an action. The -a parameter is required"
"Try this: " + $MyInvocation.MyCommand.Definition + " -h"
exit
}
DEFAULT
{
"You need to specify an action. The -a parameter is required"
"Try this: " + $MyInvocation.MyCommand.Definition + " -h"
exit
}
}
$wmi = Get-WmiObject -Class win32_systemdriver `
-computername $computer -filter $filter
format-table -InputObject $wmi -property `
displayname, pathname, name -autosize
该脚本通过param语句定义了-computer,-a和-help参数。其中-a指定要执行的操作,默认为h,即显示帮助信息。
该脚本通过switch语句定义了主要操作的业务逻辑,允许用户指定一系列不同且相关的设备驱动。switch语句显示一条当前操作状态信息,然后通过设定$filter变量值,为Get-WmiObject cmdlet创建filter(筛选)参数。
该脚本中调用了可以在多个switch语句中使用的$MyInvocation.MyCommand.Definition命令,用于在输出信息中引用正在运行的脚本。其核心功能是通过Get-WmiObject cmdlet查询Win32_SystemDriver WMI类,通过指定$computer变量查询指定的计算机,然后可以使用通过switch语句创建的筛选器并将查询结果保存在$wmi变量中。为了在输出时格式化该变量,使用了带-InputObject参数的Format-Table cmdlet。同时保存$wmi变量中的management对象,执行结果如图1所示。
图1 执行结果
作者: 付海军
出处:http://fuhj02.cnblogs.com
版权:本文版权归作者和博客园共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
个人网站: http://txj.lzuer.com/