I have this code that runs manually, but i would like a scheduled run. A pop-up appears when running manually, which will cause a non-run for auto-scheduled runs, I believe. How do i make this code auto scheduled run friendly.
<# ===================== WINDOWS POWERSHELL 5.x — UPLOAD ONE LOCAL FILE (LEGACY PnP) =====================
TARGET SHAREPOINT FOLDER (already exists):
https://saplant.sharepoint.com/sites/SAPODBDocuments/Shared Documents/Import Documents
SOURCE FILE:
C:\Users\Anthony.DESKTOP-ES5HL78\Downloads\AI n KM\A common understanding_ simplified AI definitions from leading standards _ Digital NSW.pdf
This version avoids modern-only switches (e.g., -Interactive, Resolve-PnPFolder, EnsureProperty) and works
with the legacy module `SharePointPnPPowerShellOnline` that your console is loading.
It will:
• Connect via -UseWebLogin
• Skip upload if same name & size already in SharePoint
• Overwrite (delete then re-upload) if same name but different size
#>
# ---------- CONFIG ----------
$SiteUrl = "https://saplant.sharepoint.com/sites/SAPODBDocuments"
$Library = "Shared Documents"
$SubFolder = "Import Documents"
$LocalFile = "C:\Users\Anthony.DESKTOP-ES5HL78\Downloads\AI n KM\A common understanding_ simplified AI definitions from leading standards _ Digital NSW.pdf"
# ---------- MODULE SETUP (first time) ----------
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if (-not (Get-Module -ListAvailable -Name SharePointPnPPowerShellOnline)) {
Install-Module SharePointPnPPowerShellOnline -Scope CurrentUser -Force
}
Import-Module SharePointPnPPowerShellOnline -ErrorAction Stop
# ---------- CHECK SOURCE FILE ----------
if (-not (Test-Path -LiteralPath $LocalFile)) { throw "Local file not found: $LocalFile" }
# ---------- CONNECT (legacy auth) ----------
Connect-PnPOnline -Url $SiteUrl -UseWebLogin
# ---------- BUILD SERVER-RELATIVE PATHS (legacy-safe via CSOM) ----------
$ctx = Get-PnPContext
$ctx.Load($ctx.Web)
$ctx.ExecuteQuery()
$siteRel = $ctx.Web.ServerRelativeUrl.TrimEnd("/") # e.g. /sites/SAPODBDocuments
$folderSiteRel = "$Library/$SubFolder" # e.g. Shared Documents/Import Documents (site-relative)
$serverFolder = "$siteRel/$folderSiteRel" # e.g. /sites/.../Shared Documents/Import Documents
$fileName = [IO.Path]::GetFileName($LocalFile)
$serverFileUrl = "$serverFolder/$fileName"
# ---------- SAFE OVERWRITE (skip if true duplicate by size) ----------
$existing = Get-PnPFile -Url $serverFileUrl -ErrorAction SilentlyContinue
if ($existing) {
$li = Get-PnPListItem -List $Library -UniqueId $existing.UniqueId
$remoteSize = [int64]($li["File_x0020_Size"])
$localSize = [int64](Get-Item -LiteralPath $LocalFile).Length
if ($remoteSize -eq $localSize) {
Write-Host "[SKIP] Same file already exists in SharePoint: $fileName" -ForegroundColor DarkYellow
return
} else {
Write-Host "[OVERWRITE] Removing existing then re-uploading: $fileName" -ForegroundColor Cyan
Remove-PnPFile -ServerRelativeUrl $serverFileUrl -Force
}
}
# ---------- UPLOAD (legacy Add-PnPFile wants SITE-RELATIVE folder) ----------
Add-PnPFile -Path $LocalFile -Folder $folderSiteRel
Write-Host "Upload complete." -ForegroundColor Green