Office 365

Create Test Microsoft O365 Group and Delete it

Posted on Updated on

If you are involved in rolling-out of Microsoft Teams or groups recently, then you may need a test script to create temporary O365 Groups
Using the below PowerShell script, you will be able to do so

Pre-requisites:
——————
You should have :

  1. Client Id
  2. Client Secret
  3. tenant admin Url & Credentials

function Test-Group {
##Get Access Token
$body = @{
scope="https://graph.microsoft.com/.default";
client_id=$AppID;
client_secret=$AppSecret;
grant_type="client_credentials";
}
$accessToken = (Invoke-RestMethod `
-Uri https://login.microsoftonline.com/$AADDomain/oauth2/v2.0/token `
-Method POST `
-Body $body `
-Headers @{"Accept" ="application/json"; "Content-Type"="application/x-www-form-urlencoded"}).access_token
##Create Group
$body = @{
displayName="$prefix$suffix";
description="$prefix$suffix";
groupTypes= @("Unified");
mailEnabled="true";
mailNickname="$prefix$suffix";
securityEnabled="false";
visibility="Private";
#"owners@odata.bind"=@("https://graph.microsoft.com/v1.0/users/5a97df74-a0c9-4fb9-8f9f-26bd29d4bf06");
}| ConvertTo-Json
$NewGroup = (Invoke-WebRequest `
-Uri https://graph.microsoft.com/v1.0/groups `
-Method POST `
-Body $body `
-Headers @{"Authorization" = "Bearer $accessToken"; "Accept" ="application/json"; "Content-Type"="application/json"} )
$NewGroupId = ($NewGroup.Content | ConvertFrom-Json).Id
##Check if group was provisioned
$success = $false
$time = [System.Diagnostics.Stopwatch]::StartNew()
for ($i=0; $i -lt 60; $i+=5) {
Start-Sleep -s 5
try{
Invoke-WebRequest `
-Uri https://graph.microsoft.com/v1.0/groups/$NewGroupId/drive `
-Headers @{"Authorization" = "Bearer $accessToken"; "Accept" ="application/json";} | Out-Null
$success = $true
break
}catch [System.Net.WebException]{
If ($_.Exception.Response.StatusCode.Value__ -ne 404){
throw
}
}
}
if($success){
##Delete Group, you can leave it or keep it based on your needs
# Invoke-RestMethod `
# -Uri https://graph.microsoft.com/v1.0/groups/$NewGroupId `
# -Method Delete `
# -Headers @{"Authorization" = "Bearer $accessToken"; "Accept" ="application/json";} | Out-Null
} else {
Write-Host ModernGroup failed to provision in ([Math]::floor($time.Elapsed.TotalSeconds)) seconds -ForegroundColor Red
Write-Host GroupId: $NewGroupId -ForegroundColor Red
Write-Host CorrelationId: $NewGroup.Headers.'request-id' -ForegroundColor Red
Write-Host Date: $NewGroup.Headers.'Date' -ForegroundColor Red
}
}
$AppID = 'xxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx' #Put your AppId
$AppSecret = '**********************************' #Put your Secret
$AADDomain = 'yourtenant.onmicrosoft.com'
for ($i=0; $i -lt 1; $i++) {
$suffix = (Get-Date).Ticks
$prefix = "MSFT_Test_"
Test-Group
}

At the End you will have a test group created

Advertisements

Apply SharePoint Online template using PnP PowerShell

Posted on

In this article we will define a template, and apply it for other SPO sites using PnP library
Please follow these steps :

    1. Set-up PnP module latest version from here https://github.com/SharePoint/PnP-PowerShell/releases
    2. Go to your SPO tenant, and create a SPO Site, you can also define (List structure, Theme, Pages, Content Types) because all of those will be part of the template.
    3. Make sure that the app catalog is activated on the tenant (There is no relation between app catalog and the tenant, but there is a bug found in January PnP package that required App catalog to be activated or a failure occurred)
    4. After having the site ready, run the below PowerShell command:
      $Creds = Get-Credential
      $SiteURL = "https://test.sharepoint.com/sites/demo1/"
      Connect-PnPOnline -Url $SiteURL -Credentials $Creds
      Get-PnPProvisioningTemplate -Out C:\temp\demo1Template.xml
    5. You should see a progress running for a while to export all settings.
    6. Please open the XML file at the provisioned path (C:\temp\demo1Template.xml), and have a look, it is also doable to do medications through the file, by adding items, or fields, but you need to be careful not to crap the schema.
    7. Now it is time to import it to the destination site, Create a destination site on SPO with a default teamsite template
    8. Apply the new template by running the below command.
      $DestUrl = "https://test.sharepoint.com/sites/destination1/"
      Connect-PnPOnline -Url $DestUrl -Credentials $Creds
      $template = Load-PnPProvisioningTemplate -Path "C:\temp\demo1Template.xml"
      Apply-PnPProvisioningTemplate -InputInstance $template

After this, refresh the destination site, and you should see the template applied.
I suggest to start first with a simple template, then apply more complexity to it. this approach should run smoothly also with SP2016, but I haven’t tried to import a template from SPO and import it to 2016 or vise versa

Retrieving Office 365 message center using PowerShell

Posted on Updated on

How to Retrieving Office 365 message center Information using PowerShell

Quick handy script to do that, and you can take the output to write it in CSV file or import it in SharePoint List, or store it in any other repository

1. You need to download Office 365 Service Communications PowerShell Module from Get Hub
https://github.com/mattmcnabb/O365ServiceCommunications

2. Download the library, and un-zip the file
3. Now you will need to relocate the module files, you should find it in this paths
C:\Users\Administrator\Downloads\O365ServiceCommunications-master\O365ServiceCommunications-master\O365ServiceCommunications


Import-Module C:\Users\Administrator\Downloads\O365ServiceCommunications-master\O365ServiceCommunications-master\O365ServiceCommunications
# Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files (x86)\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files (x86)\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
# Specify tenant admin"
$User = "tenant.admin@company.onmicrosoft.com"
$Pass = "password"
$cred = New-Object System.Management.Automation.PSCredential($User,(ConvertTo-SecureString $Pass -AsPlainText -Force));
# gather events from the Service Communications API
$MySession = New-SCSession -Credential $cred
$Events = Get-SCEvent -EventTypes Message -PastDays 100 -SCSession $MySession |
Select-Object Id, Title, StartTime, Service, EventType, Status, @{n='Message';e={$_.messages[0].messagetext}}
foreach ($Event in $Events)
{
$Event.Id
$Event.Title
$Event.StartTime
$Event.Message
$Event.Service
$Event.EventType
$Event.Status
}
Write-Host "Completed!"

PowerShell for Show documents in Delve and in the Discover view in OneDrive for Business

Posted on Updated on

Delve is relatively still young baby for Microsoft.

Recently we have a requirement to enable the option of Show documents in Delve and in the Discover view in OneDrive for Business.

This can be done easily via a GUI like below

Go to App lancher and pick Delve –> Select Feature settings –> then enable Show documents in Delve and in the Discover view in OneDrive for Business

 

to do this via script, please copy and paste this in your PowerShell ISE and run it.

Pre-requistes

  1. Windows Internet Explorer
  2. You have to login at least 1 time through portal.office.com because the script will use your existing credentials


#Deactivate Delve Simulator
$ie = New-Object -COMObject InternetExplorer.Application
$jsonlink = "https://eur.delve.office.com/"
try{
#You can replace your the variable below with a static name, but her it logs with your current windows credentials
$searcher = [adsisearcher]"(samaccountname=$env:USERNAME)"
$mail = $searcher.FindOne().Properties.mail
$userName =$mail.replace("@","_").replace(".","_")
#Navigate to your download file/location
Write-host "Processing Request for: " $user.UPN
$ie.visible = $true
$ie.Navigate($jsonlink)
while($ie.Busy){Sleep 4}
while($ie.Document.location.href -like '*login.microsoftonline.com/*'){
(
$ie.document.IHTMLDocument3_getElementsByTagName("table") | Where-Object{$_.Id -eq $userName} | select -First 1).click()
Sleep 4
}
Write-Host "Ensure you are in the Delve Screem ..." -ForegroundColor Yellow
#[void](Read-Host 'Press Enter to continue after login ' -ErrorAction SilentlyContinue)
#Navigate to Properties
$ie.document.IHTMLDocument3_getElementById("O365_MainLink_Settings").click()
Sleep 4
#Open Settings
($ie.document.IHTMLDocument3_getElementsByTagName("a") | Where-Object{$_.className -eq "o365cs-settings-deeplink wf-size-x12 ms-fcl-ns o365button"} | select -First 1).click()
Sleep 4
##Switch Value
$checkBox =$ie.document.IHTMLDocument3_getElementById("enableDocumentsCheckbox")
$oldValue = $checkBox.value
if($oldValue -eq "on" -or $oldValue -eq "off")
{
$checked = $checkBox.click()
}
Sleep 2
#$newValue = $ie.document.IHTMLDocument3_getElementById("enableDocumentsCheckbox").value
##Submit
Sleep 1
($ie.document.IHTMLDocument3_getElementsByTagName("button") | Where-Object{$_.className -eq "SharingActivityPanel-module_button_delve"} | select -First 1).click()
##Refresh
Sleep 4
$ie.document.location = $ie.document.location
write-host "Done Successfully Delve Flag Switched"
}
catch [System.Exception]{
write-host "Failed, Try Again" $_.Exception
}
finally {
while($ie.Busy){Sleep 1}
$ie.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie)
}