0

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)
4
  • In your code sample you use Out-File -Append, does that not output all errors instead of just the last ? Mar 5, 2014 at 12:36
  • no it doesn't output all. Mar 5, 2014 at 12:40
  • For easier testing, maybe you can just throw some errors intentionally, so you know what errors to expect in the output. Mar 5, 2014 at 12:49
  • i already tried it. Mar 6, 2014 at 4:13

3 Answers 3

5

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.

4
  • 1
    Yupp, that is the better approach. My version is more of a simple hack to get there fast ;) Mar 5, 2014 at 12:37
  • Haha! Fast is not always better, this time ;)! Mar 5, 2014 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. Mar 6, 2014 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. Mar 6, 2014 at 6:21
2

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)

}
1

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
2
  • where I should run MyFancyScriptName.ps1 > C:\file.log ? I am running whole script in powershell directly Mar 5, 2014 at 12:10
  • Exactly where you run your script today (in PowerShell i presume) ;) Mar 5, 2014 at 12:11

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.