11

Im trying to find active futures for my site from below script but im prompt with error ( cannot call a method on a null-valued expression) Appreciate your help

$activeFeatures = Get-SPFeature -Site --site _URL--
$allSiteFeatures | ForEach-Object 
{
$active = $false
    foreach($feature in $activeFeatures)
    {
      if($feature.Id.ToString() -eq $_.Id.ToString())
      {
         $active = $true
      }
    }
    if($active)
    {
       $_.DisplayName
    }
}

ERROR----------- You cannot call a method on a null-valued expression. //At line:5 char:51 //+ if($feature.Id.ToString() -eq $_.Id.ToString <<<< ()) // + CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeE xception + FullyQualifiedErrorId : InvokeMethodOnNull

2 Answers 2

18

Why are you looping instead of just getting the active items and returning it? Look below, I have added a few other possible usages.

LIST ALL INSTALLED FEATURES - using Get-SPFeature

(Feature Alphabetically)

Get-SPFeature | Sort -Property DisplayName

(Feature ID)

Get-SPFeature | Sort -Property Id

(Alphabetically with Scope)

Get-SPFeature | Sort -Property Scope,DisplayName | FT -GroupBy Scope DisplayName,Id

(Export for Excel or such)

Get-SPFeature | Sort -Property Scope,DisplayName | FT -GroupBy Scope DisplayName,Id > c:\PATH\TO\WHATEVER.txt

(Activated Scoped Features - useful for hidden features)

Get-SPFeature -Site http://sitecollectionurl | Sort DisplayName | FT DisplayName,Id

(Web Scoped Features)

Get-SPSite http://SC-URL | Get-SPWeb -Limit ALL | %{ Get-SPFeature -Web $_ } | Sort DisplayName -Unique | FT DisplayName,Id

If you are parsing so you can figure out which items are active so you don't initiate an activate attempt, you are better off looking for what is NOT active and then initiate what you want. Just a suggestion, see code below.

Additional code for detecting inactive features:

$siteFeatures = Get-SPFeature | Where-Object {$_.Scope -eq "Site" } # Farm, WebApp, Site and Web
if ($siteFeatures -ne $null)
{
   foreach ($feature in $siteFeatures)
   {
      # -Site can be replace by -Farm (without url), -WebApp, -Web
      if ((Get-SPFeature -Site "http://URL/" | Where-Object {$_.Id -eq $feature.id}) -eq $null)
      {
         # Inactive feature
         Write-Host "$($feature.Scope) feature $($feature.DisplayName) " -ForeGroundColor Red
      }
   }
}
0
2

You need to use Get-SPFeature , using Get-SPFeature with -site or -web will do it for you ;)

Here is a snippet of Windows PowerShell code to list all the activated Features for a site collection (SPSite):

Get-SPSite http://sharepoint2010 | % {

    $results = @()

    Get-SPFeature -Site $_ -Limit All | % {

    $feature = $_; 
        $featuresDefn = (Get-SPFarm).FeatureDefinitions[$_.ID]; 
        $cc = [System.Globalization.CultureInfo]::CurrentCulture;

        $obj = New-Object PSObject;
        $obj | Add-Member NoteProperty  Title  $($featuresDefn.GetTitle($cc));
        $obj | Add-Member NoteProperty  Hidden $($feature.Hidden);

        $results += $obj;
    }
    $results | FT -auto;
}

Here is a snippet of Windows PowerShell code to list all the activated Features for an individual web (SPWeb).

Get-SPWeb http://sharepoint2010/subsite | % {

    $results = @()

    Get-SPFeature -Web $_ -Limit All | % {

        $feature = $_; 
        $featuresDefn = (Get-SPFarm).FeatureDefinitions[$_.ID]; 
        $cc = [System.Globalization.CultureInfo]::CurrentCulture;

        $obj = New-Object PSObject;
        $obj | Add-Member NoteProperty  Title  $($featuresDefn.GetTitle($cc));
        $obj | Add-Member NoteProperty  Hidden $($feature.Hidden);

        $results += $obj;
    }
    $results | FT -Auto;
}

http://blogs.msdn.com/b/josrod/archive/2013/01/04/powershell-to-list-activated-features-for-a-site-and-web.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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