【问题标题】:Upload files to SharePoint Online as System Account将文件作为系统帐户上传到 SharePoint Online
【发布时间】:2018-06-23 04:50:49
【问题描述】:

如何通过 PowerShell 上传文件创建文件夹到 SharePoint Online,同时将其元数据(修改者)保留为 “系统帐户”而不是在元数据上使用我的帐户来保存文件?

我知道我可以通过这样做来更改文件的元数据:

$user = Get-PnPUser -ErrorAction Stop | ? Email -eq $email 

$newFile["Author"]   = $user.id
$newFile["Editor"]   = $user.id
$newFile["Modified"] = $currentFile.LastWriteTimeUtc
$newFile["Created"]  = $currentFile.CreationTimeUtc

$newFile.Update()

但是文件夹呢?如果我将 CSOM 与 PowerShell 一起使用(见下文),我找不到修改“作者”和“编辑者”元数据属性的方法:

$CSOM_credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password)
$CSOM_context = New-Object Microsoft.SharePoint.Client.ClientContext("https://tenanturl.sharepoint.com")
$CSOM_context.Credentials = $CSOM_credentials

$ParentFolder = $CSOM_context.web.GetFolderByServerRelativeUrl("Samples")

$newFolder = $ParentFolder.Folders.Add("New Folder")
$ParentFolder.Context.ExecuteQuery()

$CSOM_context.Load($newFolder)
$CSOM_context.ExecuteQuery()

如果我使用 PnP PowerShell (Add-PnPFolder),我会遇到同样的问题,我无法修改相同的元数据

【问题讨论】:

  • 您可以使用“服务帐户”来执行任务吗?
  • 我该怎么做?我们在谈论 SPO 而不是 SP on-prem,可以详细说明吗?我不确定我是否关注
  • 您需要在 Office365 中创建一个帐户以用作服务帐户,然后在运行 SPO 命令之前使用这些凭据连接到 SPO。您有任何代码可以分享,我们可以提供帮助吗?
  • 您能否根据此博客中的信息分享您遇到问题的代码 - petri.com/…
  • 我知道为文件保留元数据的技巧,但更准确地说,如何保留包括文件夹在内的所有内容的元数据,你提到的解决方案不包括文件夹的问题,我会用一个例子更新我的问题

标签: powershell office365 sharepoint-online


【解决方案1】:

您可以使用“SHAREPOINT\System”将作者和编辑者设置为系统帐户。

$Id = 1024
$item = Get-PnPListItem -List "listNmae" -Id $Id
Set-PnPListItem -List "listNmae" -Identity $Id -Values @{"Author" = "SHAREPOINT\System";
"Editor" = "SHAREPOINT\System";
"Modified" = $item.FieldValues.Modified 
} 

您也可以使用系统帐户用户

$sysaccount = Get-PnPUser | ? {$_.LoginName -eq "SHAREPOINT\System"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    相关资源
    最近更新 更多