Powershell consuming webservice - missing return value - web-services

Consuming a simple webservice using powershell. It is supposed to return 2 values, but powershell only shows one. SOAPUI works fine, shows two values. If I don't pass as [ref] the call errors. Not sure why this is?
$URL = "https://nonpublic/wsdl/location"
$URI = New-Object System.Uri($URL);
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$webproxy = New-WebServiceProxy -Uri $URI -Namespace webproxy-class ssl
## webservice to return two values related to passed value
$val = "ide-550"
$result = $webproxy.getinfo([ref]$val)
$result ## <-- only one value
EDIT: get member output - statustype getinfo(string [ref] val).

Related

RegEx Finding multiple matches with a quantifier

I have this PowerShell code:
$uri = "http://charts.spotify.com/api/tracks/most_streamed/au/daily/latest"
$ContentType = "application/json"
$postblog = (Invoke-WebRequest -Uri $uri).Content -match 'track_name\S:\S(.*?)",'
$matches[1]
When I run this, I get this result:
FourFiveSeconds
Problem is, I know there are more songs than just this one song. And I know that the match I am using, the string of text "track_name" exists more than once. How can I change my RegEx so that it matches every match it can find? In other words, the expected output would be multiple matches, allowing me to list all the songs, e.g. $matches[1], $matches[2], $matches[3], $matches[4], etc.
Since you are using Invoke-WebRequest, I assume you are using Powershell v4.0. Therefore, you can use ConvertFrom-Json on the data received and iterate over it, instead of using a regex solution:
$uri = "http://charts.spotify.com/api/tracks/most_streamed/au/daily/latest"
$ContentType = "application/json"
$postblog = (Invoke-WebRequest -Uri $uri).Content | ConvertFrom-Json
Now, the entire tracks data is available inside $postblog.tracks array.
Iterate over them to get the track_urls:
Foreach( $track in $postblog.tracks ) {
Write-Output $track.track_url
}
EDIT
Apparently, you can simply use:
Write-Output $postblog.tracks.track_url
instead of the Foreach code-block. Thanks to #PetSerAl for that :)
Thanks to all, yes you are right, converting it to JSON gives me heaps more options and it is cleaner than using RegEx.
I have this script now, which should definitely do the trick.
$spotifytopsongs = #()
$uri = "http://charts.spotify.com/api/tracks/most_streamed/au/daily/latest"
$ContentType = "application/json"
$spotifyjson = (Invoke-WebRequest -Uri $uri).Content | ConvertFrom-Json
$spotifyjson.tracks | select -First 50 | % {$spotifytopsongs += #($_.artist_name + " - " + $_.track_name)}
cls;$spotifytopsongs

Perl: can not post xml data to web service

The web service accepts the xml data and returns values back in xml again. I am trying to post the xml data to the web services, without any success, I need to do it using Perl. Following is the code I tried:
use SOAP::Lite ;
my $URL = "http://webservice.com:7011/webServices/HealthService.jws?WSDL=";
my $xml_data = '<Request>HealthCheck</Request>' ;
my $result = SOAP::Lite -> service($xml_data);
print $result ;
I tried another approach with proxy:
use SOAP::Lite +trace => 'debug';
my $URI = 'webServices/HealthService' ;
my $URL = "http://webservice.com:7011/webServices/HealthService.jws?WSDL=" ;
my $test = SOAP::Lite -> uri($URI)
-> proxy($URL) ;
my $xml_data = '<Request>HealthCheck</Request>' ;
my $result = $test -> healthRequest($xml_data);
print $result ;
However this is throwing the following error:
Can't locate class method "http://webservice.com:7011/healthRequest" via package "SOAP::Lite\" at 7.pl line 4. BEGIN failed--compilation aborted at 7.pl line 4.
The webservice provides only one method HealthRequest. I am not sure why it is trying to find out the class method in SOAP:Lite. I get the same error for both the approach.
Is there any other method to achieve the same using Perl?
Try something like this, I have not tested it so just test and see what happens, you should at least not get the PM error.
use strict;
use SOAP::Lite;
my $xml_data = '<Request>HealthCheck</Request>' ;
my $soap = SOAP::Lite
->uri("webServices/HealthService")
->proxy("http://webservice.com:7011/webServices/HealthService.jws?WSDL=");
print $soap->service($xml_data),"\n";
If you want to create the XML yourself and not delegate that task to SOAP::Lite, you need to let SOAP::Lite know what you are doing:
$soap = SOAP::Lite->ns( $URI )->proxy( $URL );
$soap->HealthCheck( SOAP::Data->type( xml => $xml_data ) );
I have my doubts, though, that this will work with your XML.
If your request really has no variable parameters, this may work:
$soap = SOAP::Lite->ns( $URI )->proxy( $URL );
$soap->HealthCheck;
PS: Are your sure that your webservice is a SOAP service?

i not find how in powershell pass through http autentification then use a webservices (lotus/domino)

We have here a domino/lotus webservices i want use with powershell.
probleme is in front of webservices lotus admin ask a http autentification.
how i can use this webservice??
here what i tryed first scrap the first page and get cookie.
$url = "http://xxxxxxx/names.nsf?Login"
$CookieContainer = New-Object System.Net.CookieContainer
$postData = "Username=user***&Password=pss***"
$buffer = [text.encoding]::ascii.getbytes($postData)
[net.httpWebRequest] $req = [net.webRequest]::create($url)
$req.method = "POST"
$req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
$req.Headers.Add("Accept-Language: en-US")
$req.Headers.Add("Accept-Encoding: gzip,deflate")
$req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7")
$req.AllowAutoRedirect = $false
$req.ContentType = "application/x-www-form-urlencoded"
$req.ContentLength = $buffer.length
$req.TimeOut = 50000
$req.KeepAlive = $true
$req.Headers.Add("Keep-Alive: 300");
$req.CookieContainer = $CookieContainer
$reqst = $req.getRequestStream()
$reqst.write($buffer, 0, $buffer.length)
$reqst.flush()
$reqst.close()
[net.httpWebResponse] $res = $req.getResponse()
$resst = $res.getResponseStream()
$sr = new-object IO.StreamReader($resst)
$result = $sr.ReadToEnd()
this seem work but now no idea how i can use cookie with a webservicesproxy???
or maybee a better way to do it?
ps: i success have this to work with c# + visualstudio (just the class reference is autobuilt and i don't understand half of this but it allow me to use .CookieContenaire on the generated webservice )
Have you tried to use the built in webservice cmdlet?
$webservice = New-WebServiceProxy -Uri "http://xxx/names.nsf?Login" -Credential (Get-Credential)

Powershell Webservice Method definition changes

I am using powershell to deploy our SSRS reports, but have come across an issue when deploying multiple reports.
$uri = "http:///Reportserver2008/reportservice2005.asmx"
$Proxy = New-WebServiceProxy -Uri $uri -Namespace SSRS.ReportingService2005 -UseDefaultCredential ;
$Proxy | gm "SetItemDataSources"
Which returns a method definition of:
System.Void SetItemDataSources(string Item, SSRS.ReportingService2005.DataSource[] DataSources)
If I duplicate the code above, the method definition changes the second time it is requested
e.g
$uri = "http:///Reportserver2008/reportservice2005.asmx"
$Proxy = New-WebServiceProxy -Uri $uri -Namespace SSRS.ReportingService2005 -UseDefaultCredential ;
$Proxy | gm "SetItemDataSources"
$Proxy = New-WebServiceProxy -Uri $uri -Namespace SSRS.ReportingService2005 -UseDefaultCredential ;
$Proxy | gm "SetItemDataSources"
Returns two different method definitions:
System.Void SetItemDataSources(string Item, SSRS.ReportingService2005.DataSource[] DataSources)
System.Void SetItemDataSources(string Item, SSRS.ReportingService2005.DataSource[], 0juuvurk, Ve...
Can anyone explain why the definition changes???
I have tried disposing $proxy after first request, Uri does not change
I am thinking I may have to pull out $proxy and only assign it once.
Any insight greatly appreciated!
You're right on with your instincts. Creating 2nd or 3rd proxies of web services can cause trouble, because the proxies get put into automatically generated namespaces. So proxying twice actually redoes a lot of work, and creates two very similar looking types in memory.
There are generally two ways to work with this sort of issue:
Use the -Namespace parameter to force the object into a namespace.
Use $proxy.GetType().Namespace to find the automatically generated base type
Hope this helps

Powershell, web services and object types

I'm new to using web services under powershell, so maybe I have a basic misunderstanding about something. I'm working with Microsoft's Reporting Services. Here is a repro script.
$computer = "rptdev"
$uri = "http://$($computer)/ReportServer/ReportService.asmx?WSDL"
$reporting = New-WebServiceProxy -uri $uri -UseDefaultCredential -namespace "ReportingWebService"
$dsRef = new-object ReportingWebService.DataSourceReference
$ds = new-object ReportingWebService.DataSource
$dsRef.GetType()
$ds.GetType()
If I run that, I get something that looks more or less like this:
Name BaseType
---- --------
DataSourceReference ReportingWebService.DataSourceDefinitionOrReference
DataSource System.Object
So, my question is: Why does DataSource have System.Object as a BaseType when DataSourceReference clearly has a object type that is based on the web object? They were both created from the ReportingWebService namespace, weren't they?
My root problem is that I need to hand an array of DataSources back to SetItemDataSources, and SetItemDataSources chokes on an array of System.Objects, and I don't seem to be able to cast it to what I want.
All this means is that the "DataSource" class inherits directly from System.Object. Whereas "DataSourceReference" inherits from "DataSourceDefinitionOrReference", then maybe something else, then System.Object.
However, I do not think that is your problem. Your problem is probably PowerShell's automatic splitting and recombining of collections as generic collections of System.Object. You can control this by setting a static type on the collection like so (I'm guessing on this API you are using since I haven't used it myself):
$computer = "rptdev"
$uri = "http://$($computer)/ReportServer/ReportService.asmx?WSDL"
$reporting = New-WebServiceProxy -uri $uri -UseDefaultCredential -namespace "ReportingWebService"
[ReportingWebService.DataSource[]]$DataSources = <do something to get your data sources>
$reporting.SetItemDataSources($DataSources)
If you only have a single object and you want to pass an array of objects (i.e. an array with a single element in it - your sole object), you use the #() syntax:
ps> $o = new-object mynamespace.myobj
ps> $thing.Method( #($o) )
-Oisin