Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Wednesday, 18 May 2016

How to Immediately terminate a Users Sessions in SharePoint Online


With the latest version of SharePoint Online Management Shell a new PowerShell cmdlet called Revoke-SPOUserSession was released. This cmdlet allows us to terminate all sessions established by a particular user to SharePoint online. Terminating user session remotely is pretty handy in situations where a users’s device is lost and you want to protect corporate data from unauthorized access. Revoke-SPOUserSession cmdlet is pretty simple to use and below are the steps for using it

Note: The latest version of SharePoint Online Management Shell is required to execute the Revoke-SPOUSerSession cmdlet.  This can be downloaded form https://www.microsoft.com/en-us/download/details.aspx?id=35588

1.      Run the SharePoint Online Management Shell and connect to SharePoint Online tenant as a global admin.

2.      Execute the Revoke-SPOUserSession cmdlet by passing the name of the user for whom you want to terminate sessions.

E.g. Revoke-SPOUserSession – User username@mycompanysite.com

3.      You would be prompted to confirm the action and once you confirm by giving yes the user session will be terminated from all devices that he has logged on to and a success message will be displayed.

4.      If you don’t want the confirmation prompt you could pass the value of false to Confirm parameter

E.g. Revoke-SPOUserSession – User username@mycompanysite.com – Confirm:$false

Monday, 11 August 2014

Export SharePoint Managed Metadata Terms using CSOM and PowerShell


SharePoint 2013 out of the box has provision for importing metadata terms from a CSV file but there is no provision to export the terms that have been created. Exporting the terms can be accomplished by using Get-SPTaxonomySession powershell cmdlet. Unfortunately this cmdlet  is not available for Office 365 online so cannot be used to export metadata terms from SharePoint Online.

An alternate approach is to connect to SharePoint Online Taxonomy Session through CSOM and export the terms to a file. Below script uses CSOM and a recursive function to loop through metadata terms in SharePoint Online and write them to a file

#Specify admin user and SharePoint site URL

$User = “User Name”

$Site = "SharePoint Online URL"

#Adding references to SharePoint client assemblies

Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll”

Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”

Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll”

 
$Pwd = Read-Host -Prompt “Enter your password” -AsSecureString


$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)

$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Pwd)

$Context.Credentials = $Credentials

$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)

$Context.Load($MMS)

$Context.ExecuteQuery()

 
#Get Term Stores

$TermStores = $MMS.TermStores

$Context.Load($TermStores)

$Context.ExecuteQuery()

 
$TermStore = $TermStores[0]

$Context.Load($TermStore)

$Context.ExecuteQuery()


#Get Groups

$Groups = $TermStore.Groups

$Context.Load($Groups)

$Context.ExecuteQuery()
 

#Create the file and add headings

$OutputFile = "Output File Path"


$file = New-Object System.IO.StreamWriter($OutputFile)

$file.Writeline("Term Set Name,GUID");


Foreach ($Group in $Groups)

    {

    $Context.Load($Group)

    $Context.ExecuteQuery()
 

    $TermSets = $Group.TermSets

    $Context.Load($TermSets)

    $Context.ExecuteQuery()

    Foreach ($TermSet in $TermSets)

        {

        $file.Writeline($TermSet.Name + "," + $TermSet.Id);

   
        $Terms = $TermSet.Terms

        $Context.Load($Terms)

        $Context.ExecuteQuery()

        Foreach ($Term in $Terms)

            {

                 $file.Writeline($Term.Name + "," + $Term.Id);

                 GetTerms($Term);

            }

        }

        $file.Flush();

          $file.Close();

 
        }
  

#Recursive function to get terms

function GetTerms([Microsoft.SharePoint.Client.Taxonomy.Term] $term)

{

 $SubTerms = $term.Terms;

        $Context.Load($SubTerms)

         $Context.ExecuteQuery();

            Foreach ($SubTerm in $SubTerms)

            {

            $file.Writeline($SubTerm.Name + "," + $SubTerm.Id);

            GetTerms($SubTerm);

            }

}

Note: Update the User, Site and OutputFile variables to values matching your environment.