My Site Clean Up Job介绍----自定义开发
#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$LogDirectory = "D:\CleanupUserProfile\Logs"
$LogFile = "CleanupUserProfile_" + (Get-Date -UFormat "%Y%m%d") + ".txt"
if([IO.Directory]::Exists($LogDirectory))
{
}
else
{
New-Item $LogDirectory -type directory
}
$LogPath = $LogDirectory +"\" + $LogFile
#create log file
New-Item $LogPath -type file -force
Add-Content $LogPath "MySiteUrl,SecondarySiteOwner,AccountRemove"
$CAUrl = (Get-spwebapplication -includecentraladministration | where {$_.DisplayName -eq "SharePoint Central Administration v4"}).Url
$site = new-object Microsoft.SharePoint.SPSite($CAUrl)
$StartWith="contoso"
$patterns = $StartWith.Split(" ")
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site)
#Get UserProfileManager from the My Site Host Site context
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
$mysiteHost = $ProfileManager.MySiteHostUrl
$ProfileSearchManager = [Microsoft.Office.Server.UserProfiles.ProfileSearchManager]::GetProfileSearchManager($ServiceContext)
$AllMissProfiles = $ProfileSearchManager.Search($patterns,[Microsoft.Office.Server.UserProfiles.ProfileSearchFlags]::User,0,$true)
foreach($profilebase in $AllMissProfiles)
{
try
{
Add-Content $LogPath "==========================="
$AccountName = $profilebase.GetProfileValueCollection("AccountName").Value
Add-Content $LogPath "AccountName : $AccountName"
$profile = $ProfileManager.GetUserProfile($AccountName)
Add-Content $LogPath "Profile"
$mysitePubUrl = $profile.PublicUrl.AbsoluteUri
Add-Content $LogPath "MySite Public Url $mysitePubUrl"
$accountInfo = $AccountName.Split("\")
Add-Content $LogPath "AccountInfo: $accountInfo[0]"
$mysiteUrl = $mysiteHost + "personal/"+ $accountInfo[1]+"/"
Add-Content $LogPath "MySite Url: $mysiteUrl"
$manager = $profile.GetManager()
Add-Content $LogPath "Manager: $manager"
if($manager)
{
$mAccName = $manager[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value
Add-Content $LogPath "Manager name: $mAccName"
$personalSite = Get-SPSite $mysiteUrl
if($personalSite)
{
Set-SPSite -Identity $mysiteUrl –SecondaryOwner $mAccName
}
Add-Content $LogPath "Set owner"
Add-Content $LogPath "$mysitePubUrl, $mAccName, $AccountName"
}
#$ProfileManager.RemoveUserProfile($AccountName)
}
catch{}
}
$site.Dispose()