Usuário com melhor resposta
Script VBS Verificar se existe uma chave no registro

Pergunta
-
Prezados,
Estou com dificuldades para criar um scritp VBS (para rodar no netlogon com psexec).
A finalidade deste script é, descobrir se uma determinada chave existe, caso não exista, cria a chave. Caso a chave já exista, apenas altere o valor.
Para verificar se a chave existe, estou utilizando este código:
Set Shell = WScript.CreateObject("WScript.Shell")
Chave = Shell.RegRead("HKEY_LOCAL_MACHINE\System\CurrentControlSet\services\TCPIP6\DisabledComponents")
if IsNull (Chave) = True Then
Wscript.echo "Chave não existe"
'.... executa comando
Else
Wscript.echo "Chave existe"
'.... executa comando
End If
Set Shell = NothingOBSERVAÇÃO: Este script gera um erro porque a chave não existe (linha 2).
--------------------------------------------------------------------------------------------------------------------------------------------
Tentei 2 scripts para tentar criar a chave, ambos não funcionaram:
Script 1:
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Services\TCPIP6"
strValueName = "DisabledComponents"
dwValue = 0
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue
Script 2:
valor = "0"
Set objFSO = CreateObject("Scripting.FileSystemObject")
set oShell = Wscript.CreateObject("Wscript.Shell")
oShell.RegWrite "HKLM\System\CurrentControlSet\Services\TCPIP6\DisabledComponents", valor, "REG_DWORD"OBSERVAÇÕES:
No script 1, ao ser executado nada acontece.
No script 2, surge erro na linha 4: Raiz Inválida na chave do registro.
Alguém poderia me ajudar ?
Sou Livre !
Respostas
-
Ricardodru
Não sei se compreendi bem o que vc deseja, mas execute o código abaixo e veja se lhe atende.
On Error Resume Next Const HKEY_LOCAL_MACHINE = &H80000002 strComputer = "." Set Shell = WScript.CreateObject("WScript.Shell") Chave = Shell.RegRead("HKEY_LOCAL_MACHINE\System\CurrentControlSet\services\TCPIP6\DisabledComponents") If Chave = "" Then WScript.echo "Chave não existe" valor = "0" Set oShell = Wscript.CreateObject("Wscript.Shell") oShell.RegWrite "HKLM\System\CurrentControlSet\Services\TCPIP6\DisabledComponents", valor, "REG_DWORD" Else WScript.echo "Chave existe" valor = "0" Set oShell = Wscript.CreateObject("Wscript.Shell") oShell.RegWrite "HKLM\System\CurrentControlSet\Services\TCPIP6\DisabledComponents", valor, "REG_DWORD" End If WScript.Quit
Your potential. Our passion - Microsoft
- Marcado como Resposta ricardoclaus terça-feira, 19 de janeiro de 2016 12:17
Todas as Respostas
-
Ricardodru
Não sei se compreendi bem o que vc deseja, mas execute o código abaixo e veja se lhe atende.
On Error Resume Next Const HKEY_LOCAL_MACHINE = &H80000002 strComputer = "." Set Shell = WScript.CreateObject("WScript.Shell") Chave = Shell.RegRead("HKEY_LOCAL_MACHINE\System\CurrentControlSet\services\TCPIP6\DisabledComponents") If Chave = "" Then WScript.echo "Chave não existe" valor = "0" Set oShell = Wscript.CreateObject("Wscript.Shell") oShell.RegWrite "HKLM\System\CurrentControlSet\Services\TCPIP6\DisabledComponents", valor, "REG_DWORD" Else WScript.echo "Chave existe" valor = "0" Set oShell = Wscript.CreateObject("Wscript.Shell") oShell.RegWrite "HKLM\System\CurrentControlSet\Services\TCPIP6\DisabledComponents", valor, "REG_DWORD" End If WScript.Quit
Your potential. Our passion - Microsoft
- Marcado como Resposta ricardoclaus terça-feira, 19 de janeiro de 2016 12:17
-
Prezado Leandro,
Agradeço a ajuda inicial !
A ideia do script está correta. A função dele é verificar se a chave existe. Caso exista, apenas altere o valor.
Caso não exista, cria a chave e altere o valor.
Ao executar o script no windows 8.1, surgiu este erro na linha 6:
Chave = Shell.RegRead("HKEY_LOCAL_MACHINE\System\CurrentControlSet\services\TCPIP6\DisabledComponents")
Erro apresentado: Não é possível abrir a chave do Registro para leitura.
A função deste script é desativar o IPV6 em windows 8.1 e 10.
No windows 7 funciona perfeitamente, pois já existe a chave.
Sou Livre !
-