Friday, September 14, 2018

Office 365 site self-provisioning with SharePoint Framework (SPFx), Azure Logic Apps, Queue, and Azure Function


About three years, when we first looked at the Office 365 site provisioning for the company, there were limited options. The best option at that time was using provider hosted SharePoint add-in architecture similar like posted here. Since then PnP provisioning package came out and has been gone through multiple iterations. With many Azure services available and integration with O365 now, there are more better options like the one published here. We have put together the Office 365 Modern Provisioning architecture and implemented using SharePoint Framework (SPFx), Azure Logic Apps, Queue, and Azure Function for the company. We will compare some alternative and options in the blog. We will cover SharePoint on-premises site self-provisioning with SharePoint Framework (SPFx), Azure Logic Apps, Queue, and Azure Function in another blog.

The Office 365 site self-provisioning architecture is illustrated in the below diagram. Here are the components.
  
  • User enters site provisioning information from SharePoint Framework (SPFx) form on SPO site.
  • The new list item created will trigger the Azure Logic app to send to approval.
  • If the request approved, Azure Logic App will send the request to Azure queue with request ID that is list item ID.
  • Azure queue will trigger the Azure function to provision the site collection and perform post provisioning steps.


Here is the details for each component and different alternative.

1. The first component is SharePoint online list that will store all the user site request. The reason we use this is to capture the site creation request for history and analysis. The list should have minimal the following fields. You could adjust fields based on your business requirement.
  • SPSiteTitle
  • SPSiteURL
  • SPSiteType
  • SPSiteDescriptions
  • SPSiteOwners
  • SPSiteMembers
  • SPSiteVistors
  • ApprovalStatus – Capture the approval status
  • ProvisioningStatus – This will be used to indicate if site create successfully

2. The second component is SharePoint Framework (SPFx) form. You could use Azure PowerApps, blot, SharePoint add-in as alternatives. The reason we select SPFx form because we need extensive field validation. One example is when user enters the site URL, we need to verify if the URL is already in SharePoint or already in the request list. Our current SPFx form looks like the screen below.


3. The third component is the Azure Logic App. The Logic App will be triggered when new site request SharePoint online list item created. You could use Azure flow or WebJob as alternatives. The reason we select Azure Logic App because we have not open flow for users and we would like to control all components inside same place. In this case is inside Azure resource groups.


4. The forth component is the Azure storage queue. The queue has the site provisioning request information that will trigger the Azure function. The reason we choice the queue is to decouple the services and we will extend to support both  SharePoint online and SharePoint on-premises site provisioning. We decided to only include the "RequestId" in the queue to indicate the request list item ID. This will make the design flexible since any fields we need to add to the site creation will not impact the queue. The queue looks like this below. 

{
"RequestId": "86"
}

5. The firth component is the Azure function. We use PnP PowerShell as the implementation. You can use any language as the implementation but PnP PowerShell is much simpler. The only concern is PowerShell support is experimental at current V1.x. We are hoping future PowerShell V2.x will still be supported in Azure function. Here is the snippet of the Azure function code.

#Import dll from library
Import-Module "D:\home\site\wwwroot\yourmodulefodler\modules\SharePointPnPPowerShellOnline.psd1" -Global;
Import-Module "D:\home\site\wwwroot\yourmodulefodler\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell" -Global;

#Get trigger content
$requestBody = Get-Content $triggerInput -Raw | ConvertFrom-Json

#Initial variables
$username = $env:sssp_username
$password = $env:sssp_password
$requestListSiteUrl = $env:sssp_requestsiteurl
$requestList = $env:sssp_requestlistname
$siteCollectionRootURL = $env:sssp_rooturl
$SiteDesign ="Topic"    #Right now hard coded to Topic only
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)

#Get request ID from queue
$requestID = $requestBody.RequestId

#Use request ID from the queue to retrive request information form the SPO site request list

    # Connect to request site and get request information based on request ID
    Connect-PnPOnline -url $requestListSiteUrl -Credentials $creds

    #Get request item
    $requestItem = Get-PnPListItem -List $requestList -Id $requestID
    $requestItem

    #Get all required site request information
    $requestSiteTitle =  $requestItem["SPSiteName"]
    $requestSiteUrl = $requestItem["SPSiteURL"]
    $requestDescription = $requestItem["SPSiteDescription"]
    $requestSiteOwners = $requestItem["SPSiteOwners"]
    $requestSiteMembers = $requestItem["SPSiteMembers"]
    $requestSiteVisitors = $requestItem["SPSiteVisitors"]
    $requestSiteType = $requestItem["SPSiteType"]

    #Create the site collection
    $siteCollectionURL = $siteCollectionRootURL + $requestSiteUrl

    $newSiteUrl = New-PnPSite -Type CommunicationSite -Title $requestSiteTitle -Url $siteCollectionURL -Description $requestDescription -SiteDesign $SiteDesign

    #Connect to the new created site collection
    Connect-PnPOnline -url $siteCollectionURL -Credentials $creds

    #Add additional site collection admin
    Add-PnPSiteCollectionAdmin -Owners additionalowner@mycompany.com

    foreach ($owner in $requestSiteOwners)
    {
        #Add-PnPUserToGroup -LoginName $ownerEmail -Identity $siteOwnersGroup
        if($owner.Email -ne $null -and $owner.Email -ne "")
        {
            Add-PnPUserToGroup -LoginName $owner.Email -Identity 3
        }
        else
        {
            Add-PnPUserToGroup -LoginName $owner.LookupValue -Identity 3
        }
    }
    # Other post provisioning steps …
    # Connect to request site and set ProvisioningStatus to "Provisioned"

There are few enhancements you could do. One example is  you can set up a app and use AppId and App Secret in the Powershell as below.

Connect-PnPOnline -Url <String> -AppId <String> -AppSecret <String>

The other two enhancements we are thinking are use site design and utilize PnP template. At this time, we are enhance the Office 365 site self-provisioning process to support both SharePoint online and SharePoint on-premises site creation process. We will share the architecture and implementation in the future.

Tuesday, September 4, 2018

How to resolve Azure Run As account creation "Insufficient privileges" error


Azure Automation delivers a cloud-based automation and configuration service that provides consistent management across your Azure and non-Azure environments. It consists of process automation, update management, and configuration features. Azure Automation provides complete control during deployment, operations, and decommissioning of workloads and resources. 



Run As accounts in Azure Automation are used to provide authentication for managing resources in Azure with the Azure cmdlets. When you create a Run As account, it creates a new service principal user in Azure Active Directory and assigns the Contributor role to this user at the subscription level.


If you have the following  "Insufficient privileges" error, you should check the following two settings to resolve the issue.



To create or update a Run As account, you must have specific privileges and permissions. A Global Administrator/Co-Administrator can complete all the tasks. In a situation where you have separation of duties, the permission table from Microsoft shows a listing of the task. The two permissions including "Application Developer Role" and "Application administrator" roles should be assigned to the user to create Run As account.


As a result, the first way to resolve the Run As account creation error is to assign both "Application Developer Role" and "Application administrator" roles to the user who will create the Azure automation account. I would think this is the prefer the way.

The second way is to check if user has permission to register application. Go to Azure Activity Directory-> User Setting and check the "App registrations" setting is to "Yes". If not user will NOT be able to create the Run As account.


If global admin enabled this setting, user should be able to create the Run As a account w/o issue. Since this is tenant level configuration and global admin may not want to enable this for all users, the first method might be the better option.

Thursday, August 30, 2018

Tips to install Azure Hybrid Runbook Worker

You can use the Hybrid Runbook Worker feature of Azure Automation to run runbooks directly on the computer that's hosting the role and against resources in the environment to manage those local resources. This is useful if you want to automate tasks in other clouds and on-premises environments.

The best way to start a Hybrid Runbook Worker is to read the procedure the instruction from Microsoft first. However, the steps are little difficult to follow, you can use the procedure with detailed information published by Anderson here. After we configured few Hybrid Runbook Workers, we found there are few tricks that need to be aware of in order to configure them quickly. 

1. The first trick is configure Azure Hybrid Runbook Worker will need to create and configure the following resources.
  • Azure automation account
  • Azure resource groups
  • Log Analytics
  • OMS Workspace

All the resources need to be in the same location! Since not all locations are available for the four resources listed above and only few locations are common to all the four resources. My suggestion is to look at the available OMS Workspace first since it has lest available locations and find the common location to create each resource.  I had issue in the last resource creation and I'm not able to find the resource group and had to recreate again from scratch. 

2. The second trick is you can configure the same on-premise VM/server to run as Hybrid Runbook Worker for multiple Azure tenants. For each Azure environment, you need to run the following command to login and it will configure the worker connecting to that Azure.

Login-AzureRmAccount

3. The third trick is to automate the procedure to get all needed information to run PowerShell to create Hybrid Runbook Worker.  

New-OnPremiseHybridWorker.ps1 -AutomationAccountName <NameofAutomationAccount> -AAResourceGroupName <NameofResourceGroup> -OMSResourceGroupName <NameofOResourceGroup> -HybridGroupName <NameofHRWGroup> -SubscriptionId <AzureSubscriptionId> -WorkspaceName <NameOfLogAnalyticsWorkspace>

You can see there are many parameters you need to collection by different PowerShell and it’s tedious to put the right perimeter to pass to the script. Here is the enhanced PowerShell to the one published by Anderson to automate the process. All the parameters are captured by PowerShell and assigned to variable and passed to final script.

# Step 1 - Login
Login-AzureRmAccount

# Step #2 – Get WorkspaceName and OMSResourceGroupName
$NameOfLogAnalyticsWorkspace = Get-AzureRMOperationalInsightsWorkspace | select Name
$NameofOResourceGroup = Get-AzureRMOperationalInsightsWorkspace | select ResourceGroupName

# Step #3 -  Het AutomationAccountName and AAREsourceGroupName
$NameofAutomationAccount = Get-AzureRMAutomationAccount | select AutomationAccountName
$NameofResourceGroup = Get-AzureRMAutomationAccount | select ResourceGroupName

# Step #4 – Get SubscriptionID. Please note this assume you have only one Subscription!
$AzureSubscriptionId = Get-AzureRMSubscription | select SubscriptionId

# Step #5 – Define HybridGroupName you need
$NameofHRWGroup = “QCSBXHybGroup0”

# All parameters are assigned to variables in previous commands for your convenience
New-OnPremiseHybridWorker.ps1 -AutomationAccountName $NameofAutomationAccount -AAResourceGroupName $NameofResourceGroup -OMSResourceGroupName $NameofOResourceGroup -HybridGroupName $NameofHRWGroup -SubscriptionId $AzureSubscriptionId -WorkspaceName $NameOfLogAnalyticsWorkspace


4. The forth trick is you will run into error below when using Azure Credential if you have Azure Automation PowerShell ISE Add-On was also installed on the hybrid worker server.  

AzureAutomationAuthoringToolkit: Warning - Local value for PSCredential asset "onpremCred" not found. When you are using Credential in the PowerShell code like below.
$onpremCred = Get-AutomationPSCredential -Name "onpremCred"

The issue is You can verify Azure Automation Powershell ISE add-on is impacting the call. You can verify if the following package inslatted and you can delete the whole folder.

AzureAutomationAuthoringToolkit folder under C:\Program Files\WindowsPowerShell\Modules

5. The last trick is you monitor Azure Hybrid Runbook Workers from Azure portal. You can go to Portal Azure and click the highlighted "Hybrid Worker" details icon. It will display the hybrid worker server name.

Now you should be able to set up the Azure Hybrid Runbook Workers and manage on-premises resources inside Azure cloud!

Monday, August 27, 2018

How to resolve error "The user does not exist or is not unique." when use Set-SPOUser for SharePoint online?


In order to support daily SharePoint online activities, there is a need to add a group to all SPO site as site collection admin. Before our company come up a site provisioning process that will add the group to each site collection, there are some sites created from SharePoint UI and Groups that do not have that specific group as site collection admin. When we use PowerShell to add that group, we got the following error.

“Set-SPOUser : The user does not exist or is not unique.”

We have same issues in the self-provisioning Azure function PowerShell to add some users to SPO groups like owner group. After debugging the issues, we identified that is related to SharePoint online how to deal with the groups. The group will not have email and here is the way to add them to site collection admin or add into SharePoint group.

You need to find the SID of the group like "c:0t.c|tenant|????????-????-????-????-????????????" from SPO UI. You can first check the permission for that group in SPO and you will find the SID for the group as we discussed in previous blog. Then you can add as site collection admin.

$username = "spoadmin.onmicrosoft.com"
$password = "password"

$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)

Connect-SPOService -Url "https://mycompany-admin.sharepoint.com/" -Credential $creds


$siteUrl = "https://mycompany.sharepoint.com/teams/departmentsite"
$SiteCollectionAdmin = "c:0t.c|tenant|????????-????-????-????-????????????"
Set-SPOUser -site $siteUrl -LoginName $SiteCollectionAdmin -IsSiteCollectionAdmin $True

You can use the group LookupValue value if you need to add to groups. Here is PnP PowerShell example.

Connect-PnPOnline -url $siteUrl -Credentials $creds 

$owner = “group1@mycompany.com”
if($owner.Email -ne $null -and $owner.Email -ne "")
{
       Add-PnPUserToGroup -LoginName $owner.Email -Identity 3
}
else
{
       Add-PnPUserToGroup -LoginName $owner.LookupValue -Identity 3
}

Now you should be able to deal the groups inside SharePoint online using PowerShell.

Thursday, April 26, 2018

Tips to avoid and resolve common issues in Azure Automation


Azure Automation is a software as a service (SaaS) application that provides a scalable and reliable multitenant environment in which you can use runbooks to automate processes. There are many tricks you would need to know before you could enjoy this feature.


x

1. The first tip is to handle "Automation Account" creation error. The common issue is the error Classic Run As account creation error like "Classic Run As Creation Error ForbiddenError: The server failed to authenticate the request.".


The way to resolve this is to add the Azure Account as co-administrator or the administrator of the subscription. The procedure is described here.



You can then delete the Classic Run As Creation and then recreate it.

2. The second tip is to handle certificate expiration. Azure automation certificates expire in one year.


You will get error message when it expires. You can renew from the UI or run the powershell to set never expire. The example PowerShell is listed here.  Here are some detailed information.

a. Run PowerShell Login-AzureRmAccount to get "Environment" variable.


b. Run PowerShell to get "SubscriptionId" and "AutomationAccountName".
Install-Module -Name AzureAutomationAuthoringToolkit 
Get-AzureRmAutomationAccount -ResourceGroupName "yourresourcegroupname" 


3. The third tip is to update default modules. The default modules for Azure Automation is old version and may functions may not exist for that version, you need to manually click "Update Azure Modules" in the "Modules". Here is the default modules and you can see most of the modules are in version 1.


Here is the screenshot after updates.


There are few other tips already covered in previous blog.


This link walks you through the pricing model of everything available in Azure.




Here are some documents about Update Management, which uses Log Analytics. 


Three different ways to use NextLabs 8.5 policy to block SharePoint site collections

NextLabs 8.5 provides new policy module to block SharePoint site collections. Here are three most popular ways to block SharePoint site collections.

1. Use Resource Components with site url. 

You can add one site collection url to one resource component and the policy can include multiple Resource Components to clock multiple site collection. See screenshots below.



2. Use expression in advanced condition in the policy.

You can add expression on the urls in advanced condition in the policy as in the below screenshot.


3. Use SharePoint site collection site property bag value or sub-site web property value.

a. First, add the SharePoint site collection site property bag key in the SharePoint Enforcer configuration.xml file.  Bounce the enforcer after the change.

Example is below.

    <PropertyBag disabled="false" level="SiteCollection">
      <Property disabled="false" name="SensitiveSiteCollection" attributename="SensitiveSiteCollection" />
    </PropertyBag>
    <PropertyBag disabled="false" level="SubSite">
      <Property disabled="false" name="SensitiveSubSite" attributename="SensitiveSubSite" />
    </PropertyBag>


b. Add the property to site collection property bag.


c. Construct the component resource in condition with the property bag. Then use the component resource in the policy.



Based on our testing, the option #3 is much simple than other two options. We will implement option #3 in the future for most of the policies. Of cause, the site collection bag can be modified by site owner and the policy can be impacted. As a result, we will need to address the property bag seciruty control if we use option #3.

Wednesday, April 25, 2018

Procedure to add IP condition for NextLabs policy on version 8.5

If you need to block/allow SharePoint request based on the original user request IP address, here are the procedures.

1. Go to the following web site and convert IPv4 to IP Decimal.

https://www.ipaddressguide.com/ip

2.  Create a resource component. In the conditions, add "inet_address" condition with the decimal value of the IP address from step #1. Then create the policy to include the resource component.


3. If you need to test the original IP in "X-Forwarded-For" that is in the request header but not in the direct request, you can use the "Modify Header Value" add-in for your testing. The screenshots are listed below.



Now you can test any http request header in the NextLabs policy.