1

Im having issues attaching files to multiple items using powershell.

With one item it works. my snipet code below

Thanks

#When i run the script and having to pass one item it works but not with multiple items
function AddListAttachment([string] $itemid)
   {

    $CMRSItems = $list.Items | where {$_['ID'] -le $itemid}
    $CMRSItems | ForEach-Object {

    $id =  $_['RefID']
    $localFolderPath = "D:\CMRSDocs\" + $id

    $files = [IO.Directory]::GetFiles($localFolderPath)
        Write-Host $id



    ForEach($file in $files)
    {

    $bytes = [System.IO.File]::ReadAllBytes($file)
    #getting error here :but getting error :You cannot call a method on a null-valued expression.

    $CMRSItems.Attachments.Add([System.IO.Path]::GetFileName($file), $bytes)

     #I can see each files of the $id here          #Write-Host $file
    }

   }
         Write-Host $file
    $CMRSItems.Update()
    $web.Dispose()

  }
  • The logic seems that when you pass an item id, you get all items less than or equal to id and then attach files to them. So what is the issue? – Nadeem Yousuf Mar 28 '14 at 4:56
  • Nadeem when i do e.g equal to 1 it works fine but when i pass less than 4(which mean this will return 4 items it doesn't work).I can't get it to iterate though each id items(which is the folder path) and then attach the appropriate files in that folder.Cheers – naijacoder Mar 28 '14 at 5:11
  • Are you saying that if you change $CMRSItems = $list.Items | where {$_['ID'] -le $itemid} to $CMRSItems = $list.Items | where {$_['ID'] -eq $itemid} it works and not when you use -le – Nadeem Yousuf Mar 28 '14 at 5:32
  • Yes Nadeem.Im trying to loop through each item in a list with id e.g 50.My folder structure has each folder with the id name which is 50 so e.g "D:\CMRSDocs\50" Hope this helps.Thanks – naijacoder Mar 28 '14 at 6:08
  • What do you mean by loop through each item with id 50? There can only be one item with item id. Item id is unique. – Nadeem Yousuf Mar 28 '14 at 6:12

Your Answer

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

Browse other questions tagged or ask your own question.