Set Proxy authentication for ie using Vbscript - vbscript

I want to auto fill proxy authentication value of username and password for ie using vbscript.
After I added the proxy ip and port to Tools>Internet Option>Connection Tab>LAN Settings. I am prompted with the following dialog
Is there anyways using VBS OR VB to auto fill this ?
So, far i have got the code like so
'begin script
Option Explicit
Dim valUserIn
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)
valUserIn = Inputbox("Enter the Proxy server you want to use.","Proxy Server Required","proxygate.mydomain.com:8080")
if valUserIn = "" then
RegLocate = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable"
objShell.RegWrite RegLocate,"0","REG_DWORD"
'MsgBox "No proxy mode"
else
RegLocate = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer"
objShell.RegWrite RegLocate,valUserIn,"REG_SZ"
RegLocate = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable"
objShell.RegWrite RegLocate,"1","REG_DWORD"
'MsgBox "Proxy mode: " & valUserIn
end if
WScript.Quit
'end script
But this only set's the proxy ip and port.
Thanks in advance..

You can utilize the Sendkeys shell command which will type in and activate keyboard commands for you once it has allocated the correct screen. However, this is assuming you are running the script while it is open. If you are looking for something that prefills that data before hand. I'm afraid I cannot assist in that realm.
Dim WshShell, Success
Set WshShell = WScript.CreateObject("WScript.Shell")
'Wait for window to popup....
'if this doesn't activate it directly, try clicking on the window.
Do Until Success = True
Success = objShell.AppActivate("Windows Security")
Wscript.Sleep 1000
Loop
WScript.Sleep 500
WshShell.SendKeys "Username"
wscript.sleep 500 'allow program to have time to type it in.
WshShell.SendKeys "{TAB}" 'tab for password field
WshShell.SendKeys "Password"
wscript.sleep 500 'allow program to have time to type it in.
WshShell.SendKeys "%o" 'Alt + O for hitting "OK"
wscript.sleep 500

Related

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.

Sendkeys "variable"

Is it possible to use the SendKeys the type the characters inside a variable.
for example I got characters stored in variable firstname = Regie
Here is my code.
On Error Resume Next
Dim objSysInfo, objUser
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
firstname = objUser.givenName
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Notepad"
WScript.Sleep 400
WshShell.SendKeys "{& objUser.givenName}"
WScript.Sleep 250
Or is there any way to make this happen?
Sure you can, try this little script on the command line, it will print "test".
Set WshShell = WScript.CreateObject("WScript.Shell")
keys = "test"
WshShell.SendKeys keys

Scripting MMC with vbscript

I would like to add a snap in via vbscript and I have been having a problem getting the snap in to add to the console. It will be run in a Windows 7 environment. If someone could have a look see and direct me in the right direction I would be most grateful. Thanks.
<code>
'Elevated privileges start
'Start of UAC workaround code
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If WScript.Arguments.length =0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", Chr(34) & _
WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
consoleName = "C:\Burnett.msc"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(consoleName) Then
Wscript.Echo "console already exists"
Else
On Error Resume Next
Set objMMC = CreateObject("MMC20.Application")
If err.Number <> 0 Then
Wscript.Echo "an error occurred. unable to create mmc console"
Wscript.Quit(0)
End If
objMMC.Show
Set objDoc = objMMC.Document
objDoc.snapins.add("Local Computer\Non-Administrators")
if err then
'Trap the error just after the statement where an error/exception can occur and handle it elegantly
msgbox("Snap-in Not found")
err.clear
end if
objDoc.ActiveView.StatusBarText = "Pane 1|Pane 2|Pane 3"
objMMC.UserControl = 1
objDoc.Name = consoleName
objDoc.Save()
End If
Set fso = Nothing
End If
</code>
"Local Computer\Non-Administrators" is just a system-supplied description for the particular configuration of a snap-in. In this case, the actual snap-in name is "Group Policy Object Editor". Thus to eliminate the error in the code change
objDoc.snapins.add("Local Computer\Non-Administrators")
to
objDoc.snapins.add("Group Policy Object Editor")
Unfortunately, this will only get you as far as MMC putting up a "Select Group Policy Object" dialog. You will then have to manually select the configuration you need using that dialog. As far as I can tell there is no way to supply Snapins.Add with the parameters to select the local non-admin users.
The code below will fully automate the process of setting up the snap-in. However, its reliance on SendKeys makes it extremely brittle. It worked on my system, but there's a good chance you'll need to modify the sequence of key strokes and/or the timing delays to make it work on your system. And once you get it working, there's no guarantee it will continue to do so as local conditions are mutable and can greatly effect the timing.
option explicit
if WScript.Arguments.Named.Exists("elevated") = false then
'Launch the script again with UAC permissions
CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
WScript.Quit
end if
Dim mmc : set mmc = WScript.CreateObject("MMC20.Application")
mmc.Show
mmc.UserControl = 1 'to keep MMC open
Dim oShell : set oShell = WScript.CreateObject("Wscript.Shell")
oShell.AppActivate "Console1"
WScript.Sleep 200
oShell.SendKeys "%f"
WScript.Sleep 200
oShell.SendKeys "m"
WScript.Sleep 400
oShell.SendKeys "group{TAB}{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{TAB}{TAB}{RIGHT}{TAB}Non{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{TAB}{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{TAB}{TAB}{TAB}{ENTER}"

VB script on Cisco Anyconnect VPN

I am new to use VB script. I am using the below code to connect my VPN. But the problem is that after entering "select" button in VPN client, the second page display is depending on Network Speed. Sometimes it is loaded within 4 sec, sometimes after 10 sec. Is there any code where i can get the VPN is fully loaded or not (like BUSY command for IE).
set WshShell=Wscript.CreateObject("Wscript.Shell")
WshShell.Run("""C:\\Program Files\Cisco\Anyconnect\vpnui.exe""")
WScript.Sleep 500
WshShell.SendKeys "{ENTER}"
WScript.Sleep 500
WshShell.SendKeys "username"
WshShell.SendKeys "rsa_no"
WshShell.SendKeys "password"
WScript.Sleep 500
WshShell.SendKeys "{ENTER}"
Whilst not vb script, I think this approach should still work.
I have the vpncli directory in my %path%
I have a batch file:
vpncli.exe connect xyz.123.com -s < d:\vpncreds.txt
with the credentials in a separate file (d:\vpncreds.txt):
username
password
y
Note: you need an empty line at the end.
This works fine here, and wonder if you take the credentials out of your VB script and put them in a separate file, it might achieve what you need to.
Also, if your credentials change, you only need to change the one file, and not the potential handful of script files if you access the vpn in multiple scripts.
Try my code below. Note that you may have to adjust sleep times (in milliseconds). To see what's happening in the command prompt, change the 2 in the 9th line to a 1.
Dim host, username, password, pathToClient
host = "yourHostURL"
username = "yourUsername"
password = "yourPassword"
pathToClient = "C:\Program Files {(}x86{)}\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"
Set ws = WScript.CreateObject("WScript.Shell")
ws.run("TASKKILL.exe /F /IM vpnui.exe"), 0, false
ws.run("cmd.exe"), 2, false
ws.AppActivate("Command Prompt")
WScript.Sleep 300
ws.SendKeys """" & pathToClient & """ connect " & host & "~"
WScript.Sleep 1000
ws.SendKeys(username & "~")
WScript.Sleep 50
ws.SendKeys(password & "~")
ws.run("TASKKILL.exe /F /IM cmd.exe"), 0, false
Here is an improved version that doesn't wait for sleep timeouts, i.e. connects as soon as the window becomes visible. This works with the latest version of AnyConnect (4.10 as of writing).
' Script to automatically connect to Cisco AnyConnect.
' This script assumes that you have already set up your connection.
Const Password = "[YOUR PASSWORD]" ' Enter your password here
Const ConnectionUrl = "[YOUR CONNECTION URL]" ' Enter the URL of your endpoint (without HTTP prefix)
' Copy password to clipboard in case something goes wrong (to connect manually)
CopyToClipboard(Password)
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """%PROGRAMFILES(x86)%\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"""
ActivateWindow("Cisco AnyConnect Secure Mobility Client")
WshShell.SendKeys "{ENTER}"
ActivateWindow("Cisco AnyConnect | " + ConnectionUrl)
WshShell.SendKeys "{TAB}"
WshShell.SendKeys Password
WshShell.SendKeys "{ENTER}"
Function ActivateWindow(title)
Const Step = 100
Const Timeout = 10000
Dim Result
Dim Counter
For Counter = 0 To Timeout Step Step
Result = WshShell.AppActivate(title)
If Result Then Exit For
WScript.Sleep Step
Next
If Result = False Then
MsgBox("Window '" + title + "' not found.")
WScript.Quit
End If
End Function
Function CopyToClipboard(Input)
If IsNull(Input) Then
Set Clipboard = CreateObject("HTMLFile").parentWindow.clipboardData.getData("Text")
If IsNull(Clipboard) Then Clipboard = ""
Else
CreateObject("WScript.Shell").Run _
"mshta.exe javascript:eval(""document.parentWindow.clipboardData.setData('text','" _
& Replace(Replace(Replace(Input, "'", "\\u0027"), """","\\u0022"),Chr(13),"\\r\\n") & "');window.close()"")", _
0,True
End If
End Function

How to output Command prompt to a log file using VBScript

I'm not a programmer so I don't want to overly irritate the fine folk in this forum. My issue is that I would like to use VBScript to Telnet into a Linux device, issue a DF command and output all response to a log file which I can parse later. I originally found a method to successfully Telnet but I have have been experimenting without success regarding the text file output requirement. The following code certainly does not work but I am wondering if I am even close to the correct method?
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("cmd /c dir")
WshShell.run"cmd" '*** open command window ***
WScript.Sleep 250
WshShell.SendKeys("{Enter}")
WshShell.SendKeys"telnet 10.13.2.2"
WshShell.SendKeys("{Enter}")
WScript.Sleep 2000
WshShell.SendKeys"root"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
WshShell.SendKeys"password"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile("C:\VBSmemSize.txt", 2, True)
WshShell.SendKeys"df /mnt/cf"
WshShell.SendKeys("{Enter}")
Do
strFromProc = oExec.Stdout.Readline()
WScript.Echo strFromProc
Loop While Not objLogFile.StdOut.atEndOfStream
You can capture output from external commands but not at the same time interact with them like you do with sendkeys. Here an example of what works
Function ExecPing(strTarget)
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strTarget)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "antwoord van") Then '"reply from" in E
WScript.Echo VbCrLf & strTarget & " responded to ping."
ExecPing = True
Else
WScript.Echo VbCrLf & strTarget & " did not respond to ping."
ExecPing = False
End If
End Function
ExecPing pcname

Resources