Tag Archives: Scripting

Cisco VPN – Launch a Hidden Constant PING Window on Connect

The other day I blogged about getting Cisco VPN to stay connect in Vista. The fix was a constant ping. It is all good but then you have this CMD window open all day in your taskbar, just taking up space, etc.

I decided to workaround it.

I created a .vbs (Visual Basic Script) called pingserver.vbs and put this in it:

Set WshShell = WScript.CreateObject("WScript.Shell")
cmd = "ping servername -t"
Return = WshShell.Run(cmd, 0, True)
set WshShell = Nothing

(replace servername with your name of the server you want to constant ping)

Then in Cisco VPN. Options Menu->Application Launcher. Check "enable" and browse to your VBS. Then when you connect it will run that VBS file, and you will have a hidden constant ping going to your server. Nice..

 

VBA – Reading a Base64 Element into XML and using as Byte Array

Ok, more VBA

Getting a response back as Base64, but when trying to convert it from XML to binary data just having issues.. coming back as ASCII which converts wacked.

What you need to do:

Dim MyInfo As MSXML2.IXMLDOMNodeList
Set MyInfo = xmlDoc.getElementsByTagName(“MyBase64Element”)
MyInfo .Item(0).DataType = “bin.base64”

Dim image() As Byte
image = MyInfo .Item(0).nodeTypedValue

then you can use it in a byte array and convert to an image or whatever datatype you need.

The key here is overriding the type (probably Variant/String by default) to “bin.base64” and then making sure to use the “nodeTypedValue”

This one threw me for a loop for a little while :)

MSXML2.IXMLDOMNodeList – Loading XML from files or strings

Again with the VBA, working with MSXML2.IXMLDOMNodeList objects. How do you load XML? Well MSDN shows you how to do it from an XML file..

Loading from an XML File:

Dim MyIXMLDOMNodeListVar As MSXML2.IXMLDOMNodeList
Dim xmlDoc As New MSXML2.DOMDocument30

xmlDoc.Load “c:myxml.xml”

If (xmlDoc.parseError.ErrorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox (“You have error ” & myErr.reason)
Else
   Set MyIXMLDOMNodeListVar = xmlDoc.getElementsByTagName(“MyElement”)
End If

Loading from XML string:

Dim MyIXMLDOMNodeListVar As MSXML2.IXMLDOMNodeList
Dim xmlDoc As New MSXML2.DOMDocument30
Dim myXml As String

myXml = “<MyElement>Steve Rules!</MyElement>”
xmlDoc.loadXML (myXml)

If (xmlDoc.parseError.ErrorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox (“You have error ” & myErr.reason)
Else
   Set MyIXMLDOMNodeListVar = xmlDoc.getElementsByTagName(“MyElement”)
End If

You will notice there isn’t much difference, except when you want to load from a string, you use xmlDoc.loadXml instead of just xmlDoc.Load .. I wish I could have found that in the documentation somewhere (maybe it’s there and I just didn’t look hard enough) :)

VBA vs VB.NET – turn bytes into bitmaps

So, recently working on some things, I have noticed the HUGE difference between VBA and VB.NET, specifically with turning bytes into bitmaps.. (assume GiveMeBytes() returns a byte array that is a bitmap)

VB.NET:

Dim image As Byte() = GiveMeBytes()
Dim memStream As MemoryStream = New MemoryStream(image)
Dim bitImage As Bitmap = New Bitmap(System.Drawing.Image.FromStream(memStream))

bitImage.Save(“C:test.bmp”)

 

VBA:

Dim image() As Byte
image = GiveMeBytes()

Dim bitImage

bitImage = FreeFile
Open “c:test.bmp” For Binary Access Write As bitImage
Put #bitImage, , image()
Close #bitImage

Now, maybe there is an easier way in VBA/VB6 to do it, but this is the way I learned way back in the day. I am sure you can do something with the FileSystemObject (I am guessing)..

You notice that the VB.NET snippet mostly deals with converting bytes to MemoryStream to Bitmap, and the saving is 1 line, whereas the VBA is really nothing with converting but mostly deals with saving the file. Can we get the best of both worlds? I am not sure, but I still like the .NET implementation better, it just seems “cleaner” to me, and the VBA just seems “dirty”, but they both do the same thing.. (notice in the VBA I didn’t strongly type the bitImage Dim)

In conclusion…”viva .NET!” :)

Reporting Services Scripter: Sync Reporting Services Instances and Objects

The other day, I was tasked with moving all objects from one SQL Server Reporting Services instance to another. I know you can move the database itself, but then you run into issues with encryption keys etc. I just wanted to move the objects. I know that pretty much everything in SSRS is extensible, so I knew I could write something to do it, but before I went out and coded it, I Googled first to see if someone else had something. Turns out there is an awesome utility: Reporting Services Scripter. Works like a charm!

Route: Saturday Night Programming after the bars..

OK, I have to admit, this is weak. But, if you want to learn how to create an infinite loop, with one word, this is it. Route.

I happened upon this maybe 5-6 years ago, when we ended up having to set static routes on computers because of some goofy office politics, but anyways..

Create a batch file. Name it route.bat. Edit it, type in the word route. save, exit, and run it. Infinite loop.

image

Fun stuff. Not bad for an after the bar exercise..

1729 – Saturday Night Programming before the bars: Natural Numbers…

Ok, so this afternoon I watched the movie “Proof” – really good movie. In the movie, they talk about the number 1729, about how it is the smallest number expressible as the sum of two cubes in two different ways. It also is a natural number – when its digits are added together, produces a sum which, when multiplied by its reversed self, yields the original number.

Just for the helluva it, I decided to write a little program in C# to do this. It would be nice if there was an easier way to reverse strings in .NET, maybe there is and I just don’t know. Anyways, I love how movies can get you into things you never thought you would get into.. now I only wonder what I will code up when I get back from the bars..

using System;
using System.Collections.Generic;
using System.Text;

namespace NaturalNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            // when its digits are added together, produces a sum which, 
            // when multiplied by its reversed self, yields the original number:

            for (System.UInt64 i = 1; i < 9223372036854775808; i++)
            {
                string nums = i.ToString();

                System.UInt64 sum = 0;
                System.UInt64 product = 0;

                // get the sum of each digit of the number
                for (int b = 0; b < nums.Length; b++)
                {
                    sum += System.Convert.ToUInt64(nums[b].ToString());
                }

                string nums2 = sum.ToString();

                // reverse the sum
                char[] temp = nums2.ToCharArray();
                Array.Reverse(temp);
                nums2 = new string(temp);

                // multiply the sum times the reversed sum
                product = sum * System.Convert.ToUInt64(nums2);

                // if they equal we hit the jackpot
                if (product == i)
                {
                    Console.WriteLine(i.ToString() + " is a natural number");
                }

                if (i % 10000000 == 0)
                {
                    Console.WriteLine(i.ToString());
                }
            }
        }
    }
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Technorati tags: , , , ,