WMI 工作:桌面管理
本文內容
桌面管理的 WMI 工作可以發揮控制,並從遠端桌面或本機電腦取得數據。 例如,您可以判斷本機電腦上的螢幕工具是否需要密碼。 WMI 也可讓您關閉遠端電腦。 如需其他範例,請參閱techNet ScriptCenter at https://www.microsoft.com/technet 。
本主題中顯示的腳本範例只會從本機計算機取得數據。 如需如何使用文稿從遠端電腦取得資料的詳細資訊,請參閱 遠端電腦上連線到 WMI 。
下列程式描述如何執行腳本。
執行腳本
複製程序代碼,並將它儲存在擴展名為 .vbs 的檔案中,例如 filename.vbs 。 請確定文字編輯器不會將 .txt 擴展名新增至檔案。
開啟命令提示字元視窗,並流覽至您儲存盤案的目錄。
在命令提示字元中輸入 cscript filename.vbs 。
如果您無法存取事件記錄檔,請檢查您是否正在從提高許可權的命令提示字元執行。 某些事件記錄檔,例如安全性事件記錄檔,可能會受到使用者訪問控制 (UAC) 的保護。
注意
根據預設,cscript 會在命令提示字元視窗中顯示文稿的輸出。 由於 WMI 命令稿可能會產生大量的輸出,因此您可能會想要將輸出重新導向至檔案。 在命令提示字元中輸入 cscript filename.vbs > outfile.txt ,將 filename.v bs 的輸出重新導向至 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像素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 = 主機名
<#显示桌面详细数据 #>「{1} 上有 {0} 桌面,如下所示:」-f $desktops。計數,$hostname 「$i=1 # 此系統上桌面計數
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
函式 WMIDateStringToDate($Bootup) { [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup) }
<# Main script #> $Computer = “.” #视需要调整$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