Browsed by
Month: February 2017

Send an email with GraphAPI and PowerShell

Send an email with GraphAPI and PowerShell

To follow on from this blog post where we interrogated the GraphAPI to obtain information regarding the user object which invoked the call. We are going to use GraphAPI and PowerShell to Send an email, I know what you’re say, “But Steve I can just use the Send-MailMessage commandlet” which is true, but what the this solution gets you is a record in your Sent Items of the email. All of this without even needing to think about setting up anything to do with SMTP.
So the major difference from the last script to this one is that we need to construct a JSON object, which we then POST to GraphAPI, rather then using a GET function like we did previously. An example of this is:

In my sample I have kept it simple with a single recipient, a Subject, and body. But there is a vast amount of information which you can define for the email your sending, you can find more about it here.
For a full copy of script please look here

Good Luck

Steve

PowerShell GraphAPI

PowerShell GraphAPI

In the last blog post we went over what the GraphAPI is and why it’s all powerful, with this post we are going to go over how you can leverage the GraphAPI via PowerShell.
First things first we need to register an application at https://apps.dev.microsoft.com/ this is pretty straight forward, just make sure you save the Client Secret when you create it as you will only see it once! the other thing to note is that you need add a platform as “Web” this address does not need to resolve, nor does your PowerShell Scripts need to use it.
The other important part on this page is to allocate permissions to the application which all depends upon what you want to interrogate the GraphAPI, for more information have a look here where Microsoft has documented the process in more detail then I have.

So we now have the Application ID from the new application, the Client Secret, and the Redirect Address, we can now create a simple PowerShell script like the below (update the Variables to match your environment).

Once you have updated the Script to match your environment you can now execute it and you should see something like this:

@odata.context : https://graph.microsoft.com/beta/$metadata#users/$entity
id : 2f2916e5-953c-4c5d-9f52-bd5db8131d49
accountEnabled : True
assignedLicenses : {@{disabledPlans=System.Object[]; skuId=c7df2760-2c81-4ef7-b578-5b5392b571df}, @{disabledPlans=System.Object[]; skuId=061f9ace-7d42-4136-88ac-31dc755f143f}}
assignedPlans : {@{assignedDateTime=2017-01-18T19:43:48Z; capabilityStatus=Enabled; service=SharePoint; servicePlanId=e95bec33-7c88-4a70-8e19-b10bd9d0c014}, @{assignedDateTime=2017-01-18T19:43:48Z; capabilityStatus=Enabled;
service=SharePoint; servicePlanId=5dbe027f-2339-4123-9542-606e4d348a72}, @{assignedDateTime=2017-01-18T19:43:48Z; capabilityStatus=Enabled; service=exchange; servicePlanId=efb87545-963c-4e0d-99df-69c6916d9eb0},
@{assignedDateTime=2017-01-18T19:43:48Z; capabilityStatus=Enabled; service=MicrosoftCommunicationsOnline; servicePlanId=0feaeb32-d00e-4d66-bd5a-43b5b83db82c}…}
businessPhones : {}
city :
companyName :
country :
department :
displayName : Steven Hosking
givenName : Steven
jobTitle :
mail : mail@AusIgnite2017.onmicrosoft.com
mailNickname : steve
mobilePhone :
onPremisesImmutableId :
onPremisesLastSyncDateTime :
onPremisesSecurityIdentifier :
onPremisesSyncEnabled :
passwordPolicies :
passwordProfile :
officeLocation :
postalCode :
preferredLanguage : en-AU
provisionedPlans : {@{capabilityStatus=Enabled; provisioningStatus=Success; service=exchange}, @{capabilityStatus=Enabled; provisioningStatus=Success; service=exchange}, @{capabilityStatus=Enabled; provisioningStatus=Success;
service=exchange}, @{capabilityStatus=Enabled; provisioningStatus=Success; service=exchange}…}
proxyAddresses : {SMTP:mail@AusIgnite2017.onmicrosoft.com}
refreshTokensValidFromDateTime : 2017-01-18T07:01:55Z
showInAddressList :
state :
streetAddress :
surname : Hosking
usageLocation : AU
userPrincipalName : mail@AusIgnite2017.onmicrosoft.com
userType : Member

If you want to test other options you can change the URL on Line 2 of the script “$uri = “https://graph.microsoft.com/beta/me/”” to query anything which has been published via the GraphAPI, you can find more information here and browsing through the options on the left of the page.

You can also find all of the references of the Beta calls for the GraphAPI which at the moment includes all of the calls for Intune, these might not work on your subscription today, these GraphAPI calls will be green lit for your subscription once it has been migrated over to the Ibiza portal (portal.azure.com).

Good Luck

Steve