none
Upload csv to SharePoint document library via Powershell error: Exception calling "Add" with "3" argument(s): "I/O error occurred." RRS feed

  • Question

  • Hi,

    This is SharePoint related: 

    I am using below script to get all site collection owners and put that in a csv and upload the csv to a document library in SharePoint. 

    But it gives me this error when i execute it:

    Exception calling "Add" with "3" argument(s): "I/O error occurred."
    At line:59 char:1
    + $spFile = $Folder.Files.Add($FileUrl, $FileStream, $true)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : IOException

    Any pointers?

    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    }
    Push-Location $PSScriptRoot

    [string] $SiteUrl = "http://contoso/"
    [string] $ListUrl = "http://conoso/test/Forms/AllItems.aspx"
    [string] $DocLibName = "test"

    #Get-SPWebApplication $SiteUrl | Get-SPSite -Limit All | Get-SPWeb -Limit All | Select Title, URL, ID, ParentWebID

    $webapp = Get-SPWebApplication $SiteUrl
    $sites = $webapp | Get-SPSite -Limit All
    $webs = $sites | Get-SPWeb -Limit All

    $siteOwersCollection = @()

    #Loop throuh all Sub Sites
    foreach ( $Web in $webs) {
        foreach ($ownerGroup in $web.AssociatedOwnerGroup) {
            foreach ($owner in $ownerGroup.Users) {
                $newItem = New-Object -TypeName PSObject
                $newItem | Add-Member -Type NoteProperty `
                    -Name SiteUrl -Value $web.url
                $newItem | Add-Member -Type NoteProperty `
                    -Name SiteOwnerID -Value $owner
                $newItem | Add-Member -Type NoteProperty `
                    -Name SiteOwnerDisplayName -Value ($owner.DisplayName)
                $newItem | Add-Member -Type NoteProperty `
                    -Name SPGroup `
                    -Value ($ownerGroup)

                $siteOwersCollection += $newItem
            }
        }
    }


    $fileName = "SiteOwners_{0:G}.csv" -f [int][double]::Parse((Get-Date -UFormat %s))
    $siteOwersCollection | Export-Csv -Path $fileName


    $MasterSite = (Get-SPSite $SiteUrl)
    $MasterWeb = $MasterSite.OpenWeb()
    $docLibrary = $MasterWeb.Lists[$DocLibName] 
    $Folder = $MasterWeb.GetFolder($docLibrary.RootFolder.Url)
    write-host "folder is:" $Folder 
    Write-host "Filecount" $Folder.Files.Count
    Write-Host $docLibrary.Title
    Write-Host $docLibrary.RootFolder.Url
    Write-Host  "url is" $Folder.Url
    $FileStream = ([System.IO.FileInfo](Get-Item $fileName)).OpenRead()
    $contents = new-object byte[] $FileStream.Length
    $FileStream.Read($contents, 0, [int]$FileStream.Length);
    $FileStream.Close(); 
    $FileUrl = $DocLibName + "/" + $fileName
    write-host "File URL" $FileUrl 
    Write-Host $FileStream
    $spFile = $Folder.Files.Add($FileUrl, $FileStream, $true)
    $FileStream.Close()
    $Web.Dispose()

    Friday, June 1, 2018 7:22 PM

Answers

All replies