WMI 任务:桌面管理
本文内容
用于桌面管理的 WMI 任务可以施加控制,并从远程桌面或本地计算机获取数据。 例如,你可以确定本地计算机上的屏幕保护程序是否需要密码。 WMI 还能够关闭远程计算机。 有关其他示例,请通过 https://www.microsoft.com/technet 访问 TechNet ScriptCenter。
本主题中所示的脚本示例仅从本地计算机获取数据。 有关如何使用脚本从远程计算机获取数据的详细信息,请参阅连接到远程计算机上的 WMI 。
以下过程介绍了如何运行脚本。
运行脚本
复制代码并将其保存在扩展名为 .vbs 的文件中,例如 filename.vbs。 确保文本编辑器不会向该文件添加 .txt 扩展名。
打开命令提示符窗口并导航到保存该文件的目录。
在命令提示符下键入 cscript filename.vbs。
如果无法访问事件日志,请进行检查以查看是否正从提升的命令提示符运行。 某些事件日志(例如安全事件日志)可能受用户访问控制 (UAC) 的保护。
注意
默认情况下,cscript 会在命令提示符窗口中显示脚本的输出。 由于 WMI 脚本可以生成大量输出,因此可能需要将输出重定向到文件。 在命令提示符下键入 cscript filename.vbs > outfile.txt 以将 filename.vbs 脚本的输出重定向到 outfile.txt。
下表列出了可用于从本地计算机获取各种类型的数据的脚本示例。
...确定远程计算机是否已在具有网络状态的安全模式下启动?
使用 Win32_ComputerSystem 类并检查 PrimaryOwnerName 属性的值。
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings
Wscript.Echo "System Name: " & objComputer.Name
Wscript.Echo "Registered owner: " & objComputer.PrimaryOwnerName
Next
$colSettings = Get-WmiObject -Class Win32_ComputerSystem
foreach ($objComputer in $colSettings)
{
"System Name: " + $objComputer.Name
"Registered owner: " + $objComputer.PrimaryOwnerName
}
...确定计算机屏幕保护程序是否需要密码?
使用 Win32_Desktop 类并检查 ScreenSaverSecure 属性的值。
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Desktop")
For Each objItem in colItems
Wscript.Echo "Screen Saver Secure: " & objItem.ScreenSaverSecure
Next
$Computer = "."
$Desktops = Get-WMIObject -class Win32_Desktop -ComputerName $computer
"{0} desktops found as follows" -f $desktops.count
foreach ($desktop in $desktops) {
"Desktop : {0}" -f $Desktop.Name
"Screen Saver : {0}" -f $desktop.ScreensaverExecutable
"Secure : {0} " -f $desktop.ScreenSaverSecure
""
}
...验证是否已将计算机屏幕设置为 800 x 600 像素?
使用 Win32_DesktopMonitor 类并检查 ScreenHeight 和 ScreenWidth 属性的值。
strComputer = "."
Set objWMIService = GetObject(_
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_DesktopMonitor")
For Each objItem in colItems
Wscript.Echo "Screen Height: " & objItem.ScreenHeight
Wscript.Echo "Screen Width: " & objItem.ScreenWidth
Next
<# Get desktop information #>
$computer = "." $desktops = Get-WmiObject -Class Win32_DesktopMonitor $hostname = hostname
<# Display desktop details #> "There are {0} Desktops on {1} as follows:" -f $desktops.Count, $hostname "" $i=1 # count of desktops on this system
foreach ($desktop in $desktops) {
"Desktop {0}: {1}" -f $i++, $Desktop.Caption
"Screen Height : {0}" -f $desktop.ScreenHeight
"Screen Width : {0}" -f $desktop.ScreenWidth
""
}
...确定计算机运行了多长时间?
使用 Win32_OperatingSystem 类和 LastBootUpTime 属性。 从当前时间减去该值可得出系统运行时间。
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootUpTime = WMIDateStringToDate(dtmBootup)
dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
Wscript.Echo dtmSystemUptime
Next
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) & " " & Mid (dtmBootup, 9, 2) & ":" & _
Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, 13, 2))
End Function
function WMIDateStringToDate($Bootup) { [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup) }
<# Main script #> $Computer = "." # adjust as needed $computers = Get-WMIObject -class Win32_OperatingSystem -computer $computer
foreach ($system in $computers) {
$Bootup = $system.LastBootUpTime
$LastBootUpTime = WMIDateStringToDate($Bootup)
$now = Get-Date
$Uptime = $now-$lastBootUpTime
"System Up for: {0}" -f $UpTime
}
...重新启动或关闭远程计算机?
使用 Win32_OperatingSystem 类和 Win32Shutdown 方法。 连接到 WMI 时,必须包含 RemoteShutdown 特权。 有关详细信息,请参阅使用 VBScript 执行特权操作 。 与 Win32_OperatingSystem 上的 Shutdown 方法不同,Win32Shutdown 方法允许设置标志来控制关闭行为。
strComputer = "atl-dc-01"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Shutdown(1)
Next
strComputer = "atl-dc-01"
$colOperatingSystem = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $strComputer -Namespace "wmi\cimv2"
foreach ($objOperatingSystem in $colOperatingSystem)
{
[void]$objOperatingSystem.Shutdown()
}
...确定每次启动 Windows 时自动运行哪些应用程序?
使用 Win32_StartupCommand 类。
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colStartupCommands = objWMIService.ExecQuery _
("Select * from Win32_StartupCommand")
For Each objStartupCommand in colStartupCommands
Wscript.Echo "Command: " & objStartupCommand.Command & VBNewLine _
& "Description: " & objStartupCommand.Description & VBNewLine _
& "Location: " & objStartupCommand.Location & VBNewLine _
& "Name: " & objStartupCommand.Name & VBNewLine _
& "SettingID: " & objStartupCommand.SettingID & VBNewLine _
& "User: " & objStartupCommand.User
Next
$strComputer = "."
$colItems = Get-WmiObject -Class Win32_StartupCommand -ComputerName $strComputer
foreach ($objStartupCommand in $colItems)
{
"Command: " + $objStartupCommand.Command
"Description: " + $objStartupCommand.Description
"Location: " + $objStartupCommand.Location
"Name: " + $objStartupCommand.Name
"SettingID: " + $objStartupCommand.SettingID
"User: " + $objStartupCommand.User
}
脚本和应用程序的 WMI 任务
WMI C++ 应用程序示例
TechNet ScriptCenter