February 7, 2019

Multithreading in PowerShell & Exchange Online (Office 365)


Challenge:
The details of a few (around 300) users' mailbox in Exchange Online were to be fetched, and the approach is via PowerShell. Now, the total users were over 3800, and to filter from those, consumed a lot of time. As a result, going the conventional way of getting those one-by-one took us 39 seconds per user which was around 3.5 hours for the whole script execution.  

Resolution:
We had to find another way to get this done. So, we chose the approach of getting multiple records simultaneously, using background processes. 

And, multi-threading came to the rescue!!

We created the connection to Exchange Online via PowerShell:
$Credentials = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri  https://outlook.office365.com/powershell-liveid/ -Credential $Credentials -Authentication  Basic -AllowRedirection

Import-PSSession $Session 

Then, we read the user display names from the file, and stored them in an array. 
$accounts = Import-CSV C:\Temp\File.csv

Now, instead of specifying the number of jobs, we would want the jobs to be automatically created.

So, we would be specifying, the batch size of each job. In our case, we chose 50 users per batch.

$accountsperBatch = 50

Then, we iterated the loop for all the accounts to be split into batches as jobs, and then started the job using Start-Job for creating the job. 

This Start-Job  needs:

1. ScriptBlock in order to job to know, what it has to do.
2. ArgumentList in order to job to know, the items using which the job is to be performed.    

On specifying this, PowerShell would begin the creation of jobs, and as the job is created, the code within the ScriptBlock is executed.  
$accountsperBatch = 50
$i = 0
$j = $accountsperBatch - 1
$batch = 1
$csvContents = @() 
  
while ($i -lt $accounts.Count)
{
 $accountBatch = $accounts[$i..$j]
 
 $jobName = "Batch$batch"
 $fileName = "c:\Temp\Details$jobName"
 Start-Job  -Name $jobName `
       -ScriptBlock {
         param([string[]] $accounts)
  foreach($account in $accounts)
  {
   $mailBoxDetail = Get-Mailbox  -Identity  $account | Select  DisplayName, UserPrincipalName 
   $row =  New-Object System.Object
   $row | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value  $mailBoxDetail.DisplayName
   $row | Add-Member -MemberType NoteProperty -Name "UserPrincipalName" -Value  $mailBoxDetail.UserPrincipalName

   $csvContents += $row  
  }
                $csvContents | Export-CSV $fileName
             }`
      -ArgumentList (,$accountBatch)
     
 $batch += 1
 $i = $j + 1
 $j += $accountsperBatch
 
 if($i -gt $accounts.Count) {$i = $accounts.Count}
 if($j -gt $accounts.Count) {$j = $accounts.Count}
}

Then, as per the best practices, we would remove the jobs completed by using Remove-Job  and the PS Session using Remove-PSSession $Session. 

This script helped us to generate the details of the user accounts simultaneously and the results improved drastically and the results were available under 1.5 hours.

Note: If we open too many connections with Office 365, it would suspend the connections, as the security mechanism at Office 365 would consider this as a DDoS attack, thus, giving us an error like remote host not found or the connection was closed. 

If you have any questions you can reach out our SharePoint Consulting team here.

September 20, 2017

SharePoint Anonymous access issues with Mobile devices for Public-facing website

Problem Statement:
We implemented a public-facing website in SharePoint 2013. We have had anonymous access enabled for the web application. The anonymous access was working fine in the Desktop View, but we faced an issue with accessibility of the site in Mobile View. SharePoint site was prompting for authentication while using in Mobile view. It was working fine while using on desktop view.

Cause of the Error:
If you notice the browser's console window, you will find some JavaScript files are not accessible as an anonymous user, it throws 401 error. SharePoint automatically detects that a user is connected with a mobile browser, and redirects them to a mobile view of the page. However, by default anonymous users don’t have access to this page. In this situation, the likely goal is to not redirect the users to the mobile page and display the desktop responsive version page.

Resolution:
To fix this issue follow the certain steps (If it is multi-tiered SharePoint farm, these steps need to be followed on all the servers in the farm):
  1. Open the IIS Manager.
  2. Find the SharePoint Web Application where you found these issue.
  3. Explore the Web Application in File Explorer.
  4. Edit web.config file in a text editor.
  5. In <configuration> find <system .web>.
  6. Add or modify below code section.
  7. <browserCaps> <result type=”System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”/> <filter>isMobileDevice=false</filter> </browserCaps> 
  8. Save and browse the site, the issue should have been addressed.

Reference:
https://social.technet.microsoft.com/Forums/office/en-US/d1e58832-391b-42ba-a21b-6ef40d1c9acb/anonymous-access-for-mobile-users?forum=sharepointadminprevious

If you have any questions you can reach out our SharePoint Consulting team here.

September 5, 2017

Uninstall SharePoint 2013 - One or more required office component failed to complete successfully.

Issue:
While uninstalling SharePoint Server 2013, you may get below error message.

Error:
"One or more required office component failed to complete successfully. For more information, consult the setup log file." as shown in below screenshot.
 

Resolution: To fix the issue follow below steps:
 
1. Go to your Database Server.
2. Go to Services section:- Go to Run (Windows + R) and type "services.msc".
3. Stop the following services:
  • SQL Full-text Filter Daemon Launcher (MSSQLSERVER)
  • SQL Server (MSSQLSERVER)
  • SQL Server Agent (MSSQLSERVER)
  • SQL Server Analysis Services (MSSQLSERVER)
Again go to Control Panel and uninstall SharePoint 2013, you will not get this error this time.

In case, if you reinstall SharePoint server, do not forget to restart all those services.

If you have any questions you can reach out our SharePoint Consulting team here.