Wednesday, October 14, 2015

Create mysite using Powershell

Function Create-MySite           
{           
 [CmdletBinding()]           
 Param           
 (           
  [Parameter(Mandatory = $True,Position=2,valueFromPipeline=$true)][String]$Username,           
     [Parameter(Mandatory = $True,Position=1)][String]$MySiteRootURL             
 )           
 [void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server");              
 $site=new-object Microsoft.SharePoint.SPSite($MySiteRootURL);           
 try           
 {           
  $serviceContext = Get-SPServiceContext $site;           
  $upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);            
  if($upm.UserExists($Username) -eq $false)           
  {           
   Write-Host "User $Username was not found in the profile store." -f yellow;           
   return;           
  }           
  $userProfile = $upm.GetUserProfile($Username);           
  if($userProfile.PersonalSite -eq $Null)           
  {           
   Write-Host "Creating MySite for user $Username" -f darkyellow;           
   $userProfile.CreatePersonalSite();                 
   Write-host "Successfully created MySite for user $Username" -f green;           
  }           
  else           
  {           
   Write-Host "User $Username already has a MySite." -f darkgreen;           
  }           
 }           
 catch           
 {           
  Write-Host "Encountered an error creating a MySite for user $Username. Error:"$_.Exception -f Red;           
 }           
 finally           
 {           
  $site.Dispose();           
 }           
}
Create-MySite -MySiteRootURL "http://url" -Username "domain\user"

No comments:

Post a Comment