Pregunta

I have queried AllDocs table using Powershell and can see GUID values of Id, SiteId and WebId, But I am getting Error: You cannot call a method on Null valued expression at this line below: $File = $Web.GetFile([Guid]$Result.Id)

$Query = "SELECT * from AllDocs where SetupPath like '"+$SetupFile+"'"
$QueryResults = @(Run-SQLScript -SQLServer $Server -SQLDatabase $Database -SQLQuery $Query | select Id, SiteId, WebId)

write-host $QueryResults.Id -foregroundcolor green  // I can see GUID
write-host $QueryResults.SiteId -foregroundcolor green // I can See GUID
write-host $QueryResults.WebId -foregroundcolor green // I can see GUID

#Iterate through results
foreach ($Result in $QueryResults)
{
    if($Result.Id -ne $Null)
    {
        $Site = Get-SPSite -Limit all | where { $_.Id -eq $Result.SiteId }
        $Web = $Site | Get-SPWeb -Limit all | where { $_.Id -eq $Result.WebId }       

        #Get the URL of the file which is referring the feature
        $File = $Web.GetFile([Guid]$Result.Id)   // Error on this line
        write-host "$($web.URL)/$($File.Url)" -foregroundcolor green

        #$File.delete()
    }
}

What's wrong?

¿Fue útil?

Solución

Lets look at the error message first:

You cannot call a method on Null valued expression at: $File = $Web.GetFile([Guid]$Result.Id)

The only method you call is .GetFile(), and it looks like that you're calling it on a Null object. Try validating that you have a real SPWeb in the $Web variable.

Or try this, using those IDs instead of using Where-Object:

#Iterate through results
foreach ($Result in $QueryResults)
{
    if($Null -ne $Result.Id)
    {
        $Site = Get-SPSite -Identity $Result.SiteId
        $Web = Get-SPWeb -Identity $Result.WebId -Site $Site

        #Get the URL of the file which is referring the feature
        $File = $Web.GetFile([Guid]$Result.Id)
        Write-Host "$($web.URL)/$($File.Url)" -Foregroundcolor Green

        #$File.Delete()
    }
}
Licenciado bajo: CC-BY-SA con atribución
scroll top