共用方式為


WMI 工作:桌面管理

桌面管理的 WMI 工作可以進行控制,並從遠端桌面或本機電腦取得資料。 例如,您可以判斷本機電腦上的螢幕保護裝置程式是否需要密碼。 WMI 也可讓您關閉遠端電腦。 如需其他範例,請參閱 位於 的 TechNet ScriptCenter https://www.microsoft.com/technet

本主題所示的腳本範例只會從本機電腦取得資料。 如需如何使用腳本從遠端電腦取得資料的詳細資訊,請參閱 連線到遠端電腦上的 WMI

下列程式描述如何執行腳本。

執行指令碼

  1. 複製程式碼,並將它儲存在副檔名為 .vbs 的檔案中,例如 filename.vbs。 請確定文字編輯器不會將.txt副檔名新增至檔案。
  2. 開啟命令提示字元視窗,並流覽至您儲存檔案的目錄。
  3. 在命令提示字元中輸入 cscript filename.vbs
  4. 如果您無法存取事件記錄檔,請檢查您是否從提升許可權的命令提示字元執行。 某些事件記錄檔,例如安全性事件記錄檔,可能會受到使用者存取控制 (UAC) 保護。

注意

根據預設,cscript 會在命令提示字元視窗中顯示腳本的輸出。 因為 WMI 腳本可以產生大量的輸出,所以您可能會想要將輸出重新導向至檔案。 在命令提示字元中輸入 cscript filename.vbs > outfile.txt ,將 filename.vbs 腳本的輸出重新導向至 outfile.txt

下表列出可用來從本機電腦取得各種資料類型的腳本範例。

如何… WMI 類別或方法
...判斷遠端電腦是否已以安全模式開機並處於網路狀態? 使用 Win32_ComputerSystem 類別,並檢查 PrimaryOwnerName 屬性的值。
VB
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
PowerShell
$colSettings = Get-WmiObject -Class Win32_ComputerSystem
foreach ($objComputer in $colSettings)
{
    "System Name: " + $objComputer.Name
    "Registered owner: " + $objComputer.PrimaryOwnerName
}
...判斷電腦畫面是否需要密碼?

使用 Win32_Desktop 類別,並檢查 ScreenSaverSecure 屬性的值。

VB
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
PowerShell
$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 類別,並檢查 ScreenHeightScreenWidth屬性的值。

VB
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

PowerShell
              
              <# 取得桌面資訊 #>
$computer = 「.」 $desktops = Get-WmiObject -Class Win32_DesktopMonitor $hostname = 主機名稱

<# 顯示桌面詳細資料 # > 「有 {0} 桌面, {1} 如下所示:」 -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 屬性。 從目前時間減去該值,以取得系統執行時間。

VB
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

PowerShell
              
              函式 WMIDateStringToDate ($Bootup) { [System.Management.ManagementDateTimeconverter]::ToDateTime ($Bootup) }

<# 主要腳本 # > $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方法可讓您設定旗標來控制關機行為。

VB
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
PowerShell
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 類別。

VB
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
PowerShell
$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