高级用法:
一、创建文件夹与子文件夹
先写一个创建文件夹的公用方法 $url 代表文件夹的路径(即:DocLib/XX/) $list 代表对应的列表, $name 文件夹的名字 return 的是一个文件夹对象,在下面的操作过程中会用到。
# Add ItemFunction AddItemFolder($url,$list,$name){ Write-host ".............Starting create " $name " Folder,please waiting............." -foregroundcolor blue $folder = $list.AddItem($url,$Folder,$name) $folder.Update() return $folder Write-host ".............. " $name " Folder already is created......................." -foregroundcolor blue}
获取文档库并添加文件夹与子文件夹
$listName = "组织过程管理文档库" $listDescription = "组织过程管理文档库" DeleteList $web $listName #delete AddList $web $listName $listDescription $DocTemp #add $list = $web.Lists.TryGetList($listName) $f = AddItemFolder "" $list "PowerShellFolder0" #创建文件夹 write-host $f.Url -foregroundcolor red AddItemFolder $f.Url $list "PowerShellFolder0_1" #创建子文件夹
二、上传本地文件及批量上传
$folderFile = $web.GetFolder("组织过程管理文档库").Files #获取文档库 $file = Get-ChildItem C:/123.docx $folderFile.Add($f.Url+"/123.docx",$file.OpenRead(),$true) #$f.Url 等兄弟们学习的时候调试一下就明白为什么要这样写了 $folderFile.Add($f.Url.split('/')[0]+"/123.docx",$file.OpenRead(),$true) #上传到根目录。 #批量上传 Get-ChildItem C:/ -filter "*.docx" | ForEach-Object{ $folderFile.Add($f.Url.split('/')[0]+"/$($_.Name)",$_.OpenRead(),$true) }
三、复制文件,从一个文档库移动到另一个文档库
$list = $web.Lists.TryGetList("组织过程管理文档库") write-host $list.RootFolder $Doc1 = $web.GetFolder($list.RootFolder) #获取文档库 Write-Host "Count:" $Doc1.Files.Count #Copy foreach($file in $Doc1.Files){ $file.CopyTo("DocLib1/"+$file.Name,$true) }
四、移动文件,从一个文档库移动到另一个文档库,在这什么不能用 Feach,兄弟们可以想想,很有学问的,在项目开发过程中经常遇到这样的问题,呵呵........
$list = $web.Lists.TryGetList("组织过程管理文档库") write-host $list.RootFolder $Doc1 = $web.GetFolder($list.RootFolder) #获取文档库 Write-Host "Count:" $Doc1.Files.Count #Move $count = $Doc1.Files.Count While($count -ne 0){ Write-host $count $Doc1.Files[$count - 1].MoveTo("DocLib2/"+$Doc1.Files[$count - 1].Name,$true) $count -- }
应用案例
SharePoint网站经过一年后,客户说默认样式太难看了,还要添加新的需求,结果就要重新做页面与提取新的需求,目前项目正在进行中,但一直有一个 问题没底不知如何解决,就是当我们把项目做完了,目前正在用的平台上面有很多文档,文档应该如何移动呢,一直想不出好的办法,对SharePoint数据 迁移也没有研究,只是对PowerShell有了解,最终决定先研究SharePoint,想用命令进行数据迁移,为什么不研究其它是因为自己对 Powershell了解的比较多................说了这么多废话,其实就是 文档迁移
迁移思路: 遍历文档库里的文件夹(用递归),之后在另一个文档库里创建递归出来的文件夹,同时再拷贝文件
具体方法有如下几个:
第一、递归文件夹 目前有问题,不知为什么,但有替代方案,用Fearch多次循环
Function RecursiveFolder($folder,$doc,$obj){ foreach($folderSub in $folder.SubFolders){ Write-host $folderSub.Name # AddItemFolder 这个函数调用总是出问题,"找不到对应的重载方法" 真是无语了,只要把这个函数放到递归方法 #里就出问题,真的想不出什么原因,会的兄弟给点指示 $folderObjSub = AddItemFolder $obj.Url $doc $folderSub.Name #创建文件夹 RecursiveFolder $folderSub $doc $folderObjSub }}
多次循环就不会出错
foreach($f in $folderList.SubFolders){ if($f.Name -ne "Forms" ){ if($f.Name -ne "Document" ){ #Write-host $f.Name $f.SubFolders.Add($f.Name) $folderObj = AddItemFolder "" $listForm $f.Name #创建文件夹 write-host $folderObj.Url GetFiles $f $folderObj.Url+"/" #复制文件 #RecursiveFolder $f $listForm $folderObj foreach($folderSub in $f.SubFolders){ Write-host $folderSub.Name $folderObjSub = AddItemFolder $folderObj.Url $listForm $folderSub.Name #创建文件夹 #RecursiveFolder $folderSub $doc $folderObjSub GetFiles $folderSub $folderObjSub.Url+"/" #复制文件 } } }
第二、遍历文件
Function GetFiles($folder,$url){ foreach($file in $folder.Files){ write-host $file.Name $file.CopyTo($url+$file.Name,$true) }}
第三、详细代码
Function EachFolder($web){ $list = $web.Lists.TryGetList("组织过程管理文档库") $folderList = $web.GetFolder($list.RootFolder) #获取文档库 $listForm = $web.Lists.TryGetList("项目流程表单模板库") $folderListForm = $web.GetFolder($listForm.RootFolder) #获取文档库 foreach($f in $folderList.SubFolders){ if($f.Name -ne "Forms" ){ if($f.Name -ne "Document" ){ #Write-host $f.Name $f.SubFolders.Add($f.Name) $folderObj = AddItemFolder "" $listForm $f.Name #创建文件夹 write-host $folderObj.Url GetFiles $f $folderObj.Url+"/" RecursiveFolder $f $listForm $folderObj } } }}
第四:全部代码
全部代码
# Check to ensure Microsoft.SharePoint.PowerShell is loaded$Snapin = get-PSSnapin | Where-Object { $_.Name -eq 'Microsoft.SharePoint.Powershell'}if($Snapin -eq $null){ Write-host "Loading SharePoint Powershell Snapin" Add-PSSnapin "Microsoft.SharePoint.Powershell"}#SPListTemplateType$DocTemp = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary$GenericListTemp = [Microsoft.SharePoint.SPListTemplateType]::GenericList$Folder = [Microsoft.SharePoint.SPFileSystemObjectType]::Folder#SPFieldType$TextFieldType = [Microsoft.SharePoint.SPFieldType]::Text $NoteFieldType = [Microsoft.SharePoint.SPFieldType]::Note$ChoiceFieldType = [Microsoft.SharePoint.SPFieldType]::Choice$NumberFieldType = [Microsoft.SharePoint.SPFieldType]::Number#SPAddFieldOptions$DefaultViewFieldOptions = [Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView# Get WebApplication$WebAppName = "SharePoint - 999"$WebApp = Get-SPWebApplication $WebAppName$WebUrl = $WebApp.Url+"ProcessAsset"write-host $WebUrl$Web = Get-SPWeb $WebUrlFunction AddList($web,$listName,$listDescription,$listTemplate){ Write-host ".............Starting create " $listName ",please waiting............." -foregroundcolor yellow $web.Lists.Add($listName,$listDescription,$listTemplate) Write-host ".............. " $listName " already is created......................." -foregroundcolor green}# List OnQuickLaunchFunction OnQuickLaunchList($list,$bool){ $list.OnQuickLaunch = $bool $list.Update()}# Add ItemFunction AddItemFolder($url,$list,$name){ Write-host ".............Starting create " $name " Folder,please waiting............." -foregroundcolor blue $folder = $list.AddItem($url,$Folder,$name) $folder.Update() return $folder Write-host ".............. " $name " Folder already is created......................." -foregroundcolor blue}# Delete ListFunction DeleteList($web,$listName){ $list = $web.Lists.TryGetList($listName) if($list -ne $null){ Write-Host "............." $listName "already exists,deleting................" -foregroundcolor red $list.Delete() Write-host "............." $listName "already is deleted....................." -foregroundcolor Green Write-host "" }}#Add FieldsFunction AddFieldXml($list,$xml,$bool,$fieldOptions){ $list.Fields.AddFieldAsXml($xml,$bool,$fieldOptions)}Function AddDoc_OrganizationProcessManager($web){ $listName = "组织过程管理文档库" $listDescription = "组织过程管理文档库" DeleteList $web $listName #delete AddList $web $listName $listDescription $DocTemp #add $list = $web.Lists.TryGetList($listName) $f = AddItemFolder "" $list "PowerShellFolder0" #创建文件夹 write-host $f.Url -foregroundcolor red AddItemFolder $f.Url $list "PowerShellFolder0_1" #创建子文件夹 $folderFile = $web.GetFolder("组织过程管理文档库").Files #获取文档库 $file = Get-ChildItem C:/123.docx $folderFile.Add($f.Url+"/123.docx",$file.OpenRead(),$true) #$f.Url 等兄弟们学习的时候调试一下就明白为什么要这样写了 $folderFile.Add($f.Url.split('/')[0]+"/123.docx",$file.OpenRead(),$true) #上传到根目录。 #批量上传 Get-ChildItem C:/ -filter "*.docx" | ForEach-Object{ $folderFile.Add($f.Url.split('/')[0]+"/$($_.Name)",$_.OpenRead(),$true) } #$AddSubFolder = $web.GetFolder("组织过程管理文档库") #$AddSubFolder.SubFolders.Add("Archive") #AddItemFolder "" $list "PowerShellFolder1" #AddItemFolder "" $list "PowerShellFolder2" OnQuickLaunchList $list True}#$PSSnapin = Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null#clear #$org = "http://blueleader"#$dest = "http://redleader" #$orgLibrary = (Get-SPWeb $org).Folders["Documents"]#$destLibrary = (Get-SPWeb $dest).Folders["Documents"]#$destFiles = $destLibrary.Files#foreach ($file in $orgLibrary.Files)#{ #$curFile = $file.OpenBinary() #$destURL = $destFiles.Folder.Url + "/" + $file.Name #$destFiles.Add($destURL, $curFile, $true)#} Function CopyToDoc($web){ $list = $web.Lists.TryGetList("组织过程管理文档库") write-host $list.RootFolder $Doc1 = $web.GetFolder($list.RootFolder) #获取文档库 Write-Host "Count:" $Doc1.Files.Count #Copy foreach($file in $Doc1.Files){ $file.CopyTo("DocLib1/"+$file.Name,$true) } #Move $count = $Doc1.Files.Count While($count -ne 0){ Write-host $count $Doc1.Files[$count - 1].MoveTo("DocLib2/"+$Doc1.Files[$count - 1].Name,$true) $count -- } }Function EachFolder($web){ $list = $web.Lists.TryGetList("组织过程管理文档库") $folderList = $web.GetFolder($list.RootFolder) #获取文档库 $listForm = $web.Lists.TryGetList("项目流程表单模板库") $folderListForm = $web.GetFolder($listForm.RootFolder) #获取文档库 foreach($f in $folderList.SubFolders){ if($f.Name -ne "Forms" ){ if($f.Name -ne "Document" ){ #Write-host $f.Name $f.SubFolders.Add($f.Name) $folderObj = AddItemFolder "" $listForm $f.Name #创建文件夹 write-host $folderObj.Url GetFiles $f $folderObj.Url+"/" RecursiveFolder $f $listForm $folderObj } } }}Function RecursiveFolder($folder,$doc,$obj){ foreach($folderSub in $folder.SubFolders){ Write-host $folderSub.Name # AddItemFolder 这个函数调用总是出问题,"找不到对应的重载方法" 真是无语了,只要把这个函数放到递归方法 #里就出问题,真的想不出什么原因,会的兄弟给点指示 $folderObjSub = AddItemFolder $obj.Url $doc $folderSub.Name #创建文件夹 RecursiveFolder $folderSub $doc $folderObjSub }}Function GetFiles($folder,$url){ foreach($file in $folder.Files){ write-host $file.Name $file.CopyTo($url+$file.Name,$true) }}if($Web -ne $null){ #AddDoc_OrganizationProcessManager $Web #CopyToDoc $Web #DeleteList $web "文档" #默认的文档库删除 EachFolder $Web $Web.Dispose()}else{ Write-Host $WebUrl " is not existing"} delete llists Get-SPWeb "http://spdwcdcdvm007" | Where-Object { $_.Lists.Delete([System.Guid]$_.Lists["Candidate Materials"].ID) } resource: http://www.cnblogs.com/Fengger/archive/2012/08/14/2639046.html