0

I am passing an array of 2 elements to my powershell function but I get an error like this:

Execute-Safely : Cannot convert value "w" to type "Microsoft.SharePoint.SPFeatureScope". Error: "Invalid cast from 'System.Char' to 'Microsoft.SharePoint.SPFeatureScope'."
At line:71 char:9

Which is really weird, cant find the error on my code.

param(
    $vars = @{ 
        "SiteUrl" = "https://hello/"
    }
)

function Upgrade-FeaturesInSite {
    param(
        $site,
        [array]$featureUpgradeOrder
    )


    # Feature to upgrade on site in the specified order
    foreach($featureInfo in $featureUpgradeOrder) {
            $featureName = $featureInfo[0]

        [Microsoft.SharePoint.SPFeatureScope] $featureScope = $featureInfo[1]

        #$featuresToUpgradeInSite = $site.QueryFeatures($featureScope, $true)

        #$featuresToUpgradeInSite.Reset() # reset enumerator for next run
        #$featureToUpgrade = $featuresToUpgradeInSite | ? { $_.Definition.DisplayName -eq $featureName }
        #if($featureToUpgrade -ne $null) {
        #    $featureToUpgrade | % { 
        #        try {
        #            #$_.Upgrade($false)
        #            Write-Host -ForegroundColor Green ("{0}:Feature {1} upgraded successfully, version {2}" -f $_.Parent.ServerRelativeUrl, $featureName, $_.Version)
        #        } catch {
        #            Write-Warning -Message ("{0}:Failed to upgrade feature {1}" -f $_.Parent.ServerRelativeUrl, $featureName)
        #            Write-Warning -Message $_.Exception
        #            Write-Warning -Message ("{0}:Will stop upgrading features on this site" -f $site.ServerRelativeUrl)
        #            return
        #        }
        #    }
        #} else {
        #    $featureDefinition = Get-SPFeature -Identity $featureName
        #    $featureInstance = Invoke-Expression ("Get-SPFeature {0} -{1} {2}" -f $featureDefinition.DisplayName, $featureDefinition.Scope, $site.Url)
        #    Write-Host -ForegroundColor DarkGreen ("{0}:Feature {1} was already up to date, FeatureDefinitionVersion: {2} | FeatureInstanceVersion {3}" -f $site.ServerRelativeUrl,$featureName, $featureDefinition.Version, $featureInstance.Version)
        #}
    }
}

function Execute-Safely {
    param(
        [ScriptBlock]$scriptBlock,
        [string]$action
    )

    try {
        Invoke-Command -ScriptBlock $scriptBlock
    } catch {
        Write-Error ("An error occurred when executing [{0}]" -f $action)
        Write-Error $_
    }

}

$SiteUrl = $vars["SiteUrl"]

$featureUpgradeOrder = @(
    @("awfulfeaturename", [Microsoft.SharePoint.SPFeatureScope]::Web)
)

$site = Get-SPSite $SiteUrl -ErrorAction SilentlyContinue
if($site) {
    try {
        Write-Host "Processing" $site.ServerRelativeUrl

        Execute-Safely -scriptBlock { Upgrade-FeaturesInSite -site $site -featureUpgradeOrder $featureUpgradeOrder } -action "Activating site features"

    } catch {
        Write-Warning ("An error occured while processing web {0}" -f $SiteUrl)
        Write-Warning $_.Exception
    } finally {
        $site.Dispose()
    }
}

For me the array has 2 items, one with the feature name and another with the feature scope, but it seems something is wrong

Luis Valencia
  • 32,619
  • 93
  • 286
  • 506

1 Answers1

1

Your array of array's needs a comma. PowerShell is converting it to a single array so you are indexing into your string instead of your feature. The following should force PowerShell to interpret as an array of array's.

$featureUpgradeOrder = @(
    ,@("awfulfeaturename", [Microsoft.SharePoint.SPFeatureScope]::Web)
)

Reference on the array issue: How to create array of arrays in powershell?

beavel
  • 1,077
  • 1
  • 8
  • 16