Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 无法将值“w”转换为类型“Microsoft.SharePoint.SPFeatureScope”powershell_Arrays_Powershell_Sharepoint - Fatal编程技术网

Arrays 无法将值“w”转换为类型“Microsoft.SharePoint.SPFeatureScope”powershell

Arrays 无法将值“w”转换为类型“Microsoft.SharePoint.SPFeatureScope”powershell,arrays,powershell,sharepoint,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 这真的很奇怪,在我的代码中找不到错

我正在将一个包含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()
    }
}

对我来说,数组有两个项,一个带有功能名称,另一个带有功能范围,但似乎有点不对劲

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

$featureUpgradeOrder = @(
    ,@("awfulfeaturename", [Microsoft.SharePoint.SPFeatureScope]::Web)
)
有关阵列问题的参考: