Auto Schedule Run & Pop up

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

Are you able to determine which command is generating the prompt? What does the prompt look like?

Those things would be very helpful in trying to determine how to code around a prompt

If you’re changing the logon process to remove the pop-up, you should take the opportunity to rewrite the script using the supported PnP PowerShell module. The legacy module has not been updated for several years, is archived, and is no longer maintained.

You will find all the information you need at the site linked above. For unattended access, you should create an App Registration and follow the steps for non interactive authentication using a certificate.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.