Archive for category PowerShell

Extract wsps from SharePoint

Background

Here’s a quick and dirty way to copy all wsps from a SharePoint farm.

Other info

Script

#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin “Microsoft.SharePoint.PowerShell”
}

#Get the farm
$farm = [microsoft.sharepoint.administration.spfarm]::local

#Copy all the wsps to c:\temp
foreach ($wsp in $farm.Solutions)
{
$file = “C:\wsps\” + $wsp.name + “.wsp”
$wsp.SolutionFile.SaveAs($file)
}

 

, ,

Leave a comment

Remove disabled profiles from SharePoint 2010

Background

SharePoint User Profile Service (UPS) uses a cut down version of a product called Forefront Identity Manager (FIM) to import profiles from AD. By default it imports all profiles within the OUs that you point it at. You can add filters such as “don’t import disabled profiles” but you have to do this before the very first import, if you add it later, newly disabled profiles are not imported (that’s a tripple negative)  but already imported accounts are “orphaned”. The script below could be used to clean up such accounts.

I actually used it in a slightly more unusual situation. Until SharePoint 2010 SP1, the implementation of FIM was woeful. On one occasion it failed to incrementally import profiles for a few days but because it offers no alerting and always reports success to SharePoint, we were left unaware. One morning I came into the office to be told that people were reporting that they’d received emails from SharePoint saying that members of their team had left the company and so their MySite had been given to them to backup prior to deletion. As a quick fix I disabled the profile cleanup timer job and to this day haven’t had the nerve to re-enable it (I have visions of thousands of managers getting such emails immediately prior to SharePoint purging terabytes of data.) So instead I run this script to clean up old profiles. One day I’ll have to bite the bullet, but for now this keeps the org chart clean and keeps me from having nightmares about UPS.

Other info

Requires Quest’s AD PowerShell commandlets http://www.quest.com/powershell/activeroles-server.aspx

Please read this before using any of my scripts https://scriptsahoy.wordpress.com/about/

Script

#Find out how many accounts we should delete ( I’ll make this interogate UPS at some point in future
#for now this fudge will do
Param ([int]$limit)

if (!$limit)
{
$limit = 999999999
}

$count = 1

write-host “————- Started ————-”
$output = Get-Date
$output = “Started at ” + $output.ToString()
$output | Out-File -FilePath c:\output.txt -append
#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin “Microsoft.SharePoint.PowerShell”
}
#Add Quest AD PowerShell SnapIn if not already added
if ((Get-PSSnapin “Quest.ActiveRoles.ADManagement” -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin “Quest.ActiveRoles.ADManagement”
}
#Set my site host location.
$site = new-object Microsoft.SharePoint.SPSite(“http://mysites”);
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);

#Get UserProfileManager and get all profiles
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
$AllProfiles = $ProfileManager.GetEnumerator()

#iterate around the profiles
foreach($profile in $AllProfiles)
{
#get the associated AD account
$ADUser = $PROFILE.MultiloginAccounts | Get-QADUser

#check if the account is diasabled in AD
if ($ADUser.AccountIsDisabled)
{
#delete the profile
$ProfileManager.RemoveUserProfile($ADUser.NTAccountName)
$output = $count.ToString() + “: ” + $ADUser.NTAccountName
$output | Out-File -FilePath c:\output.txt -append
write-host $output

$count++
if ($count -gt $limit)
{
break
}
}
}

write-host “————- Finished ————-”
write-host ($count -1) accounts removed
$output = Get-Date
$output = “Finished at ” + $output.ToString()
$output | Out-File -FilePath c:\output.txt -append

$site.Dispose()

, ,

2 Comments