Re-Use Your (or someone else’s) VBScript Code

June 20th, 2009 by Steve No comments »

How many of us have searched incessantly for “that script” we wrote/borrowed/stole 2 years back?  OK, I still do that sometimes but I have a method that drastically reduces this usually fruitless but always frustrating script-hunt.  This method leverages Windows Script Host’s capability to reference another script at execution time, simlar to the way programming languages allow you to reference an “include” file.  Basically there are 2 steps to start taking advantage of this sanity-saving technique.

Step 1 – The “Include” File

Create your “include file”.  This is simply a VBS file that has all of the reusable Functions and Subs you have found helpful over the years.  I call mine “Global.vbs”.  You can download the one I use here (rename it with a VBS extension) as a starting point, but be sure to add your additional functions/subs as needed.  Whenever you find or write a cool function/sub, make sure it is well tested then add it to your include file.  If you downloaded mine note the comments at the top which describe the functions/subs contained.  I recommend keeping this comment section up-to-date so that it is easy to see what goodness the file contains.  Once the include file is complete place it in a location that is accessible from anywhere on your network.  If you use Active Directory I suggest placing this in the sysvol SCRIPTS directory (the old NETLOGON share).  That way it will be replicated and accessible from all your domain controllers, can be updated from one place, and when called, the most “local” version will be used (thanks to AD Sites).

Step 2 – The “Main” Script

In step 1 you created an awesome repository of tried and true code.  Now to put it to use.  Create a text file called EXAMPLE.WSF and paste the following in it:

<job>
<script language="VBScript">
	option explicit
	Dim TotalRunTime
	TotalRunTime = Timer()
</script>
<script language="VBScript" Src="\\SERVER\SHARE\global.vbs"/>
<script language="VBScript">
'***************************************************************************************
'***************************************************************************************
'This is a blank script file that includes global.vbs.
'
' Version 0.1
' Author
' Arguements:
'***************************************************************************************
'************************************Insert user code below*****************************

'************************************Insert user code above*****************************
wscript.echo "Total Run Time = " & timer() - TotalRunTime & " Seconds"
</script>
</job>

Keep this EXAMPLE.WSF handy so you can use it as a template to create your scripts.  Everytime you need to write a script just make a copy of EXAMPLE.WSF and write your code between the “Insert code Here” lines.  As you write it and need to reference a function/sub that is listed in the “include” file, just reference it as though the function/sub was already in the script you are writing.  Remember that all of the reusable code from your “include” file is read in during execution time so it is all available to call.

As implied in the title of the blog entry, the main benefit of this approach to scripting is that you reuse your code…no more re-writing the same script you wrote (and lost) several times in the past.  And when you don’t have to re-write code you don’t have to re-debug all the same mistakes you made in the last several times you wrote the script.  Additional benefits include smaller scripts (you don’t have the functions/subs in your main script), and easier scripting for the VBS-uninitiated (all the hard stuff is already written…just call it).

Disclaimer - I did not invent this “Mac Daddy” process but I have used the heck out of it.  All thanks go to Tommy Mills, one of my many scripting mentors…Thanks Tommy!

What is “Good” and “Bad”?

April 2nd, 2009 by Steve 2 comments »

 

What is “good” and “bad”?  What is “right” and “wrong”?  Are these concepts merely constructs of a collective societal will?   A sort of sum of a group’s preferences with some averaging applied to come up with a generally agreed upon “standard”?  Is right and wrong based on preferences?  Let’s explore this idea.

 

One person named Bob has an opinion/feeling about prematurely ending the life of newborn puppies.  Terminating newborn puppies makes him feel good inside and he has always felt deeply that newborn puppies should not be allowed to live.  To Bob it seems like such a conventional, inner belief that he has a hard time understanding how anyone could feel otherwise.

 

The remaining individuals in Bob’s neighborhood, 10 people in all, deeply feel that prematurely terminating the life of newborn puppies is wrong.  Deeply wrong.  They have even attached harsh terms to this practice, calling it “murder” or “atrocity” or “crime”.   So the 10 individuals decide to band together to “protect the rights” of puppies.  They form a group called “Puppies are Precious” and form a written, prescriptive edict declaring that all puppies are precious and that their lives should be protected, enriched, and prolonged to the maximum degree possible.

 

One morning Bob is interrupted from his daily task of prematurely terminating the lives of a few puppies by a stern knock at the door.  With some degree of perturbation he answers the door in his blood-stained apron, knife in hand, only to find 10, first angry then increasingly horrified neighbors standing in his front yard.  A representative of the group collects himself enough to inform Bob that it is entirely, and absolutely wrong to “murder” innocent puppies and that Bob must cease and desist this “atrocious” act immediately.

 

Bob, who highly values his relationship with his neighbors is thoughtful for a moment.  Then as his desire for peace and good relations gives way to righteous indignation he answers the representative by asking “Or what?”.

 

This stuns the representative, who looks back at his fellow “PP” group members for reassurance.  After retiring into the group for a short dialog he rejoins Bob by declaring that the group would normally attempt to bring about change with neighbors through diplomacy but that the “heinous” nature of Bob’s “crime” demands that their group stop him with force if he does not willingly concede to their demands.

 

Now it is Bob’s turn to be stunned.  He had already been completely unprepared for this dialog, and now this threat of force by 10 individuals set him to stammering for a reply.

 

Immediately sensing their advantage, the group pressed Bob with further threats, and informed him that there would be a delegation that would inspect his home weekly for the next 3 months to look for signs of the afore mentioned “crime”.  Additionally a separate delegation would meet with Bob weekly to set about teaching him the ethical principle that puppy’s lives are precious.  The group sincerely hopes that Bob can be brought back into the “mainstream” through education.

 

Bob, who was outmatched by the power of the group is forced to agree to these terms, although in his heart he still “knows” that he is right.

 

Who is right?  Why would the opinion of 10 individuals be more legitimate than that of 1?  Who is it that dictates that the more powerful group’s idea is right?  Does the issue simply come down to who holds power?  Is an individual likely to be converted to a more powerful group’s opinion of right and wrong as a result of being forced to comply with their wishes?  Is it possible that the individual will retain their view and still practice their heart-felt belief insomuch as they can do so without detection by the more powerful group?   What is the opinion of the puppies?

 

Objections

==========

It seems self-evident that this view of ethics is not commensurate with human experience, or at least is lacking in precision.  With some variation, humans seem to have a “built in” concept of right and wrong and it seems to be universal.  One would be hard-pressed to find a culture where cowardliness was thought to be a virtue.  Or where lying was thought to be “good form” by the one lied to.  Or where indiscriminately taking the life of newborn puppies was thought to be “good”.  So the seemingly inherent, universal concept of good and evil in people (with some few exceptions) seems to indicate something beyond preference.  It seems to indicate a pre-programmed moral compass which directs toward a fixed point or standard.  A standard which is broader than any single person or isolated society.

 

Where does this universal moral standard come from if it is broader than the individual or isolated society?  Is it genetic and therefore a product of evolution?   It seems that many of our moral concepts lead to conclusions that are counter-productive to evolution’s driving force, “survival of the fittest”.  The fittest survives by pragmatism, not by principle.  The fittest does whatever is necessary in any given scenario to ensure their survival.  Lying, cheating, cowardliness, etc. are all completely acceptable to the pragmatist if these traits further the goal of surviving and prospering.  Any self-less actions have no place in a moral standard created by evolution.  Yet more commonly than not we approve whole-heartedly of those who perform self-less acts.  While we may not understand such acts, or may feel such acts are beyond what we could or would do, yet we seem to think of them as “good”, and many times wish we could be more like that self-less person.

 

Conclusions

===========

If the moral standard’s source is not an evolutionary process then what is it?  There are perhaps several possibilities but one plausible explanation is that there is a standard of right and wrong that is higher than human preference.  A standard that’s source is external to ourselves but that has been integrated into who we are as humans.  A standard set by a moral law giver.  A law giver with the inherent right to say to human beings, “you may do this, you may not do the other”, and with the power to integrate this standard into our being.  Following this line of reasoning, this lawgiver would most likely be the creator since the one who makes a thing is most likely to have access to the thing’s “inner programming” and who other than the creator of a thing has the right to demand that the thing adhere to a standard?

System Center Operations Manager – ACS Training

March 6th, 2009 by Steve No comments »

SecureVantage has some excellent training available on their web site for Audit Collection Services.  The training consists of a series of presentations from well respected System Center Operations Manager experts.  In fact most of the presenters are MVPs.  On a more dissapointing note, they did let a little-known slacker (some guy named Steve Ross) present on one topic…ACS DR/Failover.  You may want to steer clear of that one. 

Check out the whole series here.

OpsMgr Event Correlation – Branch Site Monitoring

December 11th, 2008 by Steve 10 comments »

I am a big fan of System Center Operations Manager 2007, but in it’s current version it has a limitation.  If your management server is at your primary datacenter and it monitors servers in a remote site, what happens if the WAN link between the two locations goes down?  Ideally your monitoring solution is smart enough to know that the branch site is unavailable and due to this condition it will cease trying to monitor the end points at that branch until the site is available again.  This capability is known as event correlation.  However I have found no way to configure this functionality in OpsMgr.  I have even posed the question on OpsMgr forums and asked OpsMgr MVPs, all to no avail.  So, necessity being the mother of invention (or creative re-utilization) I have pieced together a solution.

Assumptions:

  1. You have computer groups set up in OpsMgr for each branch site.  In my case, my branch sites are also set up in Active Directory as AD sites.  If this is true for you as well be sure to check out Cameron Fuller’s blog about setting up dynamic computer groups based on AD sites.
  2. You know the IP address of the branch site’s router.  We will be ping’ing this to check the availability of the site as a whole.
  3. You have Boris Yanushpolsky’s “rock-on awesome” script to place an entire computer group in maintenance mode (get it here).  Put his script in a folder on your management server, such as “C:\Scripts\GroupMM”.
  4. Your management server has Powershell and the OpsMgr Powershell console component installed.

How Does It Work

The heavy lifting is done by Boris’ maintenance mode script.  All this solution does is determine whether the branch site router is reachable (via our friend “Ping”).  If it is then we record that in the results file (auto-created).  If the router is not reachable then put the computer group in maintenance mode and create a tracking file with the site name (so when we run later we don’t try to put it in maintenance mode again).  The condition and action taken are recorded in the results file.  Later when the router becomes reachable again we remove the computer group from maintenance mode, delete the tracking file and record the condition and action taken in the results file.

To Implement the Solution:

  1. Create a directory on your management server for the branch availabilty monitoring script, such as “C:\Scripts\BranchSiteMonitoring”.
  2. Edit and save the BranchSiteMonitoring script (at the end of this blog entry) in the directory you just created.
  3. Create an input file called “input.txt” and place in the directory you created in step 1. This input file should list the IP address of the branch site router and the OpsMgr computer group name that corresponds to that site.  Separate these two bits of information with a “~”.  Each new IP address and computer group name should be on a new line.  Ensure there is not a blank line at the end!  The input file should look like this:

192.168.1.254~Houston Branch Site

192.168.2.254~Austin Branch Site

etc…

4.    Schedule a task on the management server to run the BranchSiteMonitoring script as often as you like.  Keep in mind the idea is to “catch” the site outage before OpsMgr does so the script can place the end points at the unavailable site in maintenance mode BEFORE getting a bunch of unneeded alerts.  I would start out with running it once every 5 minutes.

Cowardly Disclaimer:

Use this solution at your own risk.  I’ll take responsibility for my actions (like running scripts on my corporate network written by people I don’t know) and you can take responsibility for yours <g>.

The Script:

Big thanks to dm_4ever for his WMI ping function (you thought I was kidding about “re-utilization!).  If the WMI version gives you grief you can try his non-WMI version instead.

 Script Download

VBS script to rename files

November 19th, 2008 by Steve No comments »

So an upgrade gone wrong at my current client site (no I was not doing the upgrade!) caused about 30 file names to be renamed into a 8.3 format.  This little incident brought operations to a halt in almost every branch!  No pressure for the consultant, right!

VBS script to the rescue.  Just create a input (text) file which has the bad file name, the delimiter of your choice, and the correct file name.  It looks like this (with “#” as my delimiter):

badname1.txt#goodname1.txt
badname2.txt#goodname2.txt
badname3.txt#goodname3.txt
etc....

Place this script in the same folder as the input file, change the Path variable to the path containing the files, then run the script.

Const ForReading = 1
 
On Error Resume Next
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.OpenTextFile(".\name_changes.txt", ForReading)
var1 = f1.ReadAll
var2 = Split(var1, vbcrlf)
path = "C:\Test"
For i = 0 To UBound(var2)
                badName = left(var2(i), instr(1, var2(i), "#", 1) -1)
                goodName = right(var2(i), instrrev(var2(i), "#", -1) -1)
                Set filA = fso.GetFile(path & "\" & badName)
                filA.name = goodName
Next
msgbox "All Done!"

By the way, when I showed this script to my son his reply was, “well, that’s not that impressive…”

hmmmm…

In order to use Operations Manager 2007 Web Console it is required for JavaScript to be enabled

November 17th, 2008 by Steve No comments »

I came across this issue recently at a client site.  I installed System Center Operations Manager SP1 on a Windows 2008 server.  I had followed the guidance given on

Walter Chomak’s blog to choose the roles/features required to support OpsMgr. 

Error when accessing web console on Windows 2008 server

Error when accessing web console on Windows 2008 server

All was working fine until I tried using the web console.  I was getting an error which seemed to indicate I had a browser issue:

 

 

When I added CGI support to the Application Development Role Service on my RMS the web console page came up fine on my client machine. W00t!

I love my iPhone

October 26th, 2008 by Steve 2 comments »

I know what you’re saying…who doesn’t?  Well aside from the haters, I don’t know.  I mean, they rock.  It does not just rock on till about 11:30…it rocks on till the break-o-dawn.

Case in point.  So a couple of guys from my current client and I were riding back to San Antonio from Corpus Christi after visiting with some branch offices.  We got a call that a branch was having printer problems.  We were already too far to turn around and were out in the middle of nowhere, but I did have a pretty good signal.  So I VPN’ed in to the client’s network using iPhone’s built in Cisco ipsec VPN client.

Connect to VPN

Connect to VPN

Then I used WinAdmin to remotely connect to the print server. 

Log on to server

Log on to server

After some inspection I noticed an admin had typed in a bogus share name, which prevented the logon script from mapping the printer.

Correct printer share

Correct printer share

I fixed the share name and the users were good to go.  All of this while traveling at 70 MPH on I37.  How hard does that rock?

To preclude the chastisments, no, I was not driving and yes I know you can do the same thing from any Windows Mobile device.  The fact that 2 devices can do the same cool thing does not make one or the other less cool…hater. ;-)