Question

I have a powershell script which works but at the same time it throws this error.

Cannot index into a null array.
At D:\PowerShell\Items\Update_a_Field.ps1:10 char:69
+     if($list.BaseType -eq "DocumentLibrary" -and $List.ContentTypes[ <<<< "Company Standard"])
    + CategoryInfo          : InvalidOperation: (Exterran Standard:String) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

here is the powershell

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$siteURL = "https://inside.nov.hou/CORP/STD"
$site = Get-SPSite($siteURL)
foreach($web in $site.AllWebs) 
{
  foreach($list in $web.Lists) 
  {
    if($list.BaseType -eq "DocumentLibrary" -and $List.ContentTypes["Company Standard"])
    {   
        foreach ($item in $list.Items)
        {
            #if ($item.File.CheckOutStatus -ne "None") # DO NOT USE THIS LINE
            if ($item.File.CheckOutStatus -eq "None")
            {
                 # write-host $item.File.CheckedOutByUser
                #if($item.File.CheckedOutByUser -like "SHAREPOINT\system")
                #{
                    <#if($item.File.Versions) # -ne $item.File.VersionText)
                    {
                        write-host $item.id "-" $item.Name " - " $item.Vesion " - " $item.VersionText                       
                    }
                    #>
            #}
            #foreach($version in $item.Versions) 
            #{ 
                #if($version.Level -eq "Draft")

                if($item["_UIVersionString"] -ne $item["VersionText"])
                {
                #if($item["Name"] -eq "COM-PRC-0003.docx")
                #{
                    write-host $item.Name " - " $item["VersionText"] " - " $item["_UIVersionString"]
                    $item["VersionText"] = $item["_UIVersionString"]
                    $item.SystemUpdate()
                #   write-host $item.Name " - " $item["VersionText"] " - " $item["_UIVersionString"]
                #   write-host $item.Title $item.Versions[0].VersionLabel
                #   write-host $item.Versions[0].VersionLabel
                #   $item.File.ReleaseLock($item.File.LockId)
                #   $item.file.UndoCheckOut()

                #}
                }   

            }
        }
    }
  }
$web.Dispose()
}

$site.Dispose()
Was it helpful?

Solution

By the sounds of it, you have some document libraries that don't have any content types. To me that seems like an odd behaviour. It's that, or PowerShell doesn't exit the conditional statement after $list.BaseType -eq "DocumentLibrary" is $false and you have other types of lists that don't have content types.

You said that you get the expected result, so you can just screen for "null content types" by modifying your conditional statement to below:

if($list.BaseType -eq "DocumentLibrary" -and 
   $list.ContentTypes -and
   $list.ContentTypes["Company Standard"])
{   
    # ...
}

This checks to make sure the field exists and is not $null, then continues the execution.


As we were unable to figure out the issue, exactly, you could just mute it with an $ErrorActionPreference call. It's bad practice, imo, but it does work.

$prevErrorActionPreference = $ErrorActionPreference 
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
if($list.BaseType -eq "DocumentLibrary" -and $List.ContentTypes["Company Standard"])
{  
    $ErrorActionPreference = $prevErrorActionPreference
    # ...
}
$ErrorActionPreference = $prevErrorActionPreference

The $ErrorActionPreference is a special variable that's referenced when an error occurs. You can use the text equivalent (ie `"SilentlyContinue" instead of the .NET Enum). So for just the if-condition it should mute the error, then get reverted to the previous statement.

The call to return to the previous was in case the if-condition is skipped.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top