This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param | |
( | |
[String] | |
[Parameter(Mandatory)] | |
$SourceFolder, | |
[String] | |
$Destination , | |
[string] | |
$AdminUserName, | |
[string] | |
$AdminPassword | |
) | |
$creds = $AdminPassword | ConvertTo-SecureString -AsPlainText -Force | |
$SPdevcredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminUserName, $creds | |
#invoke-webrequest -uri $url -Credential $SPdevcredentials | |
if($MyInvocation.MyCommand.Path) | |
{ | |
$scriptpath = $MyInvocation.MyCommand.Path | |
$d = Split-Path $scriptpath | |
write-output -InputObject "Script Directory -->$D" | |
pushd | |
cd $d | |
import-module .\SharePointPnPPowerShellOnline -DisableNameChecking -Force | |
popd | |
} | |
else{import-module .\SharePointPnPPowerShellOnline -DisableNameChecking -Force} | |
$u = $null | |
$isurl = [system.uri]::TryCreate($Destination,[system.urikind]::RelativeOrAbsolute,[ref]$u) | |
if(($U.Scheme -eq 'http' ) -or ($u.Scheme -eq 'https')) | |
{ | |
if(get-command connect-pnponline -erroraction ignore) | |
{ | |
$url = $u.AbsoluteUri | |
do #for($i = 1; $i -le $u.Segments.count; $i++) | |
{ | |
try | |
{ | |
connect-pnponline -Url $url -Credentials $SPdevcredentials -CreateDrive -ErrorAction Ignore | |
} | |
catch | |
{ | |
"Error was $_" | |
$line = $_.InvocationInfo.ScriptLineNumber | |
"Error was in Line $line" | |
} | |
$i++ | |
$url = $U.AbsoluteUri -replace (($u.segments | select -last $i ) -join ''), '' | |
}While((-not (get-psdrive -name spo -ErrorAction Ignore)) -and ($i -ne $u.segments.count) ) | |
if(Get-PSDrive -Name spo -ErrorAction Ignore) | |
{ | |
Write-Output "Drives = $((Get-PSDrive).name -join ',')" | |
Write-Output "Site root = $((get-pnpsite).url)" | |
if ($sourcefolder.EndsWith('\')){$sourcefolder = $sourcefolder.trimend('\')} | |
$apps= Get-ChildItem -Path "$SourceFolder" | |
Write-Output "Application to Deploy Filename: $($apps.fullname)" | |
foreach($app in $apps) | |
{ | |
pushd | |
cd ($app ).directory | |
Add-PnPFile -Path $app.fullname -Folder ($Destination -replace (get-pnpsite).url, '') | |
write-output "uploaded $($app.fullname) to $destination" | |
popd | |
} | |
} | |
else | |
{ Throw "Cannot Connect to Sharepoint Online - $destination check username / password / url"} | |
} | |
} | |
else | |
{ | |
throw "$destination url must be an http or https address" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated with better error logic