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

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
...

Related

Prompt user before toggling proxy

I need a little help with a VBScript I am using to disable and enable the proxy settings. What I would like to happen is the script tells the user what the current setting for the proxy is either off or on then if they want it to they can click would you like to change yes/no. Or I would like it to say the proxy is now off or the proxy is now on.
I know how to make a message box I just dont know where I should put the code.
This is my text box code:
result = Msgbox("Proxy is now set to off", vbOKonly+vbInformation, "")
And this is the proxy changing code:
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")
If strSetting = 1 Then
NoProxy
Else
End If
'Subroutine to Toggle Proxy Setting to ON
Sub Proxy
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
End Sub
'Subroutine to Toggle Proxy Setting to OFF
Sub NoProxy
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
End Sub
I've reorganised your code a bit but this should allow you to choose if you wish to toggle the Proxy on or off.
Some of the changes include;
Moving reused strings that will not change into a Constant.
Giving the script a single point of execution by wrapping the main logic in a Sub Procedure called Main(). We then make sure that the call to that procedure is the only code we run in the global scope except for the WScript.Shell declaration.
Converting the return value of CurrentProxy() to a Boolean makes toggling the off and on behaviour easier.
Using an Array to store variations of words used in the MsgBox() to avoid duplication of code.
Option Explicit
Dim WSHShell, strSetting
Set WSHShell = WScript.CreateObject("WScript.Shell")
'Store strings that will not change and are reused in constants
Const APPNAME = "Proxy Setting"
Const PROXY_SETTING = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable"
'Have main procedure to be single point of script execution.
Call Main()
Sub Main()
'Convert return value to Boolean to make it easy to do toggle.
Dim setting: setting = CBool(CurrentProxy())
Dim state
'Use array to store wordy bits that will be used by the Message Box.
If setting Then
state = Array("enabled", "off")
Else
state = Array("disabled", "on")
End If
If MsgBox("Proxy is " & state(0) & vbCrLf & "Do you wish to switch it " & state(1), vbYesNo + vbQuestion, APPNAME) = vbYes Then
'Toggle is opposite of current state so use Not.
Call ToggleProxy(Not setting)
Call MsgBox("Proxy has switched " & state(1), vbInformation, APPNAME)
End If
End Sub
'Determine current proxy setting and toggle to oppisite setting
Function CurrentProxy()
Dim strSetting
strSetting = WSHShell.RegRead(PROXY_SETTING)
CurrentProxy = strSetting
End Function
'Joined Proxy and NoProxy together into one procedure call and pass in the
'ProxyEnable setting as an argument.
'Subroutine to Toggle Proxy Setting to ON
Sub ToggleProxy(setting)
'Abs() function makes sure we only pass back 1 or 0 as Boolean True in
'VBScript is actually -1.
WSHShell.RegWrite PROXY_SETTING, Abs(setting), "REG_DWORD"
End Sub

Reset Network Adapter

I have here a VBScript to "toggle" my network adapter (built from a script found on the Internet).
I was wondering if maybe someone can help me convert it to a script that can "reset" (disable, then re-enable) my wireless adapter, rather than toggle it.
'~ Toggle a SPECIFIED NIC on or off
Option Explicit
Const NETWORK_CONNECTIONS = &H31&
Dim objShell, objFolder, objFolderItem, objEnable, objDisable
Dim folder_Object, target_NIC
Dim NIC, clsVerb
Dim str_NIC_Name, strEnable, strDisable
Dim bEnabled, bDisabled
'NIC name goes here VVV
str_NIC_Name = "Wi-Fi"
strEnable = "En&able"
strDisable = "Disa&ble"
' create objects and get items
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(NETWORK_CONNECTIONS)
Set objFolderItem = objFolder.Self
Set folder_Object = objFolderItem.GetFolder
Set target_NIC = Nothing
' look at each NIC and match to the chosen name
For Each NIC In folder_Object.Items
If LCase(NIC.Name) = LCase(str_NIC_Name) Then
' proper NIC is found, get it
Set target_NIC = NIC
End If
Next
bEnabled = True
Set objEnable = Nothing
Set objDisable = Nothing
For Each clsVerb In target_NIC.Verbs
'~ Wscript.Echo clsVerb
If clsVerb.Name = strEnable Then
Set objEnable = clsVerb
bEnabled = False
End If
If clsVerb.Name = strDisable Then
Set objDisable = clsVerb
End If
Next
If bEnabled Then
objDisable.DoIt
Else
objEnable.DoIt
End If
'~ Give the connection time to change
WScript.Sleep 5000
I think it's better to use WMI (Windows Management Instrumentation).
Here is a sample script:
Option Explicit
dim ComputerName
dim WMIService, NIC
dim ConnectionName
ComputerName = "."
ConnectionName = "Ethernet"
if not IsElevated then
WScript.Echo "Please run this script with administrative rights!"
WScript.Quit
end if
On Error Resume Next
Set WMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2")
Set NIC = WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = '" & ConnectionName & "'").ItemIndex(0)
if NIC is nothing then
WScript.Echo "NIC not found!"
WScript.quit
end if
WScript.Echo "NIC Current status: " & iif(NIC.NetEnabled, "Enabled", "Disabled") & vbcrlf
if NIC.NetEnabled then
WScript.Echo "Disabling NIC..."
NIC.Disable
WScript.Sleep 1000
WScript.Echo "Enabling NIC..."
NIC.Enable
end if
function iif(cond, truepart, falsepart)
if cond then iif=truepart else cond=falsepart
end function
function IsElevated()
On Error Resume Next
CreateObject("WScript.Shell").RegRead("HKEY_USERS\s-1-5-19\")
IsElevated = (err.number = 0)
end function

VBScript to enable / disable proxy

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

vbs script to change shortcuts working in 7 but not in XP

I'm trying to write a script that will update desktop shortcuts in WinXP and Win7 (32 and 64bit). I'm having 2 problems, in XP, the target path of the shortcut won't change, and in both XP and 7, the "Start In" part of the shortcut won't change. Whats wrong here and how can I correct?
If InStr(GetWindowsVer(), "XP") > 0 then
IterateFolder("C:\Documents and Settings\")
Elseif InStr(GetWindowsVer(), "7") > 0 then
IterateFolder("C:\Users\")
End if
Sub IterateFolder(folderPath)
Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput, subFolder
strFolderToSearch = folderPath
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
Set colSubfolders = objRootFolder.SubFolders
For Each objFolder in colSubfolders
subFolder = objFolder.Path & "\Desktop"
Set objFSO1 = CreateObject("Scripting.FileSystemObject")
Set objFolder1 = objFSO.GetFolder(subFolder)
Set colFiles = objFolder1.Files
For Each objFile in colFiles
If strcomp(right(objFile.name,4),".lnk",vbTexctCompare) = 0 then
Set Shell = CreateObject("WScript.Shell")
Set Link = Shell.CreateShortcut(objFile.Path)
if instr(Link.TargetPath, "Office") > 0 then
strOutput = objFile.Path
strOutput = strOutput & vbCr & vbLf & Link.TargetPath
strOutput = strOutput & vbCr & vbLf & Link.WorkingDirectory
MsgBox strOutput 'This line returns expected data'
Link.TargetPath = Replace(Link.TargetPath, "Office11", "Office14"
if GetBits = "64" Then
Link.TargetPath = Replace(Link.TargetPath, "Program Files\", "Program Files (x86)\")
End if
Link.WorkingDirectory = Replace(Link.WorkingDirectory, "Office11", "Office14")
Link.Save
End if
End if
Next
Next
End Sub
Code was doing exactly what I told it to do, the default compare method used in vbs Replace function is Binary, which is case sensitive. For some reason paths in shortcuts are case insensitive. I added the options to do a vbTextCompare by adding > , 1, -1, 1 onto the end of my replace statements so they now look like this:
Link.TargetPath = Replace(Link.TargetPath, "Office14", "Office99", 1, -1, 1)
So its now replace Office14 with Office99 without regards to case. My target path in XP and the WorkingDirectory in 7 are now both changing!

how to delete multiple folders,desktop and start menu shortcut using vbscript

I never did any vbscript before, so i don't know if my question is very easy one. Following is the flow of steps that has to be done :
Check if exist and delete a folder at c:\test1 if found and continue. If not found continue.
Check if exist and delete a folder at c:\programfiles\test2 if found and continue. If not found continue.
Check if a desktop shortcut and start menu shortcut exist and delete if found. If not exit.
I could delete 2 folders with the following code:
strPath1 = "C:\test1"
strPath1 = "C:\test1"
DeleteFolder strPath1
DeleteFolder strPath1
Function DeleteFolder(strFolderPath1)
Dim objFSO, objFolder
Set objFSO = CreateObject ("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolderPath) Then
objFSO.DeleteFolder strFolderPath, True
End If
Set objFSO = Nothing
But i need to run one script to delete 2 folders in different paths, 2 shortcuts one in start menu and one on desktop.
I was experimenting with this code to delete the shortcut on my desktop:
Dim WSHShell, DesktopPath
Set WSHShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
on error resume next
Icon = DesktopPath & "\sample.txt"
Set fs = CreateObject("Scripting.FileSystemObject")
Set A = fs.GetFile(Icon)
A.Delete
WScript.Quit
It works fine for txt file on desktop, but how do i delete a shortcut for an application from desktop as well as start menu.
strPath1 = "C:\test1"
strPath2 = "C:\test2"
DeleteFolder strPath1
DeleteFolder strPath2
DeleteShortcut
'-------------------------------------------------------
Sub DeleteFolder(strFolderPath)
Set fso = CreateObject ("Scripting.FileSystemObject")
If fso.FolderExists(strFolderPath) Then
fso.DeleteFolder strFolderPath, True
End If
End Sub
'-------------------------------------------------------
Sub DeleteShortcut()
Set WSHShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
shortcutPath = DesktopPath & "\MyShortcut.lnk"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(shortcutPath) Then
Set myFile = fso.GetFile(shortcutPath)
myFile.Delete
End If
End Sub

Resources