Monthly Archives: August 2016

Retrieve SharePoint Site Groups Information Using PowerShell

A colleague asked me a script to retrieve SharePoint Site Groups information by excluding the user’s information and added he couldn’t use Select-Object cmdlet to exclude user’s collection in the group property. Yes, it’s not possible! But, we have a fix for it and here is the PowerShell script to get Group properties by ignoring the UserCollection property in it

Import-Module C:\Users\904870.CORPORATEROOT\Documents\WindowsPowerShell\Modules\xMicrosoft.SharePointOnline.PowerShell\Microsoft.SharePoint.Client.dll
$UserName = "chendrayan@contoso.com"
$Credential = ConvertTo-SecureString -String "P@ssw0rd" -AsPlainText -Force
$ctx = [Microsoft.SharePoint.Client.ClientContext]::new("https://contoso.sharepoint.com")
$ctx.Credentials = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($UserName,$Credential)
$Groups = $ctx.Web.SiteGroups
$ctx.Load($Groups)
$ctx.ExecuteQuery()
$ctx.Dispose()
$Props = ([Microsoft.SharePoint.Client.Group].GetProperties() | ? {$_.PropertyType -notlike '*Collection*'}).Name 
foreach($Group in $Groups)
{
    [Microsoft.SharePoint.Client.Group]$Group | Select -Property $Props
}

Reason: We need to explicit request and execute to get user collection information but in our case it’s not required. So, simply we skipped the property which has type as collection. Note: In group property user is the only collection type property.

Import User Profile Picture from Exchange Online using EWS and PowerShell

One of our customer requested a solution to import user profile picture from Exchange Online. Not a big task but this article is to share the simple and clean method to carry out the task without MSOnline module. Yes, we use only PowerShell! At customer environment user uploads profile picture in intranet portal which saves the picture in file share, yeah I got the same question why can’t they get it from the file share? We can but not easy to do because of the profile image file naming convention, every user has an intranet portal profile ID so the files are saved as ID_DATETIMESTAMP. E.G “PROFILEID_YYYYMMDDHHMMSS”. There are many possibilities to do like query intranet portal DB to retrieve image file information and retrieve, use MSOnline module Get-UserPhoto etc.

Outclass is to use PowerShell and get it done on the fly. All we need is the EWS url for the profile picture residing in the Exchange Online and it’s shared below with sizes for reference and hew is the documentation for Get User Photos by using EWS in Exchange

1 https://outlook.office365.com/ews/Exchange.asmx/s/GetUserPhoto?email=”UserEmail” &size=HR96x96 HR96x96
2 https://outlook.office365.com/ews/Exchange.asmx/s/GetUserPhoto?email=”UserEmail” &size=HR240x240 HR240x240
3 https://outlook.office365.com/ews/Exchange.asmx/s/GetUserPhoto?email=”UserEmail” &size=HR648x648 HR648x648

 

All set to spin up PowerShell ISE for scripting! Simply use Invoke-WebRequest cmdlet with OutFile parameter to save the profile image in disk like shown below

$Param =@{
Uri = "https://outlook.office365.com/ews/Exchange.asmx/s/GetUserPhoto?email=Chendrayan.Venkatesan@contoso.com&size=HR648x648"
Credential = (Get-Credential "Chendrayan.Venkatesan@contoso.com")
OutFile = "C:\Temp\UserID.jpg"
}

Invoke-WebRequest @Param

Yay, its easy to get the profile picture information at ease without using Get-UserPhoto cmdlet and no need of Set-Content with Encoding parameters (Saves more time) ! here is my picture – Output of the above snippet!

Chen

Check out the sample script below which use ADSI Searcher to retrieve user AD information and save the picture in disk as sAMAccountName_DisplayName.jpg 🙂

function Get-xUserPhoto
{
<#
.Synopsis
   Imports User Profile Picture from Exchange Online using PowerShell and EWS
.DESCRIPTION
   This is a sample script to export user profile picture from exchange online using EWS and PowerShell.
   Use Invoke-Webrequest cmdlet
.EXAMPLE
   Get-xUserPhoto -Email Chendrayan.Venkatesan@contoso.com -Path C:\Temp\ -Credential "Chendrayan.Venkatesan@contoso.com"
   Import single user profile picture from Exchange Online and save it in C:\Temp as sAMAccountName_DisplayName.jpg
.EXAMPLE
   "Chendrayan.Venkatesan@contoso.com" , "Chendrayan.Venkatesan@contoso.com" | Get-xUserPhoto -Path C:\temp -Credential "Chendrayan.Venkatesan@contoso.com"
   Retrieves multiple user profile picture and save it in C:\temp
#>
    param
    (
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        $Email,

        [Parameter(Mandatory=$False)]
        [ValidateSet("96x96","240x240","648x648")]
        $Size="648x648",

        [Parameter(Mandatory)]
        $Path,

        [Parameter(Mandatory)]
        [pscredential]
        [System.Management.Automation.CredentialAttribute()]
        $Credential
    )

    process
    {
        try
        {
            $DEObj = ([adsisearcher]"(&(objectclass=user)(objectclass=person)(mail=$Email))").FindOne().GetDirectoryEntry()
            $Uri = [String]::Concat("https://outlook.office365.com/ews/Exchange.asmx/s/GetUserPhoto?email=",$Email,"&size=HR",$Size)
            if(Test-Path -Path $Path)
            {
                $File = [string]::Concat($Path,"\",$DEObj.sAMAccountName, "_", $DEObj.DisplayName,".jpg")
                Invoke-WebRequest -Uri $Uri -Credential $Credential -OutFile $File
            }
            else
            {
                Write-Warning "Please Check the Path"
            }
        }
        catch
        {
            $_.Exception.Message 
        }
    }
}