3

I made this script :

$webUrl = "http://site.fr/"    
$library = "Qualified Information"
$spSite = new-object Microsoft.SharePoint.SPSite($webUrl)
$w = $spSite.OpenWeb()
$l = $w.Lists[$library]
$resultHashtable = @{}

foreach ($listItem in $l.Items)
{     
    Write-Host "    Content: " $listItem.ID 
    foreach ($attachment in $listItem.Attachments)
    {
        $file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)
        $linkAttachment = "http://site.fr" + $file.ServerRelativeUrl
        Write-Host "http://site.fr"$file.ServerRelativeUrl
        if($linkAttachment){
        $resultHashtable.Add($listItem.ID, $linkAttachment)
        }    
    }    
}
#Export CSV
$resultHashtable.GetEnumerator() | Sort-Object -Property Name -Descending |
Select-Object -Property @{n='SiteURL';e={$_.Name}},Value |
Export-Csv -Path Attachments.csv -NoTypeInformation

With SharePoint 2013 it works but with SharePoint 2010, my script doesn't get the list :

Exception calling "GetFile" with "1" argument(s): "Value does not fall within the expected range."

If I debug $l, it's null

My Write-Host Content: $listItem.ID just returns : Content: My list is full of items with attachments, do you have any ideas?

1
  • what happen if you debug the $w, are you getting the correct site/web?
    – Waqas Sarwar MVP
    Commented May 22, 2015 at 12:22

2 Answers 2

2
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction      
SilentlyContinue
$webUrl = "http://localsite"
$library = "ListName" 
#Local Folder to dump files
$tempLocation = "C:\Users\Documents\MyDownloadFolder"    
$s = new-object Microsoft.SharePoint.SPSite($webUrl)    
$w = $s.OpenWeb()         
$l = $w.Lists[$library]  

Write-Host "Connecting To Site: " $webUrl   

foreach ($listItem in $l.Items)
 {
  Write-Host "    Current Item ID: " $listItem.ID 

  #create folder name for each list item by using title and year column  
   value combination 

  $folderName=$listItem["Title"]+"_"+$listItem["Year"]

  $destinationfolder = $tempLocation + "\"+ $folderName  

  #check if folder is exist or not, if not exist then create new

  if (!(Test-Path -path $destinationfolder))        
   {            

     $dest = New-Item $destinationfolder -type directory      
     Write-Host "Created Folder with Name:" $folderName    
   }

  #Code to download list attachments

 if($listItem.Attachments -ne $null)
  {
   Write-Host "Downloading Attachements started: " 
  Try
   {
    foreach ($attachment in $listItem.Attachments)    
    {  
     Write-Host "Downloading Attachement: " $attachment      
     $file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)        
     $bytes = $file.OpenBinary()                
     $path = $destinationfolder + "\" + $attachment
     Write "Saving to the loction  $path"
     $fs = new-object System.IO.FileStream($path, "OpenOrCreate") 
     $fs.Write($bytes, 0 , $bytes.Length)    
     $fs.Close()    

    }
   }
    Catch{}

  }
  else
  {
  Write-Host   "For above current item don't have any attachments" 
  }
}


     Write-Host   "Script execution done !" 
0

Make sure you are using correct ListName and it exists in the Site. Try below

$webUrl = "http://site.fr/"    
$w = get-spweb $webUrl
$listName = "Qualified Information"
$l = $w.Lists[$listName]

write-host $l.Title
3
  • Are you executing this from SP 2010 Management Shell and from within the server? Commented May 22, 2015 at 12:23
  • PS C:\Users\devinfo\Desktop> $l = $w.Lists[$listName] Cannot index into a null array. At line:1 char:15 + $l = $w.Lists[ <<<< $listName] + CategoryInfo : InvalidOperation: (Qualified Information:String) [], RuntimeException + FullyQualifiedErrorId : NullArray Commented May 22, 2015 at 12:25
  • $w.Lists is null, it didn't get any list Commented May 22, 2015 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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