当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 Web 客户端和 PowerShell 将文件远程上传到 SharePoint

作者:精品下载站 日期:2024-12-14 13:43:33 浏览:13 分类:玩电脑

使用 Web 客户端和 PowerShell 将文件远程上传到 SharePoint


如果您想从客户端(或远程)将文件上传到 SharePoint 文档库,这里是漂亮的 PowerShell 代码片段。

使用 Web 客户端 PowerShell 将文件上传到 SharePoint:


#Variable for Source folder in Local machine & Destination in SharePoint
$Source ="c:\temp\file.pdf"
$Target="https://portal.crescent.com/projects/Documents/File.pdf"
 
#Create new web client object
$WebClient = new-object System.Net.WebClient
$Webclient.UseDefaultCredentials = $true

#upload file to sharepoint using webclient
$WebClient.UploadFile($Target,"PUT", $Source) 

将文件夹中的所有文件上传到 SharePoint - Web 客户端 HTTP put 方法:


#Variable for Source folder in Local machine
$FilesLocation ="D:\Scripts\Reports\"
$TargetLocation="https://Operations.Crescent.com/Reports/"

#Create new web client object
$WebClient = new-object System.Net.WebClient
$WebClient.Credentials = [System.Net.CredentialCache]::DefaultCredentials

#Use these lines if you use different credentials
#$cred = new-object System.Net.NetworkCredential("USER NAME","PASSWORD","DOMAIN")
#$WebClient.credentials = $cred

#Target File Location to upload
function Set-DestinationFile($FileName)
{ 
   $TargetLocation + $(split-path -leaf $FileName)
}

#Iterate through each file and upload
Get-ChildItem $FilesLocation | ForEach-Object {

  $DestinationFile= Set-DestinationFile $_;

  #upload file to sharepoint using webclient
  $WebClient.UploadFile($DestinationFile,"PUT", $_.FullName)
} 

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯