vbscript issue for removing trusted site - vbscript

I have a script to add the trusted sites to IE.
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\" _
& "ZoneMap\Domains\" & "https://www.google.com"
objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
strValueName = "*"
dwValue = 2
objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
The trusted sites are added successfully. But there is a problem here....
I am not able to remove the trusted sites added through the script which is a serious problem
Thanks in advance.

'**************************************************************************
'VBScript to remove all IE opened tab urls from the Trusted Site list
'***************************************************************************
Dim Windows
Dim tabUrl
On Error Resume Next
Const HKEY_CURRENT_USER = &H80000001
Set Shell = CreateObject("Shell.Application")
Set Windows = CreateObject(Shell.Windows)
For Each Window In Shell.Windows
If InStr(1, Window.FullName, "iexplore.exe", vbTextCompare) > 0 Then
tabUrl = Window.LocationUrl
Msgbox tabUrl
strComputer = "."
Set objReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\" & "ZoneMap\Domains\" &tabUrl
objReg.DeleteKey HKEY_CURRENT_USER, strKeyPath
strValueName = "*"
dwValue = 2
objReg.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
End If
Next

The Scripting Guys article tells you how to delete keys and tells you why delete sometimes doesn't work

Related

Failing to delete registry key

I'm working on a writing a VBScript and I have no experience with VBS. I can do this with a command line with no problem, however, it needs to be in VBS.
The script I'm working on checks to see if 2 registry keys exist, and if so, deletes a third key. I've been looking around on the net and found some code that works for what I want to do. My issue is on the delete RegKey code. I found this code which works fine, however, if I modify it to point to a different location, it doesn't work.
Here is the code I found and if I put a key in the registry, it deletes it just fine.
Created this key: HKCU\Software\Test
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
strKeyPath = "Software\Test"
Set objRegistry = GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
DeleteSubkeys HKEY_CURRENT_USER, strKeypath
Sub DeleteSubkeys(HKEY_CURRENT_USER, strKeyPath)
objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
DeleteSubkeys HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey
Next
End If
objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath
End Sub
But if I change the code to this, it doesn't work. What am I doing wrong?
Created this key: HKLM\Software\Test
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strKeyPath = "Software\Test"
Set objRegistry = GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeypath
Sub DeleteSubkeys(HKEY_LOCAL_MACHINE, strKeyPath)
objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeyPath & "\" & strSubkey
Next
End If
objRegistry.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
End Sub

Add trusted sites to windows registry for all users

I want to add trusted web sites to the windows registry for all users using a VBScript. I currently have a script with me and its given below. I'm neither a Windows guy nor a Visual Basic guy, so I have absolutely no idea whether the script would run or not and would it meet my needs or not. Could someone please explain the script and check whether would it run as expected.
On Error Resume Next
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objReg = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}\\" & strComputer & _
"\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\" _
& "ZoneMap\EscDomains\google.com"
objReg.CreateKey HKEY_CURRENT_USER,strKeyPath
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\" _
& "ZoneMap\EscDomains\google.com\www"
objReg.CreateKey HKEY_CURRENT_USER,strKeyPath
strValueName = "https"
dwValue = 2
objReg.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\" _
& "ZoneMap\Domains\google.com"
objReg.CreateKey HKEY_CURRENT_USER,strKeyPath
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\" _
& "ZoneMap\Domains\google.com\www"
objReg.CreateKey HKEY_CURRENT_USER,strKeyPath
strValueName = "https"
dwValue = 2
objReg.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\" _
& "ZoneMap\EscDomains\hotmail.com"
objReg.CreateKey HKEY_CURRENT_USER,strKeyPath
strValueName = "https"
dwValue = 1
objReg.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\" _
& "ZoneMap\Domains\hotmail.com"
objReg.CreateKey HKEY_CURRENT_USER,strKeyPath
strValueName = "https"
dwValue = 1
objReg.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue
If you feel this is an inappropiate script, please share the VBScript which you feel that would work.
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
Your script is adding to current user, not all users as you said you need. Will multiple real users be logging into that machine?
The keys you need are:
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\<website>.com]
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\<website>.com\www]
"http"=dword:00000002
You could create a .reg file with above information for the websites and run it (put "Windows Registry Editor Version 5.00" at the top of that .reg file)
As has already been indicated, some quality time on Google will definitely enhance your knowledge of what the script you posted is trying to do and how to modify it. Don't hesitate to ask any specific (as opposed very general) questions.

Can't use VB Script to create registry keys

I can't use VB Script to create registry keys. I've hecked WMI using wbemtest and I am running the script using administrative privileges. I beleive the code is correct - I've seen several sample on the internet and it seems to be straight forward. Is there anything else within the OS that could prevent a VB Script from creating registry keys?
Sample code is below.
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "ostslhqe-48958"
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\TestKey"
objRegistry.CreateKey HKEY_CURRENT_USER, strKeyPath
strKeyPath = "SOFTWARE\Script Center"
objRegistry.CreateKey HKEY_CURRENT_USER, strKeyPath
An example of registry entry creation would be:
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strValueName = "My DWORD Value"
dwValue = 13
objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
Source : http://blogs.technet.com/b/heyscriptingguy/archive/2006/11/16/how-can-i-create-a-new-registry-key.aspx

Script to delete user accounts from Profile list on Windows 8.1

The following code is meant to delete all user accounts from HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
Code:
On Error Resume Next
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objRegistry=GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
For Each objSubkey In arrSubkeys
strValueName = "ProfileImagePath"
strSubPath = strKeyPath & "\" & objSubkey
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strSubPath,strValueName,strValue
strPath=Left(strvalue,Len(strvalue)-Len(Right(strvalue,Len(strvalue)-9)))
strID=Right(strvalue,Len(strvalue)-9)
if ((strPath="C:\Users\") and (strID<>"rcadmin")) then
return=objRegistry.DeleteKey(HKEY_LOCAL_MACHINE, strSubPath)
end if
Next
wscript.echo "Done"
If I replace "return=objRegistry.DeleteKey(HKEY_LOCAL_MACHINE, strSubPath)" with "wscript.echo strID", it displays what I expect. So all I need to do now is delete the string "strSubPath" from "HKEY_LOCAL_MACHINE". And this is the part that I can not get right.
An example of the output of
wscript.echo strSubPath" is "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"\S-1-5-21-3469983464-63224933-1422604425-8029
What am I missing?

How to remove a registry entry from Windows 2008/Vista

I have this script to run on Windows 2008/Vista to remove one registry key, but I can't get it to run:
Const HKEY_CLASSES_ROOT = &H80000000
strComputer = "."
strKeyPath = "Installer\Products\334A4D1453680B74CA87BEE6B7E40113"
Set objRegistry = GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
DeleteSubkeys HKEY_CLASSES_ROOT, strKeypath
Private Sub DeleteSubkeys(HKEY_CURRENT_USER, strKeyPath)
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
DeleteSubkeys HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey
Next
End If
objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath
End Sub
Any idea why?
Are you running this as an admin user? Despite your use of HKEY_CURRENT_USER as a param name, you're trying to delete from HKEY_CLASSES_ROOT, which would normally require elevated access.

Resources