首页 > 解决方案 > 将文件作为系统帐户上传到 SharePoint Online

问题描述

如何通过 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),我会遇到同样的问题,我无法修改相同的元数据

标签: powershelloffice365sharepoint-online

解决方案


您可以使用“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"}

推荐阅读