powershell - 将文件作为系统帐户上传到 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),我会遇到同样的问题,我无法修改相同的元数据
解决方案
您可以使用“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"}
推荐阅读
- variables - 何时使用 $path、$dir 或 $file 作为变量名
- sql - 如何从 Oracle 中的日期时间戳中删除日期?
- php - 登录后更改页面
- memory-management - 在特定内核上运行的进程
- python - 无法在 VS Code 中选择虚拟环境
- typescript - 具有多种类型的函数声明
- java - C# 类比 java 流操作
- amazon-web-services - 在 AWS EC2 实例上以开发模式运行 Sails 项目时出错
- php - 当我们从特定路径复制并保存新的 pptx 时,PHPPresentation 会丢失样式
- java - 是否有必要使用 jedis.close()