0

I am still learning Powershell and am having problems modifying to script below. I found this at http://bhatiaashish.blogspot.com/2012/08/powershell-script-to-download.html.

This script works but I would like to only grab attachments if item has a category of "red". I have tried adding the piping on line startin $l but must be understanding something wrong. Also the folder this saves the attachments to is named "System.Object Item(int index) {get;set;}, System.Object Item(string fieldName) {get;set;}, System.Object Item(System.Guid fieldId) {get;set;}" How could I change script to create the folder with same name as the list?

# Add SharePoint Snapin to PowerShell            
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)     {            
Add-PSSnapin Microsoft.SharePoint.PowerShell            
}  

$webUrl = "http://testsite"    
$library = "testlist"   
#Local Folder to dump files
$tempLocation = "c:\TestAttachments"       
$s = new-object Microsoft.SharePoint.SPSite($webUrl)    
$w = $s.OpenWeb()         
$l = $w.Lists[$library] | ?{$_.Item["category"] -eq "red"}


foreach ($listItem in $l.Items)
{
 Write-Host "    Content: " $listItem.Item
  $destinationfolder = $tempLocation + "\" + $listItem.Item          
   if (!(Test-Path -path $destinationfolder))        
    {            
     $dest = New-Item $destinationfolder -type directory          
    }
 foreach ($attachment in $listItem.Attachments)    
     {        
         $file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)        
         $bytes = $file.OpenBinary()                
         $path = $destinationfolder + "\" + $attachment
        Write "Saving $path" 
        $fs = new-object System.IO.FileStream($path, "OpenOrCreate")
        $fs.Write($bytes, 0 , $bytes.Length)    
        $fs.Close()    
    }

}

Thanks!

2

To get items with Category=Red try this instead:

$l = $w.Lists[$library].Items | ?{$_.Item("category") -eq "red"}

To create the folder with the same name as the list try this:

$destinationfolder = $tempLocation + "\" + $listItem.ParentList.Title 
  • Getting some errors now like: Content: Exception calling "GetFile" with "1" argument(s): "Value does not fall within t he expected range." At C:\ps\downloadattachments.ps1:24 char:32 + $file = $w.GetFile <<<< ($listItem.Attachments.UrlPrefix + $attachment) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException – Chris May 13 '14 at 14:37
0

I added it to for each like this and it saved the correct items

 foreach ($listItem in $l.Items | ?{$_.Item("category") -eq "red"})

I think my original code is wrong. I'm still trying to wrap my head around this so feel free to clarify, but I think the piping is failing because $l = $w.Lists[$library] is looking at the properties at a list level and therefore doesn't have access to fields for each item in that list. By adding the piping to the $listitem, we are now looking at the fields at the "item" level.

That would also explain my previous error, because it would be looking for a "list level" property of category which doesn't exist.

The code to fix folder name by MLF worked as well.

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.