Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

With The help of this forum I am able to Write The following Script Using powershell which downloads bulk data from SharePoint with Metadata in CSV file and also Error log as txt file. but the problem I am facing here is, it only Copies last error.i want copy of all errors in Txt file.

I tried using

Set-Content "c:\" $error

still not able to solve it.

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

try
{
$destination = "C:\\Test\\"
$web = Get-SPWeb -Identity "http://xyz:4444/"
$list = $web.GetList("http://xyz:4444/Shared Documents/")


function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
        $destinationfolder = $destination + "/" + $folder.Url 
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory 
        }
        #Download file
        $binary = $file.OpenBinary()
        d
        e
        $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
          }
}
$exportlist = @()
$list.Items | foreach {
$obj = New-Object PSObject -Property @{
"Title" = $_["Title"]
"Name" = $_["Name"]
"Modified Date" = $_["Modified"]
"Modified By" =$_["Modified By"]
"Size"= $_["File Size"]
"Path" = $web.Url + "/" + $_.File.Url

}
$exportlist += $obj
$exportlist | Export-Csv -path 'C:\Test\MyList.csv' -noType
}


#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
}
}
catch
{
#Write "Error: copy-item: $_" >>c:\logfile.txt

$("$file.name: " + $_.Exception.Message) | out-file c:\file.log -Append
#set-content "c:\log2.txt"  $error


}
finally
{
#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
}

}
#DownloadMetadata ($list.RootFolder.Url)
share|improve this question
    
In your code sample you use Out-File -Append, does that not output all errors instead of just the last ? –  Cameron Verhelst Mar 5 at 12:36
    
no it doesn't output all. –  SharePointer Mar 5 at 12:40
    
For easier testing, maybe you can just throw some errors intentionally, so you know what errors to expect in the output. –  Cameron Verhelst Mar 5 at 12:49
    
i already tried it. –  SharePointer Mar 6 at 4:13

3 Answers 3

You only see the last error because Set-Content overwrites the existing content, use Add-Content instead.

If you want to have a more encompassing approach, like Robert suggested, there's a more PowerShell'y (?) way of doing this with Start/Stop-Transcript which takes all console output between the Start & Stop cmdlet calls and outputs it to a file.

share|improve this answer
    
Yupp, that is the better approach. My version is more of a simple hack to get there fast ;) –  Robert Lindgren Mar 5 at 12:37
    
Haha! Fast is not always better, this time ;)! –  Cameron Verhelst Mar 5 at 12:48
    
I Tried Using Start-Transcript -path c:\logging.txt -append just above try and used stop transcript right at the bottom.but it doesn't output any file. –  SharePointer Mar 6 at 5:11
    
Can you tell us what error you get when you type the command in a powershell prompt ? You need to have access to the file location (try C:\temp to be sure). Also, you cannot start another transcript before the first was stopped. I'm not sure but when you use -Append, it may be that the file needs to exist first. –  Cameron Verhelst Mar 6 at 6:21

You could use Write-Output "Logging text" in your catches and then when you run the script you can run it as

MyFancyScriptName.ps1 > C:\file.log
share|improve this answer
    
where I should run MyFancyScriptName.ps1 > C:\file.log ? I am running whole script in powershell directly –  SharePointer Mar 5 at 12:10
    
Exactly where you run your script today (in PowerShell i presume) ;) –  Robert Lindgren Mar 5 at 12:11
up vote 1 down vote accepted

Sorry. I corrected My Try catch Block and its seems To be Working Fine

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}


$destination = "C:\\Test\\"
$web = Get-SPWeb -Identity "http://xyz:4444/"
$list = $web.GetList("http://xyx:4444/Shared Documents/")


function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
        $destinationfolder = $destination + "/" + $folder.Url 
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory 
        }
        #Download file
        try
        {

        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
         }
        catch
        {
        #Add-content "c:\log3.txt"  $error
        Write "Error: $file.name: $_" >>c:\logfile.txt
        continue;
        }

        }
}

$exportlist = @()
$list.Items | foreach {
$obj = New-Object PSObject -Property @{
"Title" = $_["Title"]
"Name" = $_["Name"]
"Modified Date" = $_["Modified"]
"Modified By" =$_["Modified By"]
"Size"= $_["File Size"]
"Path" = $web.Url + "/" + $_.File.Url

}
$exportlist += $obj
$exportlist | Export-Csv -path 'C:\Test\MyList.csv' -noType
}


#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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