1

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()
  • You're saying you get the expected results from the script, other than the error message appearing? – KGlasier Dec 19 '18 at 15:25
  • It's almost like your list doesn't have any SiteContents. Can you add the following before your IF condition that's causing an issue? if(!$list.ContentTypes){ Write-Host $($list.Title), $($list.DefaultViewUrl) }. This should let you know if the list's attribute "ContentTypes" doesn't have any value, and what the list and relative url is. – KGlasier Dec 19 '18 at 15:32
  • yes the update command executes for the correct records. – Ruby Trustit Dec 19 '18 at 15:43
0

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.

  • Issue still stands. Let me do more tinkering and see if it helps. – Ruby Trustit Dec 19 '18 at 16:14
  • @RubyTrustit Is the error happening on every Document Library? – KGlasier Dec 19 '18 at 16:16
  • I think it's tapping on hidden libraries because I put write-host $list.Title and those 3 errors are in the middle of M and O. It may be hidden Nintex document libraries which starts with N. – Ruby Trustit Dec 19 '18 at 16:23
  • @RubyTrustit You could try to see if !list.Hidden works, instead of "!$list.ContentTypes" in my above answer. – KGlasier Dec 19 '18 at 16:42
  • tried variety !($list.Hidden) or $list.Hidden -eq $False but no luck. Dont worry it's really not a big deal. Thanks for all your help – Ruby Trustit Dec 19 '18 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.