How to get format of SOAP parameters via PowerShell - web-services

I'm trying to consume a web service via PowerShell. This is what I have so far:-
$uri = https://customertest.rm-manifest.com/PosterUpload2/PosterUpload.svc?wsdl
$proxy = New-WebServiceProxy -uri $uri -credentials $cred
I can then get a list of methods and properties with Get-Member, but when I try to call a method I get 400 bad request. Probably because my arguments are not formatted properly.
$arg = New-Object -TypeName Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rUpload2_PosterUpload_svc_wsdl.RetrieveActiveSupplyChainsRequest
[xml]$response = $proxy.RetrieveActiveSupplyChains($arg)
I don't think I need to pass any data to the method in this example but I think it does expect a single object as a parameter.
Should I be able to use the wsdl to generate/format my argument object? If so how do I retrieve the information.
Thanks.

Related

Error generating embed token for editable Power BI Embedded dashboard

I am using a Powershell script to generate an embed token for a Power BI dashboard:
Login-PowerBI
$url = "https://api.powerbi.com/v1.0/myorg/groups/395ce617-f2b9-xyz/dashboards/084c9cc4-xyz/GenerateToken"
$body = "{ 'accessLevel': 'View' }"
$response = Invoke-PowerBIRestMethod -Url $url -Body $body -Method Post -ErrorAction "Stop"
$response
$json = $response | ConvertFrom-Json
$json.token
This works, however I was hoping to make the dashboard editable by changing the accessLebel like this:
$body = "{ 'accessLevel': 'Edit' }"
Instead of generating a token, an error is thrown indicating Bad Request, but with no other detail. How can I determine how the request should be created? Are dashboards even editable like reports are? (I can generate edit tokens for reports with no issue) I can't find a code sample for that, and I note the online sample doesn't allow you to edit dashboards like you are able to with reports: https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html
You got the error Bad request because accessLevel: Edit is not supported for dashboards.
The accessLevel supported for Generate EmbedToken for dashboard in the group is only View.
Create and Edit accessLevel is available only for reports.
Refer to this link: https://learn.microsoft.com/en-us/rest/api/power-bi/embedtoken/dashboards_generatetokeningroup#tokenaccesslevel
You can use the Try it feature there to see how the REST API calls are made.

SharePoint REST - Fetch list items w/ skiptoken gives HTTP 403

I am GETting a bunch of list items from a SharePoint Online site using a PowerShell script. I need to fetch all the items, using the
/_api/web/lists/getbytitle('MyList')/items
REST call.
I can call this endpoint just fine and receive the first 100 items. But when I call the URL for "skiptoken" which I received in the response, I get a HTTP 403 (Forbidden).
What am I doing wrong?
After struggling around I decided to resort back to using the CSOM and CAML query. This is not as flexible as the REST service and makes working on the result a little harder, but did the trick (code functional, but might be a good idea to split this up):
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user , $password)
$context.Credentials = $credentials
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = '...'
$web = $ctx.Web
$lists = $web.Lists
$list = $lists.GetById($listID)
$ctx.Load($lists)
$ctx.Load($list)
$ctx.ExecuteQuery()
$resultCollection = New-Object 'System.Collections.ArrayList'
do {
$items = $list.GetItems($query)
$ctx.Load($items)
$ctx.ExecuteQuery()
$items | ForEach-Object {
$item = #process item here
$resultCollection.Add($item) |Out-Null
}
$query.ListItemCollectionPosition = $items.ListItemCollectionPosition #advance cursor
} while ($query.ListItemCollectionPosition -ne $null)

Using PowerShell to send data from SQL Server to service via SOAP

I'm fairly new to PowerShell and brand new (as in, today) to web services and SOAP. A vendor gave us documentation on their web service API that allows the creation of user accounts. I'm trying to use PowerShell to pull our users from SQL Server and send the data to their service. We will need to add users on an ongoing basis.
Below is a pared-down version of what I came up with and it actually seems to work; the vendor told me to include a dry_run parameter while testing and I'm getting a dry_run_success from the response_type.
My question is: Is this even close to being the appropriate way to do it with PowerShell?
# Open ADO.NET Connection to database
$dbConn = New-Object Data.SqlClient.SqlConnection;
$dbConn.ConnectionString = "Data Source=mydbserver;User ID=someuserid;Password=mypassword;Initial Catalog=mydatabase";
$dbConn.Open();
$sql = "select * from mytable";
$dbSqlCmd = New-Object Data.SqlClient.SqlCommand $sql, $dbConn;
$dbRd = $dbSqlCmd.ExecuteReader();
# Create a Web Service Proxy
$proxy = New-WebServiceProxy -Uri https://somedomain.com/service/wsdl
$namespace = $proxy.GetType().NameSpace
$param = New-Object($namespace + ".somemethod")
# Loop through records from SQL and invoke the web service
While ($dbRd.Read())
{
$param.user_id = $dbRd.GetString(0)
$param.password = $dbRd.GetString(1)
$param.display_name = $dbRd.GetString(2)
$request = $proxy.TheMethod($param)
if ($request.response_type -eq 'error')
{
$request.error.extended_error_text
}
}
# Clean up
$dbRd.Close();
$dbSqlCmd.Dispose();
$dbConn.Close();
A couple things you could improve:
Don't use select * in your SQL queries. Always specify the fields you need, in the order you need. As written, if someone were to restructure the table such that the user ID wasn't the first column, you'd have a mess on your hands because you're accessing the fields by their ordinal number
You're apparently storing those passwords in plaintext in your database. Anyone with access to your database knows the credentials for every one of your users. This is a very bad thing. Resolving this could be a very big discussion.
Your code keeps the database connection open until the script completes. Given the scope here, it's probably not going to cause a major problem, but your database access strategy should be to get in, get your data, get out & disconnect as quickly as possible.
$sql = "select user_id, password, display_name from mytable";
$QueryCmd = $dbConn();
$QueryCmd.CommandText = $sql;
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter;
$QueryCmd.Connection = $dbConn;
$SqlAdapter.SelectCommand = $QueryCmd;
$DataSet = New-Object System.Data.DataSet;
$SqlAdapter.Fill($DataSet)
$dbConn.Close();
$dbConn.Dispose();
$MyResults = $DataSet.Tables[0];
$MyResults | foreach-object {
$param.user_id = $_.user_id;
$param.password = $_.password;
$param.display_name = $_.display_name;
$request = $proxy.TheMethod($param);
if ($request.response_type -eq 'error')
{
$request.error.extended_error_text;
}
}

Is it possible to send additional HTTP headers to web services via New-WebServiceProxy

A web service I need to interact with (often manually for testing) requires an extra HTTP header on certain requests. Quick manual testing works quite great with PowerShell's New-WebServiceProxy but so far I haven't found an option to add another HTTP header to the request.
Is there something for that?
Invoke-WebRequest http://yourURLhere -Headers #{"accept"="application/json"}
Introduced in POSH 3.0
You could use .NET's web client from PowerShell.
> $webClient = New-Object System.Net.WebClient
> $webClient.Headers.add('accept','application/json')
> $webClient.DownloadString('http://yourURLhere')
I am surprised this hasn't come up:
$uri = 'http://...'
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Accept','Application/Json')
$headers.Add('X-My-Header','...')
$result = Invoke-WebRequest -Uri $uri -Headers $headers
For completeness, a reference to the headers property:
https://msdn.microsoft.com/en-us/library/s4ys34ea(v=vs.110).aspx
One more easy way to send Authorization and perform Get or Delete request in Loop
$orders=#("522536","522539")
foreach ($order in $orders){
iwr "https://api.taxjar.com/v2/transactions/orders/$order" `
-Method 'DELETE'`
-Headers #{ 'Authorization' ='Token token="34625gdsgd74rghdg4"'}
}

Access web service in PERL

I am accessing web service which is hosted in IIS from perl script. I have a methos in service which returns an array of string. I am not able to read the response from service. By using Dumper i printed the response returned by service, and there I can see the array values but I am not able to access the array values. How to access thr array values returned form web service method.
Calling of method in perl:
my $method2 = SOAP::Data->name('getCustInfo')->attr({xmlns => 'http://tempuri.org/'});
my #param=(SOAP::Data->name(custId=>$custid));
my $response1= $soap->call($method2=>#param);
print $response1;
print Dumper $response1;
#result11=$response1->result;
print Dumper $response1;
$i=-1;
foreach my $result(#result11)
{
++$i;
print $result[$i];
}
The above code I am using to access the method, and I am trying to print it but it not working its giving: HASH(0x3a84518)$VAR1 = undef; What is the problem.
Thanks,
Avinash
Before knowing the response check whether the call happens.
use strict;
use warnings;
use SOAP::Lite +trace=>"debug"; # it debugs whether the connection is set or not
my ($soap,$proxy,$uri);
eval {
$soap = new SOAP::Lite
proxy=>$proxy,
uri=>$uri;
};
if ( $# ){
print " Service Down\n";
}
For the soap call, it requires the proxy and the uri kindly have those in the eval to check whether they are accessible.