Partager via


Tâches WMI : Gestion des bureaux

Les tâches WMI pour la gestion de bureau peuvent exercer un contrôle et obtenir des données à partir d’un bureau à distance ou d’un ordinateur local. Par exemple, vous pouvez déterminer si le filtre d’écran sur un ordinateur local nécessite un mot de passe. WMI vous donne également la possibilité d’arrêter un ordinateur distant. Pour obtenir d’autres exemples, consultez TechNet ScriptCenter à https://www.microsoft.com/technet.

Les exemples de script présentés dans cette rubrique obtiennent des données uniquement à partir de l’ordinateur local. Pour plus d’informations sur l’utilisation du script pour obtenir des données à partir d’ordinateurs distants, consultez Connexion à WMI sur un ordinateur distant.

La procédure suivante décrit comment exécuter un script.

Pour exécuter un script

  1. Copiez le code et enregistrez-le dans un fichier avec une extension .vbs, telle que filename.vbs. Vérifiez que votre éditeur de texte n’ajoute pas d’extension .txt au fichier.
  2. Ouvrez une fenêtre d’invite de commandes et accédez au répertoire où vous avez enregistré le fichier.
  3. Tapez cscript filename.vbs à l’invite de commandes.
  4. Si vous ne pouvez pas accéder à un journal des événements, vérifiez si vous exécutez à partir d’une invite de commandes Avec élévation de privilèges. Certains journaux d’événements, tels que le journal des événements de sécurité, peuvent être protégés par les contrôles d’accès utilisateur (UAC).

Note

Par défaut, cscript affiche la sortie d’un script dans la fenêtre d’invite de commandes. Étant donné que les scripts WMI peuvent produire de grandes quantités de sortie, vous pouvez rediriger la sortie vers un fichier. Tapez cscript filename.vbs > outfile.txt à l’invite de commandes pour rediriger la sortie du script filename.vbs vers outfile.txt.

Le tableau suivant répertorie les exemples de script qui peuvent être utilisés pour obtenir différents types de données à partir de l’ordinateur local.

Comment faire... Classes ou méthodes WMI
... déterminer si un ordinateur distant a démarré en mode sans échec avec l’état réseau ? Utilisez la classe Win32_ComputerSystem et vérifiez la valeur de la propriété 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
}
... déterminer si un écran d’écran d’ordinateur nécessite un mot de passe ?

Utilisez la classe Win32_Desktop et vérifiez la valeur de la propriété 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
     ""
}
... vérifiez qu’un écran d’ordinateur a été défini pour 800 pixels de 600 pixels ?

Utilisez la classe Win32_DesktopMonitor et vérifiez les valeurs des propriétés ScreenHeight et 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
              
              <# Get desktop information #>
$computer = « . » $desktops = Get-WmiObject -Class Win32_DesktopMonitor $hostname = nom d’hôte 

<# Afficher les détails du bureau #> « Il existe {0} Ordinateurs de bureau sur {1} comme suit : » -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 "" }

... déterminer combien de temps un ordinateur est en cours d’exécution ?

Utilisez la classe Win32_OperatingSystem et la propriété LastBootUpTime. Soustrait cette valeur de l’heure actuelle pour obtenir le temps d’activité du système.

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
fonction WMIDateStringToDate($Bootup) { [System.Management.ManagementDateTimeconverter] ::ToDateTime($Bootup) } 

<# Script principal #> $Computer = « . » # ajuster si nécessaire $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 }

... redémarrer ou arrêter un ordinateur distant ?

Utilisez la classe Win32_OperatingSystem et la méthode Win32Shutdown. Vous devez inclure le privilège RemoteShutdown lors de la connexion à WMI. Pour plus d’informations, consultez exécution d’opérations privilégiées à l’aide de VBScript. Contrairement à la méthode Shutdown sur Win32_OperatingSystem, la méthode Win32Shutdown vous permet de définir des indicateurs pour contrôler le comportement d’arrêt.

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()
}
... déterminer quelles applications s’exécutent automatiquement chaque fois que je démarre Windows ?

Utilisez la classe 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
}

tâches WMI pour les scripts et les applications

exemples d’applications WMI C++

TechNet ScriptCenter