powershell: [BUG] Remove-PnpFile failing in v1.11.0 with parameter issue
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
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 34 (10 by maintainers)
I made a fix for this issue. Locally it works for me now. Just have to wait for a maintainer to review the changes.
it works now! i had remove the folder and reinstall the pnp.powershell module with scope to current user and now i have the right version of the command and it works well. Thanks to @milanholemans and @JazBInKC !
Hi @JeffreyvanEijk
Strangely enough your version does not end on
-nightly. When I install the nightly release, it looks like this:What I usually do is completely uninstall the alpha release to prevent unexpected behavior and install the nightly release as follows:
After doing this, it works fine for me.
Hello all, the issue has been fixed by @milanholemans ! It will be available from tomorrow’s nightly and the next major version.
Thank you for raising the issue and thanks Milan for providing the fix.
WORKAROUND: I gave up trying to get Remove-PNPFile to work intuitively, but did find an old article here: Why Use Remove-PnPListItem vs Remove-PnPFile? which suggested there is a better way anyway. (In my case, the Remove-PnPFile command is within a separate function and I’m using more than one site, so I need to specify the -Connection parameter. You may not find it necessary)
Given: $pnpFileObj returns
and $pnpFileObj.ServerRelativeUrl returns
/PROJECTS/14/14452/03_Images/Construction/Windows/IMG_1363.jpg$deletedObj = Remove-PnPFile -Connection $Script:Conn -ServerRelativeURL $pnpFileObj.ServerRelativeURL -Recycle -Force Fails with:
INSTEAD: use Remove-PNPListitem. Thus: $deletedObj = Remove-PnPListitem -connection $Script:Conn -identity ($pnpFileObj.Listitemallfields) -Recycle -Force works and $deletedObj returns