Attività WMI: gestione desktop

Le attività WMI per la gestione desktop possono esercitare il controllo e ottenere dati da un desktop remoto o da un computer locale. Ad esempio, è possibile determinare se lo screenaver in un computer locale richiede una password. WMI offre anche la possibilità di arrestare un computer remoto. Per altri esempi, vedere TechNet ScriptCenter all'indirizzo https://www.microsoft.com/technet.

Gli esempi di script illustrati in questo argomento ottengono dati solo dal computer locale. Per altre informazioni su come usare lo script per ottenere dati da computer remoti, vedere Connessione a WMI in un computer remoto.

La procedura seguente descrive come eseguire uno script.

Per eseguire uno script

  1. Copiare il codice e salvarlo in un file con estensione vbs, ad esempio filename.vbs. Assicurarsi che l'editor di testo non aggiunga un'estensione .txt al file.
  2. Aprire una finestra del prompt dei comandi e passare alla directory in cui è stato salvato il file.
  3. Digitare cscript filename.vbs al prompt dei comandi.
  4. Se non è possibile accedere a un registro eventi, verificare se si esegue da un prompt dei comandi con privilegi elevati. Alcuni registri eventi, ad esempio il registro eventi di sicurezza, possono essere protetti da controlli di accesso utente.

Nota

Per impostazione predefinita, cscript visualizza l'output di uno script nella finestra del prompt dei comandi. Poiché gli script WMI possono produrre grandi quantità di output, è possibile reindirizzare l'output a un file. Digitare cscript filename.vbs > outfile.txt al prompt dei comandi per reindirizzare l'output dello script filename.vbs a outfile.txt.

Nella tabella seguente sono elencati gli esempi di script che possono essere usati per ottenere vari tipi di dati dal computer locale.

Ricerca per categorie Classi o metodi WMI
... determinare se un computer remoto è stato avviato in modalità provvisoria con stato Rete? Utilizzare la classe Win32_ComputerSystem e controllare il valore della proprietà 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
}
... determinare se uno screenaver del computer richiede una password?

Usare la classe Win32_Desktop e controllare il valore della proprietà 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
     ""
}
... verificare che una schermata del computer sia stata impostata per 800 pixel di 600 pixel?

Usare la classe Win32_DesktopMonitor e controllare i valori delle proprietà ScreenHeight e 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
<# Ottenere informazioni sul desktop #>
$computer = "." $desktops = Get-WmiObject -Class Win32_DesktopMonitor $hostname = nome host

<# Visualizzare i dettagli del desktop #> "Ci sono {0} desktop in {1} come indicato di seguito:" -f $desktops. Conteggio, $hostname "" $i=1 numero di desktop in questo sistema

foreach ($desktop in $desktops) { "Desktop {0}: {1}" -f $i++, $Desktop.Caption "Screen Height : {0}" -f $desktop.ScreenHeight "Screen Width : {0}" -f $desktop.ScreenWidth "" }

... determinare per quanto tempo è in esecuzione un computer?

Utilizzare la classe Win32_OperatingSystem e la proprietà LastBootUpTime . Sottrarre tale valore dall'ora corrente per ottenere il tempo di attività del sistema.

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

<# Script principale #> $Computer = "." # regola in base alle esigenze $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 }

... riavviare o arrestare un computer remoto?

Usare la classe Win32_OperatingSystem e il metodo Win32Shutdown . È necessario includere il privilegio RemoteShutdown durante la connessione a WMI. Per altre informazioni, vedere Esecuzione di operazioni con privilegi tramite VBScript. A differenza del metodo Shutdown in Win32_OperatingSystem, il metodo Win32Shutdown consente di impostare i flag per controllare il comportamento di arresto.

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()
}
... determinare quali applicazioni vengono eseguite automaticamente ogni volta che si avvia Windows?

Usare 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
}

Attività WMI per script e applicazioni

Esempi di applicazioni WMI C++

TechNet ScriptCenter