【问题标题】:grab most recently-received file from SharePoint List in Outlook从 Outlook 中的 SharePoint 列表中获取最近收到的文件
【发布时间】:2014-05-15 14:14:39
【问题描述】:

我有一个连接到 Outlook 的 Sharepoint 目录。每天都会有一个 csv 发布到这个目录,我可以从 Outlook 中获取它,而不是导航到 Sharepoint 站点。

我需要编写一个 Powershell 脚本,该脚本将从 Outlook 中的 Sharepoint 列表中提取最新文件,并将其转储到本地目录中。

我无法直接从 Sharepoint 中提取,因为我没有管理员权限。我也不能直接从提供给 Sharepoint 的目录中提取,因为我没有该目录的权限。这必须通过 Outlook 完成。

我认为我可以像从 Outlook 中提取电子邮件附件一样执行此操作,但我的脚本不起作用:

#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!"
}

注意 - 我收件箱中的 Sharepoint 项目只是 csv 文件。它们不是带附件的消息。

我认为我不能像对待电子邮件一样对待这些 Sharepoint 项目。我会四处寻找解决方案,但我想发布这个以防有人能指出我正确的方向。

【问题讨论】:

  • @HAL9256 - 为什么不将其标记为 ETL?
  • 服务器运行的是什么版本的 SharePoint?
  • Sharepoint 2007。我找到了另一种获取 csv 的方法:我通过 Access 链接到 SharePoint 列表,并使用 WinHttpReq 通过提供其 URL 来下载文件。但是,如果您知道使用 PowerShell 从 Outlook 获取文件的方法,我仍然很想知道。

标签: sharepoint powershell outlook etl


【解决方案1】:

好的,通过 Outlook 获取项目...完全可行!

#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" }

现在您需要做一些工作。您将需要找到您要查找的文件夹。这样做:

$SPL.Folders|Select Name

您是否看到列出了可以找到该文件的文件夹?它是一个嵌套文件夹吗?如果是这样的:

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

你必须深入到它所在的文件夹。所以,我们在这里举一个例子:

$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)")}

所以我得到了 Document Library 对象,从中得到了 Daily Files 对象,从中得到了 Lists 对象。这最终让我们找到了正确的文件夹。一旦我们有了文件夹,我们就可以从中获取项目,并且在每个项目上,我们都会获得存储文件名的主题​​属性,然后我们将其过滤为所需的文件。一旦我们有了 Item 对象,我通过检查 DownloadState 来检查它是否已从服务器下载。 0 = 尚未下载,1 = 已下载。我想我们可以这样做:

$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)")

这将检查它是否已下载,如果没有,它将等到下载完毕,然后保存文件。

【讨论】:

  • +1,非常感谢您。我会测试并告诉你它是否有效。
猜你喜欢
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
相关资源
最近更新 更多