Private/Send-ExcelToSharePointSecure.ps1
<#
.SYNOPSIS Securely uploads an Excel file to a SharePoint Online document library using encrypted credentials. .DESCRIPTION The Send-ExcelToSharePointSecure function allows you to upload a local Excel file to a SharePoint Online document library. It accepts a secure passcode (encrypted password) and username, decrypts them internally, and performs authentication via PnP PowerShell. .PARAMETER SiteUrl The URL of the SharePoint Online site to connect to (e.g., https://company.sharepoint.com/sites/team). .PARAMETER SharePointFileServerRelativeUrl The full server-relative path of the SharePoint file location where the Excel file should be uploaded (e.g., "Shared Documents\Reports\MyFile.xlsx"). .PARAMETER LocalExcelFilePath The full path to the local Excel file to be uploaded. .PARAMETER Username The SharePoint or Microsoft 365 username used for authentication. .PARAMETER Passcode A base64-encoded passcode that is decrypted using the username to form the password for authentication. This enhances security by avoiding plain text passwords. .EXAMPLE Send-ExcelToSharePointSecure -SiteUrl "https://company.sharepoint.com/sites/ITCMR" ` -SharePointFileServerRelativeUrl "Shared Documents\Citrix Documents\Citrix license Monthly Data.xlsx" ` -LocalExcelFilePath "C:\Reports\Citrix license Monthly Data.xlsx" ` -Username "service-citrix@company.com" ` -Passcode "AEnC4lsQHl..." .EXAMPLE # Upload to a different folder with another user Send-ExcelToSharePointSecure -SiteUrl "https://company.sharepoint.com/sites/Finance" ` -SharePointFileServerRelativeUrl "Documents\FinanceReports\FY24Budget.xlsx" ` -LocalExcelFilePath "D:\Exports\FY24Budget.xlsx" ` -Username "finance.user@company.com" ` -Passcode "encryptedstring123..." .NOTES Uses ConvertFrom-SecurePasscode function for secure authentication. Requires the PnP PowerShell module (Install-Module PnP.PowerShell). #> function Send-ExcelToSharePointSecure { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]$SiteUrl, [Parameter(Mandatory = $true)] [string]$SharePointFileServerRelativeUrl, [Parameter(Mandatory = $true)] [string]$LocalExcelFilePath, [Parameter(Mandatory = $true)] [string]$Username, [Parameter(Mandatory = $true)] [string]$Passcode ) try { # Convert passcode to secure password $DecryptedPassword = ConvertFrom-SecurePasscode -Passcode $Passcode -Username $Username $SecurePassword = ConvertTo-SecureString -String $DecryptedPassword -AsPlainText -Force $Credential = [System.Management.Automation.PSCredential]::new($Username, $SecurePassword) # Connect to SharePoint Online Connect-PnPOnline -Url $SiteUrl -Credentials $Credential -WarningAction SilentlyContinue # Prepare upload target $targetFolder = Split-Path -Path $SharePointFileServerRelativeUrl -Parent $fileName = Split-Path -Leaf $SharePointFileServerRelativeUrl # Upload file #Add-PnPFile -Path $LocalExcelFilePath -Folder $targetFolder -FileName $fileName -OverwriteIfAlreadyExists Add-PnPFile -Path $LocalExcelFilePath -Folder (Split-Path -Parent $SharePointFileServerRelativeUrl) Write-LogEntry -Message "Successfully uploaded: $LocalExcelFilePath to $targetFolder" } catch { Write-LogEntry -Message "Upload failed: $_" } finally { Disconnect-PnPOnline } } |