无法转换价值" w"键入" Microsoft.SharePoint.SPFeatureScope"的powershell

时间:2018-01-08 13:06:06

标签: arrays powershell sharepoint

我将2个元素的数组传递给我的powershell函数,但是我得到了这样的错误:

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

这真的很奇怪,无法在我的代码中找到错误。

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()
    }
}

对我来说,数组有2个项目,一个具有功能名称,另一个具有功能范围,但似乎出现了问题

1 个答案:

答案 0 :(得分:1)

您的数组数组需要逗号。 PowerShell正在将其转换为单个数组,因此您将索引到字符串而不是功能。以下内容应强制PowerShell解释为数组的数组。

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

关于阵列问题的参考:How to create array of arrays in powershell?