Windows Vista Forums

Access web services through powershell
  1. #1


    Matt Guest

    Access web services through powershell


    Hi,

    All the examples I've seen are where people download xml from a url: like
    abishek's weather function:

    $url="http://xml.weather.yahoo.com/forecastrss?p=10036"
    $web = new-object system.net.webclient
    $weather = [xml]$web.downloadstring($url)

    These are relatively simple. But is it possible to access soap apis? I'd
    like to access google or live search and those examples are only provided for
    vb.net and c# usually.

    If someone could point me in the right direction I'd appreciate it.









      My System SpecsSystem Spec

  2. #2


    Mark [exmsft] Guest

    Re: Access web services through powershell

    Hello Matt,

    Here's one way to achive this; it assumes you have the .NET 2.0 SDK installed
    for the first step..

    1. Using wsdl.exe (part of the .NET SDK), you can generate a c# or vb.net
    proxy class for calling the webservice. You need to know the name/location
    of the WSDL file that describes what methods the service provides. I'll
    be using the dictionary service provided at http://services.aonaware.com/DictSer...ctService.asmx.

    example:

    wsdl /outictService.cs http://services.aonaware.com/DictSer...vice.asmx?WSDL

    2. Using csc.exe, compile the source file into a library (dll) to use:

    csc /t:library DictService.cs

    3. Load that library into PS (I do this in my profile):

    if ([System.IO.File]::Exists('c:\temp\DictService.dll')) {
    [Reflection.Assembly]::LoadFrom("c:\temp\DictService.dll")
    }

    (I check to see if the file exists because I use this profile on a few machines
    and not all of them have the dll available on them).

    3a. (optional) Create a function (again, I do this in my profile) to call
    the method(s) on the webservice that you are interested in.

    $globalict = new-object DictService
    function Def ($word)
    {
    $x = $Dict.Define($word)
    for ($i = 0; $i -lt $x.Definitions.Count; $i++) {
    write-host $x.Definitions[$i].Word
    write-host $x.Definitions[$i].WordDefinition
    }
    }


    If you already have the source code in c# or vb.net, you could just use csc.exe
    or vbc.exe (which can be found in $nev:windir\Microsoft.NET\Framework\v2.0.50727)
    to compile it and skip step #1.

    thanks,
    mark


    > Hi,
    >
    > All the examples I've seen are where people download xml from a url:
    > like abishek's weather function:
    >
    > $url="http://xml.weather.yahoo.com/forecastrss?p=10036"
    > $web = new-object system.net.webclient
    > $weather = [xml]$web.downloadstring($url)
    > These are relatively simple. But is it possible to access soap apis?
    > I'd like to access google or live search and those examples are only
    > provided for vb.net and c# usually.
    >
    > If someone could point me in the right direction I'd appreciate it.
    >




      My System SpecsSystem Spec

  3. #3


    Marcel J. Ortiz [MSFT] Guest

    Re: Access web services through powershell

    Another way is to use System.Net.HttpWebRequest to send the SOAP message and
    get back the reply. Its a bit more convoluted than simply using wsdl.exe
    though so I would suggest using Matt's method.




    "Mark [exmsft]" <MarkIngalls@nospam.nospam> wrote in message
    news:c3aa33fb1087ae8c8c5a46cbc7ac5@msnews.microsoft.com...
    > Hello Matt,
    >
    > Here's one way to achive this; it assumes you have the .NET 2.0 SDK
    > installed for the first step..
    >
    > 1. Using wsdl.exe (part of the .NET SDK), you can generate a c# or vb.net
    > proxy class for calling the webservice. You need to know the
    > name/location of the WSDL file that describes what methods the service
    > provides. I'll be using the dictionary service provided at
    > http://services.aonaware.com/DictSer...ctService.asmx.
    >
    > example:
    >
    > wsdl /outictService.cs
    > http://services.aonaware.com/DictSer...vice.asmx?WSDL
    >
    > 2. Using csc.exe, compile the source file into a library (dll) to use:
    >
    > csc /t:library DictService.cs
    >
    > 3. Load that library into PS (I do this in my profile):
    >
    > if ([System.IO.File]::Exists('c:\temp\DictService.dll')) {
    > [Reflection.Assembly]::LoadFrom("c:\temp\DictService.dll")
    > }
    >
    > (I check to see if the file exists because I use this profile on a few
    > machines and not all of them have the dll available on them).
    >
    > 3a. (optional) Create a function (again, I do this in my profile) to call
    > the method(s) on the webservice that you are interested in.
    >
    > $globalict = new-object DictService
    > function Def ($word)
    > {
    > $x = $Dict.Define($word)
    > for ($i = 0; $i -lt $x.Definitions.Count; $i++) {
    > write-host $x.Definitions[$i].Word
    > write-host $x.Definitions[$i].WordDefinition
    > }
    > }
    >
    >
    > If you already have the source code in c# or vb.net, you could just use
    > csc.exe or vbc.exe (which can be found in
    > $nev:windir\Microsoft.NET\Framework\v2.0.50727) to compile it and skip
    > step #1.
    >
    > thanks,
    > mark
    >
    >
    >> Hi,
    >>
    >> All the examples I've seen are where people download xml from a url:
    >> like abishek's weather function:
    >>
    >> $url="http://xml.weather.yahoo.com/forecastrss?p=10036"
    >> $web = new-object system.net.webclient
    >> $weather = [xml]$web.downloadstring($url)
    >> These are relatively simple. But is it possible to access soap apis?
    >> I'd like to access google or live search and those examples are only
    >> provided for vb.net and c# usually.
    >>
    >> If someone could point me in the right direction I'd appreciate it.
    >>

    >
    >




      My System SpecsSystem Spec

  4. #4


    mvgnyc@gmail.com Guest

    Re: Access web services through powershell

    Thanks for the help Mark. I was able to get the Google api working, so
    I'm just posting in case anyone is interested:

    I compiled the google wsdl into google.dll using your instructions.
    Then in Powershell:
    $k = [reflection.assembly]::loadfrom("c:\tools\googleapi\google.dll")
    $s = new-object googlesearchservice
    $r = new-object googlesearchresult
    $r = $s.doGoogleSearch("##GOOGLE KEY##", $search_string, 0, 10,
    $false, "", $false, "", "", "")

    foreach ($result in $r.resultelements) {
    write-host $result.title -foregroundcolor magenta
    write-host $result.snippet
    write-host $result.url -foregroundcolor gray
    write-host
    }

    Or, if there is a proxy add in:
    $proxy = new-object system.net.webproxy("##PROXY IP##",proxy port)
    $proxy.usedefaultcredentials = $true
    $s.proxy = $proxy


    I never got the microsoft live search working, here's my code though:

    [reflection.assembly]::loadfrom("c:\tools\msnsearch.dll")
    $s = new-object msnsearchservice
    $searchrequest = new-object searchrequest
    [sourcerequest[]]$sr2 = new-object sourcerequest
    $sr2[0].source = [sourcetype]::web
    $searchrequest.query = "web services"
    $searchrequest.requests = $sr2
    $searchrequest.appid = "##appi##"
    $searchresponse = new-object searchresponse
    $searchresponse = $s.search($searchrequest)

    But I always get:
    Exception calling "Search" with "1" argument(s): "Client Error"
    At line:1 char:28
    + $searchresponse = $s.search( <<<< $searchrequest)

    I'm not so concerned with this since Google works, but if anyone has
    any ideas please let me know.

    thanks again.


    On Oct 24, 6:56 pm, Mark [exmsft] <MarkInga...@nospam.nospam> wrote:
    > Hello Matt,
    >
    > Here's one way to achive this; it assumes you have the .NET 2.0 SDK installed
    > for the first step..
    >
    > 1. Using wsdl.exe (part of the .NET SDK), you can generate a c# or vb.net
    > proxy class for calling the webservice. You need to know the name/location
    > of the WSDL file that describes what methods the service provides. I'll
    > be using the dictionary service provided athttp://services.aonaware.com/DictService/DictService.asmx.
    >
    > example:
    >
    > wsdl /outictService.cshttp://services.aonaware.com/DictService/DictService.asmx?WSDL
    >
    > 2. Using csc.exe, compile the source file into a library (dll) to use:
    >
    > csc /t:library DictService.cs
    >
    > 3. Load that library into PS (I do this in my profile):
    >
    > if ([System.IO.File]::Exists('c:\temp\DictService.dll')) {
    > [Reflection.Assembly]::LoadFrom("c:\temp\DictService.dll")
    >
    > }(I check to see if the file exists because I use this profile on a few machines
    > and not all of them have the dll available on them).
    >
    > 3a. (optional) Create a function (again, I do this in my profile) to call
    > the method(s) on the webservice that you are interested in.
    >
    > $globalict = new-object DictService
    > function Def ($word)
    > {
    > $x = $Dict.Define($word)
    > for ($i = 0; $i -lt $x.Definitions.Count; $i++) {
    > write-host $x.Definitions[$i].Word
    > write-host $x.Definitions[$i].WordDefinition
    > }
    > }
    >
    > If you already have the source code in c# or vb.net, you could just use csc.exe
    > or vbc.exe (which can be found in $nev:windir\Microsoft.NET\Framework\v2.0.50727)
    > to compile it and skip step #1.
    >
    > thanks,
    > mark
    >
    > > Hi,

    >
    > > All the examples I've seen are where people download xml from a url:
    > > like abishek's weather function:

    >
    > > $url="http://xml.weather.yahoo.com/forecastrss?p=10036"
    > > $web = new-object system.net.webclient
    > > $weather = [xml]$web.downloadstring($url)
    > > These are relatively simple. But is it possible to access soap apis?
    > > I'd like to access google or live search and those examples are only
    > > provided for vb.net and c# usually.

    >
    > > If someone could point me in the right direction I'd appreciate it.



      My System SpecsSystem Spec

Access web services through powershell problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
calling Sharepoint services using powershell AdityaKir PowerShell 0 04 Aug 2008
Powershell and certificate services Marcela PowerShell 2 18 Jul 2008
PowerShell : Stopped Services with Autostart output required Dee PowerShell 2 19 Mar 2008
reporting services and powershell results Thuan PowerShell 5 21 Oct 2007
PowerShell access to AD user Terminal Services attributes John Yarborough PowerShell 2 13 Jul 2007