My Site Clean Up Job介绍----自定义开发

                               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() 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值