Skip to content

[BUG] Remove-PnpFile failing in v1.11.0 with parameter issue #2099

@softwareliberationarmy

Description

@softwareliberationarmy

Reporting an Issue or Missing Feature

We have a PS script that runs as part of our CI/CD process that installs Pnp.Powershell (no required version) and calls Connect-PnPOnline, Resolve-PnPFolder, Get-PnPFolderItem, and then Remove-PnPFile in a loop (to clean up old files). Starting on July 5th, when v1.11.0 was released, we've noticed that the Remove-PnPFile calls are all failing with what looks like a parameter issue.

Expected behavior

Expecting the Remove-PnPFile command to delete the SP file (actual command syntax being used: Remove-PnPFile -ServerRelativeUrl $file.ServerRelativeUrl -Force)

Actual behavior

Error output:

Remove-PnPFile : Parameter set cannot be resolved using the specified named parameters.
At D:\a\_temp\680a8da3-2a92-4b2f-bd49-4107237ca3ab.ps1:51 char:5
+     Remove-PnPFile -ServerRelativeUrl $file.ServerRelativeUrl -Force
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-PnPFile], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmbiguousParameterSet,PnP.PowerShell.Commands.Files.RemoveFile
PowerShell exited with code '1'.

Looking at the code file, I suspect this might be caused by there being two ParameterSetNames for ServerRelativeUrl (these appear to have been added in 1.11.0), and the Force parameter isn't able to break the tie and decide which ParameterSetName to use, hence the "AmbiguousParameterSet" error. But that's just a guess.

Steps to reproduce behavior

Please include complete script or code samples in-line or linked from gists

# SharePoint variables
$SharePointUrl = '$(SP.Url)'
$ClientId = '$(SP.ClientId)'
$ClientSecret = '$(SP.ClientSecret)'
$Directory = '$(SP.TargetFolder)\$(Release.Artifacts.SourceBranchName)'

# Archiving variables
$FilesToKeep = 3

If (!(Get-Module -Name Pnp.Powershell)) {
    Install-Module -Name Pnp.Powershell -Scope 'CurrentUser' -Force
    Import-Module -Name Pnp.Powershell
}

Get-Module -Name Pnp.Powershell 

Connect-PnPOnline -Url $SharePointUrl -ClientId $ClientId -ClientSecret $ClientSecret -WarningAction Ignore
$files = Get-PnPFolderItem -FolderSiteRelativeUrl $Directory -ItemType File

$files | Out-Host

#add additional properties
$groupedFiles = $null
$matchedFiles = @()

$pattern = '(?<major>\d+)_(?<minor>\d+)_(?<patch>\d+)_(?<rev>\d+).zip'

foreach ($file in $files) {
    $fileName = $file.Name
    if ($fileName -match $pattern) {
        $file | Add-Member -MemberType NoteProperty -Name 'Major' -Value ([int]$Matches['major'])
        $file | Add-Member -MemberType NoteProperty -Name 'Minor' -Value ([int]$Matches['minor'])
        $file | Add-Member -MemberType NoteProperty -Name 'Patch' -Value ([int]$Matches['patch'])
        $file | Add-Member -MemberType NoteProperty -Name 'Rev' -Value ([int]$Matches['rev'])
        $matchedFiles += $file
    }
    else {
        Write-Host "##vso[task.LogIssue type=warning;] the file '$fileName' did not match the regular expression '$pattern'. The file will be ignored."
    }    
}

$oldFiles = $matchedFiles | Sort-Object 'Major', 'Minor', 'Patch', 'Rev' -Descending | Select-Object -Skip $FilesToKeep

foreach ($file in $oldFiles) {
    Write-Host "Deleting $($File.Name)..."
    Remove-PnPFile -ServerRelativeUrl $file.ServerRelativeUrl -Force  # <<<< THIS LINE FAILS
    Write-Host "$($File.Name) Deleted"
    Start-Sleep -Milliseconds 200 #Sharepoint has thrown errors if Remove-PnPFile or Move-PnpFile are called too quickly
}

What is the version of the Cmdlet module you are running?

1.11.0

Which operating system/environment are you running PnP PowerShell on?

  • Windows
  • Linux
  • MacOS
  • Azure Cloud Shell
  • Azure Functions
  • [x ] Other : please specify - Azure Remote Build Agent running windows-latest

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions