【问题标题】:Cannot convert value "w" to type "Microsoft.SharePoint.SPFeatureScope" powershell无法将值“w”转换为类型“Microsoft.SharePoint.SPFeatureScope”powershell
【发布时间】:2018-06-17 11:15:46
【问题描述】:

我将一个包含 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 个项目,一个带有功能名称,另一个带有功能范围,但似乎有问题

【问题讨论】:

    标签: arrays powershell sharepoint


    【解决方案1】:

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

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

    数组问题参考:How to create array of arrays in powershell?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多