如何更新SharePoint上的.jpg

时间:2017-09-07 13:28:23

标签: powershell sharepoint webrequest

我有一个旧脚本,每天在我们的SharePoint上更新一次图片。 在升级到SharePoint 365之前,脚本执行的操作是使用网络摄像头制作照片,在本地保存照片,然后将照片上载到SharePoint。它工作正常。

但是,由于我们升级到SharePoint 365,最后一步不再起作用了。

这是将其上传到SharePoint的旧代码。

# Upload to Sharepoint
[System.Management.Automation.PSCredential] $SharepointCredentials = New-Object System.Management.Automation.PSCredential ($SharepointUser, ($SharepointPassword | ConvertTo-SecureString -AsPlainText -Force) )  
Invoke-WebRequest -Uri $SharepointUrl -Method Put -InFile "$Path\webcam.jpg" -ContentType 'image/jpg' -Credential $SharepointCredentials -UseBasicParsing | Out-Null

我认为问题出在Invoke-WebRequest上,而Put已经不再有用了。您知道是否有更新或更新文件的新方法吗?

2 个答案:

答案 0 :(得分:0)

本文介绍如何使用CSOM通过PowerShell将文件上载到Office 365 SharePoint库。希望这会让你走上正轨。

https://blogs.technet.microsoft.com/fromthefield/2014/02/19/office-365-powershell-script-to-upload-files-to-a-document-library-using-csom/

答案 1 :(得分:0)

Invoke-WebRequest cmdlet中的

Credentials parameter不支持基于SharePoint Online 声明的身份验证,它仅支持身份验证方案,如基本,摘要,NTLM和Kerberos身份验证。

使用用户凭据SharePointOnlineCredentials class在SharePoint Online中执行经过身份验证的请求可以使用,如下所示:

function Connect-SPO ([string] $Username, [string]$Password, [string]$Url) {

    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $(ConvertTo-SecureString -AsPlainText $Password -Force))
    $Context.ExecuteQuery()

    $AuthenticationCookie = $Context.Credentials.GetAuthenticationCookie($Url, $true)
    $WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $WebSession.Credentials = $Context.Credentials
    $WebSession.Cookies.SetCookies($Url, $AuthenticationCookie)

    $FormsDigest = $Context.GetFormDigestDirect()
    $WebSession.Headers['X-RequestDigest'] = $FormsDigest.DigestValue

    $Context.Dispose()
    return $WebSession
}

然后您的上传文件的脚本可以像这样修改:

#Connect
$Session = Connect-SPO -Username $Username -Password $Password -Url $WebUrl
# Upload file
$targetFileUrl = "$WebUrl/documents/$($FileInfo.Name)"  #set target file url 
Invoke-WebRequest -Uri $targetFileUrl -Method Put -WebSession $Session -InFile $filePath

SPOUploadFile.ps1