We needed to publish a file to all document libraries, with the same name, in a site collection. To achieve that, the following powershell script was made, by bits and pieces from different blogs. The script runs trough the site collection and uploads the file to the "Documents" folder.
clear
$DocLibName = "Documents"
$FilePath = "d:\file.pdf"
################## NUMBER OF MEMBERSITES COUNT ##################
$webapplication = Get-SPWebApplication "Web"
$icount = 0
foreach($sitecount in $webapplication.Sites){
$icount++
}
$icount = ($icount-81)
echo $icount
################## NUMBER OF MEMBERSITES COUNT END ###############
################## Upload to ALL MEMBERSITES ##################
$i = 0
$numberarray = 0..$icount
echo ($nowStart = Get-Date)
foreach($i in $numberarray) {
#We have different site collections, so we need to run trough them all
$siteurl = "URL/"+$i+"/"
$site = Get-SPSite($siteurl)
foreach($web in $site.AllWebs) {
#Get the Web & Lists to upload the file
$List = $Web.GetFolder($DocLibName)
#Get the Files collection
$Files = $List.Files
#Get File Name from Path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)
#Get the File
$File= Get-ChildItem $FilePath
#Add File to the collection
$Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$false)
echo "Uploader til "$web.Url
#Dispose the objects
$web.Dispose()
}
$i++
}
echo "Start:"$nowStart
echo "Slut:"($nowEnd = Get-Date)
################## Upload to MEMBERSITES END #################
It should also work with multiple files in the upload folder.
Ingen kommentarer:
Send en kommentar