Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am new to PowerShell and have been asked to modify a script that is used to activate features on a site. The script has some latency issues between the two feature activations, so I decided to make a new function that will enable the feature and sleep or delay until the feature is finished enabling. Will this work? And if so, how can I tell that the feature is finished activating?

# This is a function that enable's a Sharepoint Feature and Sleeps Until Its Finished Enabling

function Feature-Enable

{param ([string]$ID, [string]$URL)

#Enable Feature

Enable-SPFeature -Identity $ID -url $URL -Confirm:$false

#Sleep Until Feature is Done Enabling

}

#Set parameters/variables for script
$serverName = "someServerName"
$rootwebUrl = "someRootUrl"

$currentpath=Split-Path -Path $MyInvocation.MyCommand.Path -Parent

$siteURL = "http://" + $servername + $rootwebURL

$start = Get-Date -DisplayHint Time

# check to ensure Microsoft.SharePoint.PowerShell is loaded

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}

if ($snapin -eq $null) {

Write-Host "Loading SharePoint Powershell Snapin"

Add-PSSnapin "Microsoft.SharePoint.Powershell"

}


#         Active Site Collection Features (in order)

        write-host ""

        write-host "----------------------------------------"

        write-host "Begin activation of site collection features"

        write-host "----------------------------------------"

        write-host ""

  Feature-Enable –identity "3cbf5d1e-ff2e-48d2-82a4-99b060381268" -URL $siteUrl


#NOTE:  POTENTIAL LATENCY ISSUES.   MAY NEED TO INSERT DELAY TIMER!

  Feature-Enable –identity "bbde524e-9293-468e-84f7-fdb763ffa309" -URL $siteUrl

        write-host ""

        write-host "----------------------------------------"

        write-host "Activation of site collection features - DONE!"

        write-host "----------------------------------------"

        write-host ""


$end= Get-Date -DisplayHint Time

Write-Host "Started at: " $start " Ended at:  " $end;
share|improve this question
    
any final solution with full source code final sample? –  Kiquenet Jul 24 at 7:14

1 Answer 1

As far as I know Enable-SPFeature will only continue when feature is enabled (or on error) so you don't need explicity check if it is enabled or not.

Not sure about your latency issues but you can check if your feature is enabled on site scope with:

$site = Get-SPSite "[Your Site Url]"
if (Get-SPFeature -Site $site | Where-Object {$_.Id -eq "[Your feature ID]"}) {
    Write-Host "Feature is enabled"
} else {
    Write-Host "Feature is disabled"
}

But this snippet will always return true if you call it after Enable-SPFeature (I have tried on some simple features)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.