WMI タスク:デスクトップ管理

デスクトップ管理用の WMI タスクは、リモート デスクトップまたはローカル コンピューターから制御を行い、データを取得することができます。 たとえば、ローカル コンピューターのスクリーンセーバーにパスワードが必要かどうかを判断できます。 また、WMI を使用すると、リモート コンピューターをシャットダウンすることもできます。 その他の例については、https://www.microsoft.com/technet の TechNet ScriptCenter を参照してください。

このトピックに示したスクリプト例では、ローカル コンピューターからのみデータを取得します。 スクリプトを使用してリモート コンピューターからデータを取得する方法の詳細については、「リモート コンピューター上の 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 ピクセル x 600 ピクセルに設定されていることを確認しますか?

Win32_DesktopMonitor クラスを使用し、ScreenHeight プロパティと ScreenWidth プロパティの値を調べます。

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 = hostname 

<# デスクトップの詳細を表示する #> "{0} デスクトップが {1} に、次のようにあります:" -f $desktops.Count, $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) } 

<# 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_OperatingSystemShutdown メソッドとは違い、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