Copy-Paste the below script in a text file and rename its extension to - *.ps1
function LoadSharePointPowerShellEnvironment
{
write-host
write-host "Setting up PowerShell environment for SharePoint..." -foregroundcolor Yellow
write-host
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
}
function RetractSolution($packageName)
{
$spAdminServiceName = "SPAdminV4"
$solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
if ($solution -ne $null)
{
#Retract the solution
if ($solution.Deployed)
{
Write-Host
Write-Host "Retracting solution $packageName..." -foregroundcolor Yellow
if ($solution.ContainsWebApplicationResource)
{
$solution | Uninstall-SPSolution -AllWebApplications -Confirm:$false
}
else
{
$solution | Uninstall-SPSolution -Confirm:$false
}
Write-Host
Write-Host "starting admin service...(this may take few minutes to complete)" -foregroundcolor Yellow
Stop-Service -Name $spAdminServiceName
Start-SPAdminJob -Verbose
Start-Service -Name $spAdminServiceName
#Block until we're sure the solution is no longer deployed.
do { Start-Sleep 2 } while ((Get-SPSolution $packageName).Deployed)
}
#Delete the solution
Write-Host
Write-Host "Removing solution $packageName..."
Get-SPSolution $packageName | Remove-SPSolution -Confirm:$false
Write-Host "Solution Removed Sucessfully." -foregroundcolor Green
}
else
{
Write-Host
Write-Host "No solution found to retract with name $packageName" -foregroundcolor Yellow
}
}
#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Retract the package
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
LoadSharePointPowerShellEnvironment
Write-host "Retracting Solution..." -foregroundcolor Yellow
$dir = $(gl)
$list = Get-ChildItem $dir | where {$_.extension -eq ".wsp"}
foreach($my_file in Get-ChildItem $list)
{
write-host $my_file.Name
[string]$packageName = $my_file.Name
RetractSolution $packageName
}
No comments:
Post a Comment