Friday, August 3, 2012

Calling an Asmx Service using PowerShell

So a recent problem we had at work was calling a one-way asmx service on some kind of repeatable schedule; no security concerns to speak of, it was all internal-facing. So it became my job to write something as small and light as possible.

My first approach involved jumping into what I will call our 'existing' ticketing system. The popular analogy we were using was 'using an ICBM to kill a mosquito'. It involved a windows scheduler waking up an executable to drop a message onto an MSMQ, that was picked up by a BizTalk service, and was routed to some command processor. In this case, that would have been an HTML send port in same said BizTalk world... not simple, not quick, and pretty much vomit-inducing complexity for what should be REALLY dumb and simple.

My next approach, somewhat better: a quick console app, pulling the configurable values (server, command parameters) from the app.config. It looked something like this:


        static void Main()
        {
            try
            {
                var username = ConfigurationManager.AppSettings["username"];
                var password = ConfigurationManager.AppSettings["password"];

                var client = new XXXSoapClient();
                client.ProcessXXX(username, password);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadLine();
            }
        }

Better, and certainly usable in our current world. Our Enterprise Architect made a great suggestion to me though; check out  PowerShell, see if there is some really light way to just 'get' the proxy and make the call. Turns out, there was:


        $page = New-WebServiceProxy -Uri 'http://server/InternalServices/XXX.asmx'
        $page.ProcessXXX('USER','PASS');

And there you have it. Two PowerShell lines to call the server, get a proxy, and make the call.

No comments:

Post a Comment