Attività WMI: Registro di sistema

Attività WMI per la creazione e la modifica delle chiavi e dei valori del Registro di sistema. Per altri esempi, vedere TechNet ScriptCenter in 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 dai 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 aggiunge 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 log eventi, ad esempio il registro eventi di sicurezza, possono essere protetti da controlli di accesso utente (UAC).

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.txtal prompt dei comandi per reindirizzare l'output dello scriptfilename.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
... leggere i valori delle chiavi del Registro di sistema usando WMI? Utilizzare la classe StdRegProv , disponibile nello spazio dei nomi root\default. Non è possibile ottenere istanze di questa classe perché il provider del Registro di sistema è un metodo e un provider di eventi solo. È tuttavia possibile ottenere dati del Registro di sistema tramite metodi come EnumKey o EnumValue. Il Win32_Registry, situato nello spazio dei nomi root\cimv2, ottiene i dati relativi al Registro di sistema nel suo complesso, ad esempio quanto è grande.
VB
const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Console"
strValueName = "HistoryBufferSize"
oReg.GetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue
WScript.Echo "Current History Buffer Size: " & dwValue
PowerShell
$HKEY_CURRENT_USER =2147483649
$computer ='.'
$reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
$Key = "Console"
$Value = "HistoryBufferSize"
$results = $reg.GetDWORDValue($HKEY_CURRENT_USER, $Key, $value)
"Current History Buffer Size: {0}" -f $results.uValue
... creare una nuova chiave del Registro di sistema?

Utilizzare la classe StdRegProv , disponibile nello spazio dei nomi root\default e nel metodo CreateKey .

VB
const HKEY_LOCAL_MACHINE = &H80000002 strComputer = "." Set objReg=GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\NewKey" objReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath WScript.Echo "Created registry key HKEY_LOCAL_MACHINE\SOFTWARE\NewKey"

PowerShell
$HKEY_Local_Machine =2147483650 
$computer ='.'
$reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
$Key     = "SOFTWARE\NewKey"
$results   = $reg.CreateKey($HKEY_LOCAL_MACHINE, $Key)
If ($results.Returnvalue -eq 0) {"Key created"} 
... creare un nuovo valore del Registro di sistema in una chiave?

Utilizzare la classe StdRegProv , disponibile nello spazio dei nomi root\default e nel metodo CreateKey . Usare quindi uno dei metodi Set, a seconda del tipo di dati del Registro di sistema, ad esempio SetDWORDValue. I metodi Set creano un valore se non esiste già. Per altre informazioni, vedere Mapping di un tipo di dati del Registro di sistema a un tipo di dati WMI.

VB
Const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARE\NewKey"
strComputer = "."
Set objReg=GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strValueName = "Example_Expanded_String_Value"
strValue = "%PATHEXT%"
objReg.SetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
WScript.Echo "Example expanded_String_Value at " & "HKEY_LOCAL_MACHINE\SOFTWARE\NewKey"
PowerShell
$HKEY_Local_Machine =2147483650 
$computer ='.'
$reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
$ValueName = "Example_Expanded_String_Value"
$Value     = "%PATHEXT%"
$Key       = "SOFTWARE\NewKey"
$results   = $reg.SetExpandedStringValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Value)
If ($results.Returnvalue -eq 0) {"Value created"}
... evitare di ottenere un errore di classe non valido quando si tenta di scrivere uno script per leggere il Registro di sistema?

Usare lo spazio dei nomi radice\predefinito quando si accede alla classe StdRegProv . StdRegProv non fa parte dello spazio dei nomi cimv2, motivo per cui viene generato un errore "Classe non valida" se si tenta di connettersi a "root\cimv2:StdRegProv".

VB
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set oReg=GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") 
strKeyPath = "Console"
strValueName = "HistoryBufferSize"
oReg.GetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
Wscript.Echo "Current History Buffer Size: " & dwValue
... controllare la sicurezza in una chiave del Registro di sistema specifica?

Utilizzare la classe StdRegProv , disponibile nello spazio dei nomi root\default e nel metodo CheckAccess . È possibile controllare solo i diritti di accesso per l'utente corrente che esegue lo script o l'applicazione. Non è possibile controllare i diritti di accesso per un altro utente specificato.

... leggere e scrivere valori del Registro di sistema binari?

Utilizzare la classe StdRegProv, disponibile nello spazio dei nomi "Root\Default" e nei metodi GetBinaryValue e SetBinaryValue. I valori del Registro di sistema visualizzati nell'utilità RegEdt32 come serie di valori esadecimali byte si trovano nel formato di dati REG_BINARY . Per altre informazioni, vedere Mapping di un tipo di dati del Registro di sistema a un tipo di dati WMI. Nell'esempio di codice VBScript seguente viene creata una nuova chiave con un valore binario. Il valore binario viene fornito nella matrice di byte iValues specificata in Hex.

VB
const HKEY_LOCAL_MACHINE = &H80000002 strKeyPath = "SOFTWARE\NewKey" strComputer = "." iValues = Array(&H01,&Ha2,&H10) Set oReg=GetObject("winmgmt")s:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath strKeyPath = "SOFTWARE\NewKey" BinaryValueName = "Example Binary Value"

oReg.SetBinaryValue HKEY_LOCAL_MACHINE,strKeyPath,BinaryValueName,iValues

Lo script seguente legge il valore binario.

VB
const HKEY_LOCAL_MACHINE = &H80000002 
strKeyPath = "SOFTWARE\NewKey"
strValueName = "Example Binary Value"
strComputer = "."
dim iValues(3)
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
oReg.GetBinaryValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,iValues
For i = lBound(iValues) to uBound(iValues)
Wscript.Echo iValues(i)
Next
PowerShell
$HKEY_Local_Machine =2147483650 
$computer ='.'
$reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
$ValueName = "Example Binary Value"
$Values     = @(0x54, 0x46, 0x4C)
$Key       = "SOFTWARE\NewKey"
$results   = $reg.GetBinaryValue($HKEY_LOCAL_MACHINE, $Key, $ValueName)
Foreach ($byte in $results.uvalue) {"{0}" -f $byte.tostring("x")}
... leggere e scrivere valori del Registro di sistema che contengono più stringhe?

Utilizzare la classe StdRegProv, disponibile nello spazio dei nomi radice\predefinito e nei metodi GetMultiStringValue e SetMultiStringValue. Le chiavi del Registro di sistema visualizzate nell'utilità RegEdt32 come serie di stringhe separate da spazi si trovano nel formato di dati REG_MULTI_SZ . Per altre informazioni, vedere Mapping di un tipo di dati del Registro di sistema a un tipo di dati WMI. L'esempio di codice VBScript seguente crea una nuova chiave e un nuovo valore multistringa.

VB
const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARE\NewKey"
MultValueName = "Example Multistring Value"
strComputer = "."
iValues = Array("string1", "string2")
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
oReg.SetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,MultValueName,iValues
PowerShell
$HKEY_Local_Machine =2147483650 
$computer ='.'
$reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
$Key       = "SOFTWARE\NewKey"
$ValueName = "Example MultiString Value"
$Values     = @("Thomas", "Susan", "Rebecca")
$Key       = "SOFTWARE\NewKey"
$results   = $reg.SetMultiStringValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Values)
If ($results.Returnvalue -eq 0) {"Value Set"} 

Lo script seguente legge il valore multistringa.

VB
const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARE\NewKey"
strComputer = "."
iValues = Array("string1", "string2")
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
MultValueName = "Example Multistring Value"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,MultValueName,iValues
For Each strValue In iValues
WScript.echo strValue
Next
PowerShell
# Define Constants
$HKEY_Local_Machine =2147483650 
$computer ='.'
$reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
$Key       = "SOFTWARE\NewKey"
$ValueName = "Example MultiString Value"
$results   = $reg.GetMultiStringValue($HKEY_LOCAL_MACHINE, $Key, $ValueName)
$results.svalue
... rimuovere una chiave del Registro di sistema?

Usare la classe StdRegProv , disponibile nello spazio dei nomi radice\predefinito e nei metodi DeleteKey .

PowerShell
$HKEY_Local_Machine =2147483650 
$computer ='.'
$reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
$Key     = "SOFTWARE\NewKey"
$results   = $reg.DeleteKey($HKEY_LOCAL_MACHINE, $Key)
If ($results.Returnvalue -eq 0) {"Key Removed"} 

Attività WMI per script e applicazioni

Esempi di applicazioni WMI C++

TechNet ScriptCenter

Modifica del Registro di sistema

StdRegProv