SharePoint 2010 Powershell 上传本地目录文件夹到文档库 Poweshell upload files to document library

Function Ipmort-OSCFolder
{

 param
 (
 [string]$siteurl,
 [string]$Library,
 [string]$path
 )
 $spWeb = Get-SPWeb $siteurl
 $spDocumentLibrary = $spWeb.Lists[$Library] 
 If($spDocumentLibrary)
 {
  $Fol = Get-Item -Path $path
  $result = $spDocumentLibrary.ParentWeb.GetFolder($spDocumentLibrary.RootFolder.ServerRelativeUrl +"/"+ $Fol.Name )
  If($result.Exists -eq "True")
  {
   Write-Warning "There is a folder existing on site $siteUrl."
  }
  Else
  {
   $SPFol = $spDocumentLibrary.AddItem("",[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$Fol.Name)
   $SPFol.Update()
   SubFolder $path $SPFol $spDocumentLibrary
  }
 }
 Else
 {
  Write-Warning "There is no library named $Library on site $siteurl."
 }

}

Function SubFolder($Folder,$SPFol,$spDocumentLibrary)
{
 $SPFolder = $spDocumentLibrary.ParentWeb.GetFolder($SPFol.Folder.ServerRelativeUrl)
 $Objects = Get-ChildItem -Path $Folder
 Foreach($obj in $Objects)
 {
  If($obj.PSIsContainer)
  { 
   $SubFolder = $spDocumentLibrary.AddItem($SPFolder.ServerRelativeUrl,[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$obj.Name)
   $SubFolder.Update()
   $Fullname = $obj.FullName
   SubFolder $Fullname $SubFolder $spDocumentLibrary
   
  }
  Else
  { 
   $fileStream = ([System.IO.FileInfo]$obj).OpenRead()
   $contents = new-object byte[] $fileStream.Length
   $FolderObj = $spDocumentLibrary.ParentWeb.GetFolder($SPFolder.ServerRelativeUrl)
   $SpFile = $FolderObj.Files.Add($FolderObj.Url + "/"+$obj.Name, $contents, $true)
   $spItem = $SpFile.Item
  }
 }

}

Ipmort-OSCFolder -siteurl "http://win-lfseeatt8jr/sites/myteam" -Library "Shared Documents" -path  "C:\Users\Administrator\Desktop\Test"

你可能感兴趣的:(SharePoint)