VBScript to enable / disable proxy - windows

I'm trying to create a script to go into startup applications which will automatically set the proxy in my workplace. The idea is this however I'm new to VBScript and I keep getting an error.
Option Explicit
Dim WSHShell, strSetting
ans = msgbox("Are you working in the office?" , vbyesno)
If ans = vbyes then
Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer", "10.2.2.88:8090", "REG_SZ"
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyOverride", "*.dev;*.test;*.local;<local>", "REG_SZ"
else
Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
End If
Can somebody show me where I'm going wrong?
Thanks!

Suppose your error is something as follows:
==>cscript //nologo D:\VB_scripts\SO\28832310.vbs
28832310.vbs(3, 1) Microsoft VBScript runtime error: Variable is undefined: 'ans'
Self-explanatory message, IMHO. As you have used Option Explicit statement correctly (forces explicit declaration of all variables in a script), simply add ans variable declaration, i.e. use
Dim WSHShell, strSetting, ans

Related

Add file to HKEY_CURRENT_USER Autorun AND HKEY_LOCAL_MACHINE in only ONE script

I have this VBScript:
On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")
myKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\example"
WshShell.RegWrite myKey, "C:\Program Files (x86)\example.exe", "REG_SZ"
Set WshShell = Nothing
which adds
"C:\Program Files (x86)\example.exe"
to the startup registry
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\example"
Now I also want to add
"C:\Program Files (x86)\example.exe"
to the startup registry
"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run\example"
How can you do that in only ONE SINGLE VBScript?
I tried to just modify this code:
On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")
myKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\example"
WshShell.RegWrite myKey, "C:\Program Files (x86)\example.exe", "REG_SZ"
Set WshShell = Nothing
so that it would add the program to HKEY_LOCAL_MACHINE and then paste it underneath, but that didn't work.
Dim wshShell
Dim myKey
Set wshShell = CreateObject("WScript.Shell")
myKey = "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run\example"
wshShell.RegWrite myKey, "C:\Program Files (x86)\example.exe", "REG_SZ"

VBS Windows proxy enable

Option Explicit
Dim objShell, RegLocate
Set objShell = WScript.CreateObject("WScript.Shell")
RegLocate = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable"
objShell.RegWrite RegLocate,"0","REG_DWORD"
WScript.Sleep 1000
If "REG_DWORD" = "" then
CreateObject("WScript.Shell").RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet\Settings\ProxyEnable Settings\ProxyEnable",1,"REG_DWORD"
else
MsgBox "Proxy alrdy running"
end if
WScript.Quit
So my problem is not to get the proxy settings in, I have script for that and it works. The problem is to enable it in IE explorer.
The script runs and don't get an error... so I have hit a wall.
Sorry for bad grammar and English.

How to integrate icon modification script with script to toggle proxy?

I refer to this post. Just as in the original post I have a proxy on/off script.
Where I need help is with the inclusion of the "icon change script" (see referral post) into my already existing script, i.e. where would
Set sh = CreateObject("WScript.Shell")
lnkfile = sh.SpecialFolders("Desktop") & "\your.lnk"
Set lnk = sh.CreateShortcut(lnkfile)
If lnk.IconLocation = "C:\path\to\some.ico" Then
sh.IconLocation = "C:\path\to\.ico"
Else
sh.IconLocation = "C:\path\to\some.ico"
End If
lnk.Save
fit into
Option Explicit
Dim WSHShell, strSetting
Set WSHShell = CreateObject("WScript.Shell")
'Determine current proxy setting and toggle to oppisite setting
strSetting = WSHShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
If strSetting = 1 Then
NoProxy
Else
Proxy
End If
'Subroutine to Toggle Proxy Setting to ON
Sub Proxy
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
WScript.Echo "Proxy is On"
End Sub
'Subroutine to Toggle Proxy Setting to OFF
Sub NoProxy
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
WScript.Echo "Proxy is Off"
End Sub
My interpretation of your answer Ansgar
Option Explicit
Dim WSHShell, strSetting
Set WSHShell = WScript.CreateObject("WScript.Shell")
'Determine current proxy setting and toggle to oppisite setting
strSetting = WSHShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
lnkfile = WSHShell.SpecialFolders("Desktop") & "\proxypal.lnk"
Set lnk = WSHShell.CreateShortcut(lnkfile)
If strSetting = 1 Then
WSHShell.IconLocation = "C:\path\to\on.ico"
NoProxy
Else
WSHShell.IconLocation = "C:\path\to\off.ico"
Proxy
End If
lnk.Save
'Subroutine to Toggle Proxy Setting to ON
Sub Proxy
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
Wscript.echo "Proxy is On"
End Sub
'Subroutine to Toggle Proxy Setting to OFF
Sub NoProxy
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
Wscript.echo "Proxy is Off"
End Sub
However this returns this error
line 9
Variable is undefined: linkfile
800A01F4
MS VBScript Runtime Error
That's not too complicated. Just change the icon where you call the functions that modify the proxy settings:
...
strSetting = WSHShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
lnkfile = WSHShell.SpecialFolders("Desktop") & "\your.lnk"
Set lnk = WSHShell.CreateShortcut(lnkfile)
If strSetting = 1 Then
WSHShell.IconLocation = "C:\path\to\enable.ico"
NoProxy
Else
WSHShell.IconLocation = "C:\path\to\disable.ico"
Proxy
End If
lnk.Save
...

objshell.regwrite wont work

It works with:
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoFolderOptions", 1, "REG_DWORD"
Set objShell = Nothing
But it doesn't work with:
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoDrives", 3FFFFFF, "REG_DWORD"
Set objShell = Nothing
For the second one it will have an end of statement error. Why is that?
Your answer is here (read the whole page, there is also vbs example code).
Note that only decimal values can be added as strings (as described in the article).
You get the error because 3FFFFF is neither a valid variable name nor valid notation for a hexadecimal number. Try this:
objShell.RegWrite "HKCU\...\NoDrives", &h3FFFFFF, "REG_DWORD"

Change Wallpaper in Windows 7

I have written a small vbs script to download and change the registry for the current users wallpaper. however it copies and does the change but the wallpaper does not change... any ideas on errors in the below code?
Option Explicit
Dim WshShell, strValue, sleepTime, oFSO
strValue = "C:\wallpaper.bmp"
sleepTime = 30000
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CopyFile "\\anspksnms1\OSD\Scripts\wallpaper\wallpaper.bmp", "C:\"
Set WshShell = WScript.CreateObject("Wscript.Shell")
WshShell.RegWrite "HKCU\Control Panel\Desktop\Wallpaper", strValue
WScript.Sleep sleepTime
WshShell.Run "%windir%\System32\RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters", 1, False
Set WshShell = Nothing
I'm not much of a vbscripter, but maybe try this?
wshShell.run "cmd /c RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters", 1, True

Resources