PoshCode Archive  Artifact [19e3a92b66]

Artifact 19e3a92b665b990a88c218f95d52df65bb11ff6cc2359db51b9abe3c9f20ef92:

  • File Publish-File.ps1 — part of check-in [5220f14b40] at 2018-06-10 13:05:23 on branch trunk — Use this to upload one or more files to a SharePoint document library. Should also work with any WebDAV service, although that hasn’t been tested. The filename parameter expects fileinfo objects. Easiest way to do so is to pass them on the pipeline from Get-ChildItem. This script is a refinement of a technique that I first saw here: http://blogs.flexnetconsult.co.uk/colinbyrne/PermaLink,guid,a326572f-8f78-4c80-86d5-1fe52cbd6fe5.aspx. (user: halr9000 size: 1873)

# encoding: ascii
# api: powershell
# title: Publish-File
# description: Use this to upload one or more files to a SharePoint document library. Should also work with any WebDAV service, although that hasn’t been tested. The filename parameter expects fileinfo objects. Easiest way to do so is to pass them on the pipeline from Get-ChildItem.  This script is a refinement of a technique that I first saw here: http://blogs.flexnetconsult.co.uk/colinbyrne/PermaLink,guid,a326572f-8f78-4c80-86d5-1fe52cbd6fe5.aspx.
# version: 0.1
# type: function
# author: halr9000
# license: CC0
# function: Publish-File
# x-poshcode-id: 2122
# x-archived: 2016-06-29T20:02:17
# x-published: 2011-09-07T09:54:00
#
# The credential parameter is optional. If you do not supply it, then your currently logged-in credentials are used.
#
# Note that this version will not descend directories.
function Publish-File {
	param (
		[parameter( Mandatory = $true, HelpMessage="URL pointing to a SharePoint document library (omit the '/forms/default.aspx' portion)." )]
		[System.Uri]$Url,
		[parameter( Mandatory = $true, ValueFromPipeline = $true, HelpMessage="One or more files to publish. Use 'dir' to produce correct object type." )]
		[System.IO.FileInfo[]]$FileName,
		[system.Management.Automation.PSCredential]$Credential
	)
	$wc = new-object System.Net.WebClient
	if ( $Credential ) { $wc.Credentials = $Credential }
	else { $wc.UseDefaultCredentials = $true }
	$FileName | ForEach-Object {
		$DestUrl = "{0}{1}{2}" -f $Url.ToString().TrimEnd("/"), "/", $_.Name
		Write-Verbose "$( get-date -f s ): Uploading file: $_"
		$wc.UploadFile( $DestUrl , "PUT", $_.FullName )
		Write-Verbose "$( get-date -f s ): Upload completed"
	}
	
}

# Example:
# dir c:\path\files* | Publish-File -Url "https://mysharepointsite.com/personal/userID/Personal%20Documents"