Thursday, January 29, 2015

Powershell scripting - an advanced lesson

Intro:


So you now kind of know what powershell scripting is, and there's a better way of scripting via a Microsoft utility included with Windows 7 and newer OSes.  You now have even used a parameter in your script so it's re-usable for different machines.  What else is available?  invoking a web service and displaying the results to the user!

Coding:


I'm rather fond of the parameter based concept for these scripts, methinks I'll use one.  I found a handy dandy free soap service on the internet that converts Celsius to Fahrenheit (hey, it could be useful for the metric system users out there).  Time to break out a cmd-let called New-WebServiceProxy.  As you can see in the following code it's quite easy to invoke a web-service in your script:

param(
    [Parameter(Mandatory=$true)][string]$conversionValue
)

$svc = New-WebServiceProxy http://www.w3schools.com/webservices/tempconvert.asmx
$result = $svc.CelsiusToFahrenheit($conversionValue)
write-host "You entered " $conversionValue "degrees celsius and it converted to " $result "degrees fahrenheit"

You basically make a local variable and pass the URL as the parameter to the New-WebServiceProxy.  from there you call the variable.MethodName and pass any parameters that might need to be used and store the results in a variable to do what you wish.  For our purposes I'll write out the results .

Time to run the script, check out the results:








Huzzah!

Source:


No comments:

Post a Comment