Posts Tagged ‘wsh’

h1

Skype and Web Servers

08/08/2009

I briefly had a problem with the web server that’s running my personal wiki yesterday.  Try as I might it just wouldn’t start-up.  I hadn’t had a problem before and after three or four attempts at clicking the start option I took a step back and surveyed my machine – a useful option in many circumstances.

I spotted the culprit straight away as I had come across the issue before – Skype.  When Skype starts up if the default port (51907 on my current instance of Skype) is unavailable (because another application is using it or more likely a firewall is blocking it) it tries to set-up on a port that is more likely to be available – 80 or 443 which are used by web servers for insecure and secure traffic respectively.

Whilst there is an advanced option in Skype to force it away from these two alternative ports often there are no other convenient ones to choose from.  This is because it is good practice security to block unused ports and Skype isn’t always a top runner so to speak on ports to have open.

The moral of the story is simple – start your web server before you start Skype.

However this got me to wondering what else might pick a port that could conflict and how would you know what it was without just systematically working through all the applications and services you have running … which for me could be a large number.  My solution was to use some DOS commands.  NETSTAT can be used to examine what ports are in use by what processes.  TASKLIST can be used then to identify the executable.

To smooth things over a little I’ve concocted a little script to automate this process.  It’s really just a bit of a nicer front end to the whole thing and whilst several command prompt windows may briefly flash up as it runs, the script will take in the information from the commands and process them to filter out the pertinent information.

Option Explicit

Dim objCommandPort
Dim astrResults
Dim astrInfoLine
Dim strOutput
Dim strPortCommand
Dim strPID
Dim strApps

strPortCommand = "netstat -ano" 

set objCommandPort = New clsDOSCommandExecutor

objCommandPort.ExecuteCommand(strPortCommand)
strOutput = objCommandPort.GetOutput
astrResults = split(strOutput, vbCrLf)

strApps = "PORT: APPLICATION" & vbCrLf
AssessPort "80"
strApps = strApps & vbCrLf
AssessPort "443"

MsgBox strApps, vbInformation + vbOKOnly, "Applications using web server ports"

Sub AssessPort(p_strPort)
 Dim intCounter

 For intCounter = 1 to Ubound(astrResults)
 If inStr(astrResults(intCounter), ":" & p_strPort) > 0 Then
 astrInfoLine = split(astrResults(intCounter))
 strPID = astrInfoLine(ubound(astrInfoLine))
 strApps = strApps & vbCrLf & p_strPort & ": " & AppOnPort(strPID)
 End If
 Next
End Sub

Function AppOnPort(p_strPID)
 Dim objCommandPID
 Dim strPIDCommand
 Dim strPIDOut
 Dim astrPIDOut

 set objCommandPID = New clsDOSCommandExecutor
 strPIDCommand = "tasklist /fi ""PID eq " & p_strPID & """"

 objCommandPID.ExecuteCommand(strPIDCommand)

 strPIDOut = Right(objCommandPID.GetOutput, len(objCommandPID.GetOutput) - inStrRev(objCommandPID.GetOutput, "=" & vbcrlf))
 astrPIDOut = split(strPIDOut)

 AppOnPort = Replace(astrPIDout(0), vbCrLf, "")
End Function

Class clsDOSCommandExecutor
 Dim objShell, objExec
 Dim strCommand
 Dim strError
 Dim objError
 Dim objOutput
 Dim strOutput

 Sub ExecuteCommand(p_strCommand)
 strCommand = "cmd /c " & p_strCommand
 Set objShell = CreateObject("Wscript.Shell" )

 objShell.Exec(strCommand)

 Set objExec = objShell.Exec(strCommand)

 Do Until objExec.Status
 Wscript.Sleep 200
 Loop

 Set objError = objExec.StdErr
 strError = objError.ReadAll

 Set objOutput = objExec.stdOut
 strOutput = objOutput.ReadAll
 End Sub

 Function GetOutput()
 GetOutput = strOutput
 End Function

 Function GetError()
 GetError = strError
 End Function

 Function Failed()
 If strError = "" Then
 Failed = false
 Else
 Failed = true
 End If
 End Function
End Class
Advertisements
h1

DOS Command Class for VBScript

08/08/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article.

h1

VBScript and Getting the Message Across

08/08/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article.

h1

DropBox Freezes Out Explorer

05/08/2009

I’ve been using DropBox for some time now and I use it in some ways that I think are quite innovative and will be the subject of future blog posts.  However in the last few days I’ve been having a few problems with Windows Explorer taking forever (well over a minute or two at times) to respond and it responds very sluggishly.  I’ve managed to track the problem down to DropBox misbehaving.

I’m running version 0.6.507.0 as I have been for some time, but something somewhere has started causing these little fits.  The solution is simple.  Bring up task manager, find the dropbox.exe process and kill it.  Then navigate to the start up programs on the Windows start menu and start Dropbox.

Actually whilst that’s simple it all seemed a bit too much of a hassle for lazy me so I wrote a little script to do it.

Option Explicit
Const DROPBOX_FOLDER = "C:\Program Files\Dropbox\"
Const DROPBOX_EXE = "Dropbox.exe"

KillProcess(DROPBOX_EXE)
StartApplication(DROPBOX_FOLDER & DROPBOX_EXE)

Sub KillProcess(p_strProcess)
 Dim objService, objProcess
 Dim colProcesses
 Dim strProcessKill

 Set objService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

 Set colProcesses = objService.ExecQuery("Select * from Win32_Process Where Name = '" & p_strProcess & "'")
 For Each objProcess in colProcesses
 objProcess.Terminate()
 Next
End Sub

Sub StartApplication(p_strAppPath)
 Dim objShell
 Set objShell = WScript.CreateObject( "WScript.Shell" )
 objShell.Run("""" & p_strAppPath & """")
End Sub

Pop a shortcut to the script on your desktop and you should be able to access it quick enough – or even quicker if you set a shortcut key combination on it.

The script uses a couple of fairly standard little routines.  One kills the process and the other starts the application.  If you happen to have DropBox installed in an unusual location (mine is in the default), then just amend the first constant.

You may see two icons

You may see two icons

NB: You’ll probably notice that after the script ruins you’ll have two DropBox icons in your system tray.  If you move your mouse pointer over the two icons the one for the now terminated process will disappear.

h1

VB Script – Count occurrences in a text string

30/07/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article.

h1

A Flexible Progress Window in VBScript

19/07/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article.

h1

Self Installing VB Scripts

18/07/2009

I like using VB Scripts and the Windows Script Host to resolve issues as it has proved quick and flexible for most tasks.  One thing I have found problematic on occasion is the installation of the scripts on a user’s PC.  It takes time to do if it is not being deployed using logon scripts, group policy, etc.  To this end I decided to write a function to allow a script to install itself.   The script below is primarily made up from three functions/routines.

Starting from the bottom, the BuildFolderPath() function just takes a file path and adds a backslash to the end if it needs one.

The next function is a little more interesting.  ScriptPath() returns the folder in which the script is being run from.  Note that if it is being run from a shortcut it is the target folder.  This script works by removing the name of the script from the full file path of the script file.

The last chunk of code is the sub routine InstallScript() and does the majority of the work so to speak.  This checks to see if the script is in the correct location.  If it is not it asks the user for permission to copy it from the current location to the one against which it checked.  This does of course rely on the user having appropriate file permissions to copy the script there in the first place, but if we need anything more then some sort of executable or direct access by an administrator is always going to be best otherwise you’ll be handing out administrator passwords verbally or in plain text in script files.  Once installed (or if the user chose not to install) there is a bit of feedback to the user and the script ends.  If the script is running from the right location, no further action is taken and the script continues.

Option Explicit

Const INSTALL_TO = "%UserProfile%\SendTo"

InstallScript INSTALL_TO

Msgbox "The script is being run from the right place..."

'This routine checks if the script is in the right folder location and if it
'isn't it will give the user the option of copying the script to the right location
Sub InstallScript(p_strInstallTo)
 Dim strInstallToFolder
 Dim objShell, objFSO

 'Set the installation folder
 Set objShell = CreateObject( "WScript.Shell" )
 strInstallToFolder = BuildFolderPath(objShell.ExpandEnvironmentStrings(p_strInstallTo))

 If Not strInstallToFolder = ScriptPath Then
 'Copy the script to the correct folder if the user agrees
 If Msgbox ("This script is not being run from the expected location." & vbCrLf & _
 "Would you like to install it to " & strInstallToFolder & " now?", _
 vbYesNo + vbQuestion, "Install Script") = vbYes Then

 Set objFSO = CreateObject("Scripting.FileSystemObject")
 objFSO.CopyFile WScript.ScriptFullName, strInstallToFolder & WScript.ScriptName

 MsgBox "The script has been installed/updated.", vbOkOnly & vbInformation, "Install Complete"
 Else
 MsgBox "The script has not been installed and will not run.", vbOkOnly + vbExclamation, "Install Cancelled"
 End If

 'Finish the script at this point
 WScript.Quit
 End If

End Sub

'This function returns the path which the script is running from
Function ScriptPath()
 ScriptPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
End Function

'This function just adds a trailing backslash if there isn't one
Function BuildFolderPath(p_strPath)
 If Right(p_strPath,1) = "\" Then
 BuildFolderPath = p_strPath
 Else
 BuildFolderPath = p_strPath & "\"
 End If
End Function

An interesting thing to note is that I’ve passed folder paths through the ExpandEnvironmentStrings() function.  This converts any environment variables in the string and allows them to be converted to their full path.  If you want to know more about environment variables I’d recommend a few minutes browsing this page on environment variables in Windows XP.

So all you have to do is decide on a folder where you want the script run from and then pop these functions into your script.  Personally I like to create shortcuts for scripts too (e.g. in the user’s send to folder, start menu or the desktop).  I’ll be posting a little later on how to add that to your toolbox – particularly useful for installations like this.