经过最近几天的查询与搜索,研究与学习对PowerShell 文档管理有了新的认识,特别写两篇文章来介绍用PowerShell管理文档库,通过测试与研究,通过PowerShell上传文档速度是相当快的,对初始化文档库有着举足轻重的作用,下面来一一讲解具体的应用方法。
第一:创建一个文档库,创建文档库与列表的方法一样,下面是介绍如何创建文档库的方法
Function 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 }
第二:创建一个文件夹,Return是返回一个Folder对象,用于获取创建的文件夹的基本属性信息,$url是代表文档库所在的位置,通过这个$url可以创建子文件夹。
# Add Item Function 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 }
第三:创建文件夹与子文件夹
$f = AddItemFolder "" $list "PowerShellFolder0" #创建文件夹 write-host $f.Url -foregroundcolor red AddItemFolder $f.Url $list "PowerShellFolder0_1" #创建子文件夹
第四:上传文件到文档库,也可以上传到指定的文件夹,$true代表的意思是 如果有相同的文件,则覆盖。
$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) }
为了让兄弟们能够更好的学习与理解,我把自己总结的方法与大家分享,下面列出所有的代码,供学习参考与理解,如有好的方法与想法,请在下面留言,分享自己的代码与想法。
详细代码,供参考与学习
# 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 $WebUrl Function 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 OnQuickLaunch Function OnQuickLaunchList($list,$bool){ $list.OnQuickLaunch = $bool $list.Update() } # Add Item Function 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 List Function 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 Fields Function 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) } #AddItemFolder "" $list "PowerShellFolder1" #AddItemFolder "" $list "PowerShellFolder2" OnQuickLaunchList $list True } if($Web -ne $null){ AddDoc_OrganizationProcessManager $Web DeleteList $web "文档" #默认的文档库删除 $Web.Dispose() } else{ Write-Host $WebUrl " is not existing" }