Question

I have a Sharepoint directory that I connected to Outlook. A csv gets posted to this directory each day, and I can grab it from Outlook instead of navigating to the Sharepoint site.

I need to write a Powershell script that will pull the most recent file from this Sharepoint List in Outlook, and dump it into a local dir.

I cannot pull directly from Sharepoint because I don't have admin rights. I also cannot pull directly from the dir that feeds into Sharepoint because I don't have rights to that dir. This must be done through Outlook.

I thought I would be able to do this the same way I pull email attachments from Outlook, but my script does not work:

#file path
$filepath = "I:\WriteToHere"
$account = "mail@company.com"    

#set outlook to open
$o = New-Object -comobject outlook.application
$n = $o.GetNamespace(“MAPI”)

$Account = $n.Folders | ? { $_.Name -eq $account };
    $Inbox = $Account.Folders | ? { $_.Name -match 'Root Folder' };
    $Data = $Inbox.Folders | ? { $_.Name -match 'Sub1' };
    $Year = $Data.Folders | ? { $_.Name -match 'Sub2' };
    $Month = $Year.Folders | ? { $_.Name -match 'Sub3' };

$email = $Month.Items| Sort-Object ReceivedTime -Descending | Select-Object -First 1

# Log the email we are looking for, and mention attachments if they exist.
Write-Output "Last email received at $($email.receivedtime), attached file(s) are: (if any)"
$email.attachments|%{Write-Output $_.filename}

# If the email has at least one attachment, save them to the filepath.
If($email.attachments.count -gt 0){
    $email.attachments|%{$_.saveasfile((join-path $filepath $_.filename))} 
} else { 
    Write-Output "Latest email at $($email.receivedtime) has no attachments!"
}

Note - the Sharepoint items in my inbox are just the csv files. They are not messages w/ attachments.

I assume I can't treat these Sharepoint items the same way I treat emails. I will dig around for a solution, but I wanted to post this in case someone can point me in the right direction.

Était-ce utile?

La solution

Ok, getting the items through Outlook... totally doable!

#file path
$filepath = "I:\WriteToHere"

#set outlook to open
$o = New-Object -comobject outlook.application
$n = $o.GetNamespace(“MAPI”)

$SPL = $n.Folders | ? { $_.Name -eq "SharePoint Lists" }

Now here's where it takes a bit of work on your part. You will need to find the folder that you are looking for. Do this:

$SPL.Folders|Select Name

Do you see your folder listed where the file can be found? Is it a nested folder? If it is in something like:

\\SharePoint List\Document Library\Daily Files\Lists\04032014.csv

You will have to drill down to the folder it is in. So, here we go with an example for that:

$DocLib = $SPL.Folders|?{$_.Name -match "Document Library"}
$Dailies = $DocLib.Folders|?{$_.Name -match "Daily Files"}
$Lists = $Dailies.Folders|?{$_.Name -match "Lists"}

$TargetFile = $Lists.Items|?{$_.Subject -match "04032014.csv"}
If($TargetFile.DownloadState -eq 1){$TargetFile.SaveAs("$FilePath\$($TargetFile.Subject)")}

So I got the Document Library object, from that I got the Daily Files object, from that I got the Lists object. That gets us to the right folder at last. Once we have the folder we get the Items from it and on each item we get the Subject property which is where it stores the file name, and we filter that for the desired file. Once we have the Item object I checked to see if it was downloaded from the server yet by checking DownloadState. 0 = not downloaded yet, and 1 = downloaded. I suppose we could do something like:

$TargetFile = $Lists.Items|?{$_.Subject -match "04032014.csv"}
If($TargetFile.DownloadState -eq 0){
    do{start-sleep 1}while{$TargetFile.DownloadState -eq 0}
}
$TargetFile.SaveAs("$FilePath\$($TargetFile.Subject)")

That would check and see if it's downloaded, and if not it'll wait until it is downloaded, and then save the file.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top