Parsing PowerBIRestMethod response to CSV - powershell

I've been working on a way to retrieve our PowerBI data, and managed to get exactly all the data I'd need to process, however I can't seem to figure out on how to actually parse the data to a proper CSV.
I use the invoke command to get user access to a certain dataset with the below command in PowerShell
Invoke-PowerBIRestMethod -Url 'https://api.powerbi.com/v1.0/myorg/admin/datasets/DATASETID/users' -Method Get
This then comes back with the below response:
{
"#odata.context":"http://wabi-north-europe-j-primary-redirect.analysis.windows.net/v1.0/myorg/admin/$metadata#Collection(Microsoft.PowerBI.ServiceContracts.Api.
Access.DatasetUser)","value":[
{
"datasetUserAccessRight":"ReadWriteReshareExplore","emailAddress":"hiddenemail","displayName":"hiddenname","identifier":"hiddenidentifier","graphId":"hiddengraphid","principalType":"User","userType":"Member"
},{
"datasetUserAccessRight":"Read","emailAddress":"hiddenemail","displayName":"hiddenname","identifi
er":"hiddenidentifier","graphId":"hiddengraphid","principalType":"Group"
}
]
}
As shown above it came back with 2 entries of permissions on that dataset, one being a user and the other a group.
I want to parse/export this to a simple CSV file containing the below information:
User rights
Email address
Displayname
Identifier
Principal Type
How would one achieve this? I've tried several ConvertFrom utilities in PowerShell, but those doesn't seem to work correctly. Is there any easy way to get this to export correctly?
Thank you in advance for any suggestions/advice!

Related

Send log with customDimensions to Application Insights using Powershell

I want to send logs to my Application Insights using PowerShell script and I want it to contain customDimensions value.
I tried something like this:
$client = [Microsoft.ApplicationInsights.TelemetryClient]::new()
$client.InstrumentationKey = "xxxx"
$client.TrackTrace("MY CUSTOM LOG", "Information", #{foo="bar"})
$Client.Flush()
but it gives me an error:
Cannot find an overload for "TrackTrace" and the argument count: "3".
Ideally I want the code above to work so that I can read the log like this:
I know how to do it using Python and opencensus-ext-azure lib but now I also need Powershell as well.

What is the best way to export large nested objects in Powershell for later testing/mocking? Particularly Http and Web Exceptions

I am writing a script that heavily utilizes an API. I have a large custom error handling function for the various error responses from Invoke-RestMethod calls that I would like to be able to unit test appropriately.
I saved some of these errors by using Export-CliXml, however I'm not actually able to rebuild the object back to it's original form. At some point when I'm expecting data I just get strings, and when I increase the -Depth parameter for Export-Clixml, Powershell takes a huge amount of time to write a gigantic .xml file.
For example I get an Http Response Error from Invoke-RestMethod, I check the status code and use that for some custom error handling in some of the scenarios. This is grabbed with $ErrorRecord.Exception.Response.StatusCode.Value__. However when I try to rebuild the CliXml object, everything held in the nested 'Response' object turns into just a string and I can't parse it in the same way.
So my questions are:
Is there a good way to export a complicated, nested, error record object that doesn't take a huge amount of time to export and import?
How is it that powershell gets the response and creates this error record in miliseconds, but my exporting and importing of the same object with it's nested properties takes an enormous amount of time?
Public testing API that will just return a 401 unauthorized error:
try {
$global:response = Invoke-RestMethod -Uri "https://reqres.in/api/login" -Method 'POST'
}
catch {
$global:err = $_
}
ConvertTo-Json is a quick way to capture the exception and its nested properties. Specify -Depth to navigate through the object tree more than the default 2 layers:
$global:err = $_ | ConvertTo-Json -Depth 3
You can then deserialize it back to an object with ConvertFrom-Json and access the properties:
($Global:err | ConvertFrom-Json).ErrorDetails
If you include the code that's running slowly it will be easier to isolate its issue, but I expect that you might be encountering an issue with filesystem access rather than code. You can use Measure-Command to isolate issues with slow-running code.
EDIT: It looks like you were on the right track with ExportCli-Xml -- the built-in JSON serializer might not represent an object as well as CliXml. I missed the requirement that you needed to deserialize back to an Error object.
You might try skipping the Export and just use Management.Automation.PSSerializer. This code didn't raise any errors and was very quick:
$DeserializedError = [Management.Automation.PSSerializer]::DeSerialize([Management.Automation.PSSerializer]::Serialize($Error[0]))

how to get dictionary value from webrequest using sharepoint designer

I am trying to retrieve a value from a HTTP web service call in sharepoint designer. This should be simple. the Rest query is simple, and always returns only a single value:
https://Site.sharepoint.com/sites/aSiteName/_api/web/lists/getByTitle('MyListTitle')/items/?$select=Title&$top=1
In the Sharepoint Designer workflow, I'm setting the required Accept and Content-type header to the value of "application/json;odata=verbose
I am unable to get the value of the "Title" field that is returned by the call.
when I execute the REST query in the browser, I get the following data returned:
{"d":{"results":[{"__metadata":{"id":"af9697fe-9340-4bb5-9c75-e43e1fe20d30","uri":"https://site.sharepoint.com/sites/aSiteName/_api/Web/Lists(guid'6228d484-4250-455c-904d-6b7096fee573')/Items(5)","etag":"\"1\"","type":"SP.Data.MyListName"},"Title":"John Doe"}]}}
I've tried dozens of variations of the dictionary 'query', but they always return blank.
I'm using the 'get an item from a dictionary' action in SP Designer, using item name or path values like:
d/results(0)/Title
d/Title
d/results/Title
and literally dozens of other variations - but it always returns blank.
I'm writing the raw response from the webRequest to the list for debugging, and it shows the value like this:
{"odata.metadata":"https:\/\/site.sharepoint.com\/sites\/aSiteName\/_api\/$metadata#SP.ListData.MyListTitle&$select=Title","value":[{"odata.type":"SP.Data.MyListTitle","odata.id":"616ed0ed-ef1d-405b-8ea5-2682d9662b0a","odata.etag":"\"1\"","odata.editLink":"Web\/Lists(guid'6228d484-4250-455c-904d-6b7096fee573')\/Items(5)","Title":"John Doe"}]}
I must be doing something simple that is wrong?
Using "d/results(0)/Title" is right. Check the steps in article below to create a workflow.
CALLING THE SHAREPOINT 2013 REST API FROM A SHAREPOINT DESIGNER WORKFLOW
It working fine in my test workflow.
I faced the exact same issue. In my case the reason was that in the API call, the header was not set properly.
As you would have noticed many times, that if you type the variables inline when creating the "Call Http Web service" action, those might not get set properly. The surest way is to open the properties and set from there. In my case when i opened the properties i found that RequestHeaders was not set. Once i set it from there, i got the desired results.
Hope this helps to someone in future, this question being unanswered till now!!
tried dump those json called from sharepoint workflow into a list. Sometimes you'll get a different format than when you called that from browser. I experienced this issue when calling API projectserver (project online). When I called it using servistate (chrome extension) it returns d/results, but when I dump the value into the list I got value and yet I used same request header value.

Creating Azure Stream Analytics Job through Powershell

I would like to create a stream analytics job using only Powershell. I know that the command to do this is: New-AzureRMStreamAnalyticsInput. However it requires a JSON file with job details. I found a documentation provided by Microsoft where there is a small template of such a JSON file (check Create paragraph). However it's not enough for me.
I want to create an input from blob storage hence my JSON looks like this:
{
"properties":{
"type":"stream",
"datasource":{
"type":"Microsoft.Storage/Blob",
"properties":{
"accountName":"abc",
"accountKey":"###",
"container":"appinsights",
"pathPattern":"test-blob_2324jklj/PageViews/{date}/{time}",
"dateFormat":"YYYY-MM-DD",
"timeFormat":"HH"
}
}
}
}
After saving it and passing as an argument in New-AzureRMStreamAnalyticsInput I receive following error: New-AzureRMStreamAnalyticsInput : Stream analytics input name cannot be null. I think that my JSON file is not correct.
Do you have any templates of json files containing stream analytics job details or can you just tell me how to correctly set up a job through powershell?
A simple way of getting your template right is to manually create an input from Portal and then run PowerShell command Get-AzureRmStreamAnalyticsInput to get the JSON payload.
From your example, it seems you missed the input name. Try something like below:
{
"Name": "BlobInput1",
"Properties": {
... ...
}
}

Referring to Dumped LinkedIn API data

Quick problem here that I don't know how to solve and thought you guys could give me a heads-up on which way to go. I have successfully pulled all my connection data using the LinkedIn REST API both in XML and JSON and dumped them (the former using the cPickle plugin). The issue is that I need to refer to a single field within the data and so decided to use XML as it seemed to be the easiest one to use by far. When I refer to the specific field in my .pickle file, it gives me the following error:
AttributeError: 'str' object has no attribute 'location'
However, opening the pickle file with notepad, I can see that all my connections do have their location field stored in the XML format. It's very strange!
Here's my referral code:
import cPickle
connections_data = 'linkedin_connections.pickle'
response = cPickle.load(open(connections_data))
print response
locations = [ec.location for ec in response]
I have a print function set up to show what's in my file and all of the data appears as a normal XML output using the people call of the REST API. The XML data appears as follows:
<person>
<id>ID_number</id>
<first-name>blah</first-name>
<last-name>blah</last-name>
<headline>Business Development and Sales Executive at Computaris</headline>
<picture-url>picture_url</picture-url>
<api-standard-profile-request>
<url>profile_request</url>
<headers total="1">
<http-header>
<name>x-li-auth-token</name>
<value>name</value>
</http-header>
</headers>
</api-standard-profile-request>
<site-standard-profile-request>
<url>request_url</url>
</site-standard-profile-request>
<location>
<name>location</name>
<country>
<code>country_code</code>
</country>
</location>
<industry>industry</industry>
Any help will be much appreciated.