The VBScript Network and Systems Administrator's Cafe:

January, 2009

Jan 30 2009   1:30AM GMT

Easy String Searches with the VBScript Instr function



Posted by: Jerry Lees
InStr function, String manipulation, strings, string, VBScript, Scripting

I recently had a situation where I needed to find string inside a string to parse the larger string into usable and separate chunks of data. In writing this script, I realized I’ve not shared this gem of a function with you previously… and it really makes matters easier when you are manipulating strings.

That function is the Instr function. When configured with the proper parameters, by default, it will return the FIRST location in a string where a sub-string occurs. (Hint: this is where a mid() function would come in handy after you have that starting location). If the sub-string is not found, the function returns 0. The syntax is as follows:

Instr(StartPostion, StringToSearch,StringtoFind, Compare)

 

 The script I wrote really is a blog entry of it’s own, and will be, but I wanted to first introduce you to this great function and let you have a chance to play with it and see some code in action. Below is an example script that takes a string and searched for specific sub strings, then returns the location in the string where the sub string occurs.

 

Option explicit
Dim StrSource

StrSource = “Now I can find a word In a long string of words in a sentance.”

WScript.Echo(InStr(1,StrSource,”In”))
WScript.Echo(InStr(1,StrSource,”in”))
WScript.Echo(InStr(1,StrSource,” in “))
WScript.Echo(InStr(1,StrSource,”now”))

Below is the output from the script. Notice, the following:

  • The case of in and In in the sub-string being searched for and the positions it returns?
  • Notice the substring “in ” returns something entirely different?
  • Notice the substring now returns 0?

 

23
12
48
0

 

Enjoy!

Jan 28 2009   11:40PM GMT

Microsoft CRM Email Router stops working mysteriously and then starts working again– just as mysteriously.



Posted by: Jerry Lees
System Administration, systems management, Microsoft CRM, CRM, Microsoft Dynamics CRM

I’ve been doing a good deal of work with Microsoft CRM lately. Heck, I’ll say it… I’ve been doing ALL my work with Microsoft CRM lately. It’s a beast that no one here at “the office” knows about and I’ve become “The Guy”. You know what that means, scramble to stay ahead of the game and get the environment under control before it blows up in your face!

Well, recently I learned a couple things about the CRM Email router service. This post is the first thing I learned, and I found an article that states exactly what is happening:

You use the Microsoft Dynamics CRM 4.0 E-Mail Router service to process e-mail messages. After a specific period of time, the service stops processing e-mail messages. Then, after the same period of time, the service starts to process e-mail messages again.

Essentially that’s what was happening to me, CRM would send e-mail messages for about an hour then the messages would queue up for a while before being sent again. Really great when those messages are time sensitive… talk about keeping a guy up all night!

You can read all about the solution here, but basically the Microsoft Dynamics CRM 4.0 Update Rollup 1 fixes the problem, though I imagine you could adjust the  ConfigUpdatePeriod mentioned in the article and get a little bit of tweaking out of it in the short term while you test the update in a non-production environment like all good systems admins do. ;-)

Enjoy!


Jan 23 2009   7:21PM GMT

Tip: Searching Google for solutions to a Microsoft product problem



Posted by: Jerry Lees
tips and tricks, troubleshooting, searching google, google, Microsoft, Microsoft Knowledgebase, KB articles

Maybe this isn’t earth shattering, and most all of you already know this trick. However, I just stumbled upon it and am willing to bet at least a few people are troubled by searching google for answers to problems and finding the needle in the haystack. Here’s the scenario:\

You’re looking for Microsoft Knowledge Base articles on a error your getting with a particular Microsoft application and using google to do the search because you can search better with it compared to the Microsoft site. However, you get so much noise back in your results in links to pages that were never solved, the solution is ambiguous, or (frankly) the solutions that are given are coming from sources where you simply question which end of the gene pool they are standing in at the time they wrote the proposed answer… you know what I’m talking about. Those guys that give answers like Format and call me back later or “Re-install the OS to get the drivers updated”.

Well, I suddenly noticed a trend at the bottom of some KB articles I had to call MS to get an answer to a problem. They are tagging the articles with key words! Yes, I know it’s not earth shattering. However, the following might help you.

For troubleshooting articles, it seems they always tag it with: kbtshoot

For articles that mention a event log error message they always tag it with: kberrmsg

Soooo…. If you need to search google for a Microsoft product KB article put this in as part of your search! It greatly limits the number of noisy results in your search! For example, you get trouble shooting KB articles for Microsoft CRM try this in a Google search:

kbtshoot “Microsoft CRM”

Of course that’s very generic… but hopefully you get the idea. Enjoy!


Jan 19 2009   4:14AM GMT

Windows Tip: Clearing Internet Explorer cached data from the command line



Posted by: Jerry Lees
tips and tricks, windows tips, command line, batch commands, batch files, Internet Explorer, rundll.exe, windows secrets, undocumented windows

As a Network Administrator sometimes you need to clear the browsing history, cookies, or the like for a entire community of users. Sure, you could tell them how to do it thinking that you were being super smart by providing instructions for them. However, as you well know there will be some who will be unable to follow the directions and you will spend 3 times the amount of time it took you to write the directions and refine them to a point where any one could do it— or so you thought. All the while, thinking about the amount of time you will save by having the users do it.

Why not be really lazy!!!??!? And do it with a batch script? You could then put it in their login script and it would happen at their next logon, or every logon for that matter. Well, there’s an easy way to do this with windows’ built in functions– it’s just obscure enough to not be found anywhere you would normally look!

Yo can preform many of the things I mentioned (and more) with Microsoft’s RUNDLL.EXE file, plus you don’t have to write a line of VBScript to accomplish the task!

Here is a short list of some tasks you can preform on IE, along with the command to do it… give them a shot!

Temporary Internet Files
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

Cookies
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2

History
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1

Form Data
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16

Passwords
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32

Delete All
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255

Delete All - “Also delete files and settings stored by add-ons”
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351

 These commands should work in Internet Explorer 7


Jan 16 2009   1:56AM GMT

Word Tip: Quick and easy tables in Microsoft Word



Posted by: Jerry Lees
Microsoft Word, Word Tips, tips and tricks

This isn’t a VBScript tip— or even a systems administrator tip. However, it is a valuable tip on a quick easy way to create tables in word that are EXACTLY the size you need without knowing ahead of time and doing a lot of clicking in the menus… and if your like me with the number of tables you have to create for documentation this tip is a life saver!

Essentially, You Type the rows like you would a normal sentence except between where you want the columns you place a tab. When the row is completed, instead of pressing ENTER to start a new row… press SHIFT ENTER to start a new row. Then, end the table by simply pressing ENTER.

Finally, highlight the text you wanted to make into a table and select “Make Table” or “Insert Table” from the menu in Microsoft Word, depending on your version. TAHDAH!!!

Here is an example:

This is a cell ”Tab” in a row ”Tab” that is the first row ”Shift Enter”
This is another cell ”Tab” in another row ”Tab” that is the second row ”Shift Enter”
This is a cell ”Tab” in the last row ”Tab” that is the end of the table ”Enter”

It should look like this after you select to Insert the table:

This is a cell in a row that is the first row
This is another cell in another row that is the second row
This is a cell in the last row that is the end of the table


Jan 12 2009   1:46AM GMT

Windows Tip: Moving a window from off screen back into view



Posted by: Jerry Lees
tips and tricks, windows tips

If you’ve ever hooked up your laptop to a secondary monitor and then disconnected without remembering to move the windows back to the primary desktop, you’ve probably encounted this problem: 

The application is running. You can see it in the taskbar, but you can’t see it on the screen, because it still thinks it’s running on the secondary monitor. You try and use right-click, Move, but that doesn’t do anything, and the window doesn’t move anywhere. Maybe you try doing a minimize and then a restore… but still the window doesn’t come into a area of the screen you can actually use. Likely, you simply end up rebooting.

There’s a simple trick to get around this. First make sure you’ve clicked on the windows to bring it into focus. Then right-click on the taskbar and choose Move, then hit any one of the arrow keys and simply move your mouse.

TAAADAHHH!!! Enjoy!


Jan 8 2009   2:18AM GMT

Essential tools: Fiddler Web Debugger, Display HTTP packets LIVE without a packet sniffer



Posted by: Jerry Lees
essential tools, System Administration, Web applications, web sites, webmaster, free software, free tools

You may have been thinking I had forgotten about this series of articles since I haven’t posted to it in a while. Nope! Just haven’t run across anything that was truely aazing enough to be an “Essential Tool”. Well, I recently found just that web I had to troubleshoot a application that was making calls to a web service.

The tool i was lead to is Fiddler, the Web Debugger!!! If you’re like me and you have to trouble shoot web applications that make web service calls– you’re going to LOVE fiddler. No more digging through logs to determine problems real time, especially since IIS caches the log data and writes it in chunks.

Fiddler displays the HTTP (and HTTPS) traffic originated from a machine it is ran on and then shows the cooresponding response from the server you connected with in the request. It’s a real life saver, especially if your not certain the packet is well formed for a SOAP web service on the other end. As an added bonus, Fiddler also allows you to see the raw HTTP (or HTTPS) headers and response headers.

Best of all it’s FREE!!! (We’ll it cost me a $250 call to Microsoft to find out about it… but the application is free and for you the information is free!) Try it out— TODAY!


Jan 5 2009   2:03AM GMT

Retrieve environment variable values from a remote system with WMI



Posted by: Jerry Lees
WMI, Environment Variables, Win32_Environment, VBScript, Windows Management Interface, System Administration, Remote management, PATH variable

Back in October I shared with you a way to retrieve environment variables with a script titled Using Environment variables inside a VBScript script. The script worked like a champ and got me through several tight binds in gathering information, hopefully it has for you as well. However, it has one flaw… it doesn’t work on a remote system! In fact, it only works on the system that the script is running on.

So, I had to recently go back to the drawing board and create a script and series of functions that would work remotely so that I could wrap it in a function to gather information from a bunch of servers. I also Learned a thing or two in the process!

First, The script uses the Win32_environment classes in WMI and this class returns ALL envirnment variables for ALL users. That threw me for a loop when I saw multiple PATH environment variables– some that were different. Second, The search is case sensitive without using the SQL % wildcards (I didn’t need them, so implementing that is left up to you the reader.)

I have the three functions I wrote, that are really variations of one another below. Just add them to your script and call them with the appropriate values and you should be in business!

Function RemoteAllEnvironmentVariables(ServerName)
     ‘Lists all environment variables on the remote system
    
Dim objWMIService, colItems, objItem
 
     RemoteAllEnvironmentVariables = “”
     Set objWMIService = GetObject(”winmgmts:\\” & ServerName & “\root\cimv2″)
     Set colItems = objWMIService.ExecQuery(”Select * from Win32_Environment”)
     For Each objItem in colItems
         RemoteAllEnvironmentVariables = RemoteAllEnvironmentVariables & objitem.name & “: ” & objItem.VariableValue & VbCrLf
     Next
End Function

Function RemoteAllEnvironmentVariablesVariations(ServerName, VariableName)
     ‘Lists all environment variables on the remote system, along with every case variation
     Dim objWMIService, colItems, objItem
 
     RemoteAllEnvironmentVariablesVariations = “”
     Set objWMIService = GetObject(”winmgmts:\\” & ServerName & “\root\cimv2″)
     Set colItems = objWMIService.ExecQuery(”Select * from Win32_Environment”)
     For Each objItem in colItems
         If UCase objItem.name) = UCase(VariableName) Then
         RemoteAllEnvironmentVariablesVariations = RemoteAllEnvironmentVariablesVariations & objitem.name & “: ” & objItem.VariableValue & VbCrLf
         End If
     Next
End Function

 
Function RemoteEnvironmentVariable(ServerName, VariableName)
     ’Lists all environment variables on the remote system, exactly as you typed it in the function call.
     Dim objWMIService, colItems, objItem
 
     RemoteEnvironmentVariable = “”
     Set objWMIService = GetObject(”winmgmts:\\” & ServerName & “\root\cimv2″)
     Set colItems = objWMIService.ExecQuery(”Select * from Win32_Environment Where Name = ‘” & VariableName & “‘”)
     For Each objItem in colItems
         RemoteEnvironmentVariable = objItem.VariableValue
     Next
End Function


Jan 2 2009   3:01AM GMT

Searching the Windows Eventlog for specific events with WMI



Posted by: Jerry Lees
Win32_NTLogEvent, WMI, Windows Management Interface, VBScript, Systems Administration, systems management

On occasion as a systems administrator you have to find the proverbial needle in the haystack with respect to the events in the event logs. You know what I mean, the one event you care about and need to know when it occured as part of your troubleshooting… then throw in that you need to do it in many servers. That’s a mess!

Sure, you can use event viewer and pull out some superadmin skills to filter the events to only see the ones you want– but your still only looking at one server at a time! Yes, you could export the events from multiple servers to a CSV file and then compile them into one excel spreadsheet– but that would take hours to do.

What if I told you there was a way to do it with VBScript? How much would you expect to pay? Three easy payments of 19.99?? … WAIT, don’t answer because it’s FREE!

The script below calls a function it defines called GetLogInfo to gather the requested event information to standard the standard output (the console). The function uses the Win32_NTLogEvent class from— you guessed it, our life long friend WMI! It accepts four inputs, in order; a string that is the name of the server, the Event ID that you are looking for, the specific application log you want to search, and the date in YYYYMMDD format. (Hint: if you have custom event logs on your server, or it is a DNS server or a Domain Controller, you can specify the name of the log instead of Application, System, or Security to get at the log information.)

Here is the script I wrote:

GetLogInfo “ServerName”,”1309″, “application”, “20081218″

Function GetLogInfo( StrComputer1, EventID, EventLogType, YYYYMMDD)

    Dim objWMIService, colItems, objItem
    Dim TempStr

    On Error Resume Next
    ‘ error control block
    Set objWMIService = GetObject(”winmgmts:{impersonationLevel=impersonate}//”_
    & strComputer1 & “\root\cimv2″)
    Set colItems = objWMIService.ExecQuery (”Select * from Win32_NTLogEvent Where EventCode=” &_
    EventID & ” and logfile=’” & EventlogType & “‘”)
    For Each objItem in colItems
        TempStr = “”
        If mid(objItem.timegenerated,1,8) = YYYYMMDD Then
            TempStr = objItem.message
        End if
    Next
    On Error GoTo 0
End Function