powershell icon indicating copy to clipboard operation
powershell copied to clipboard

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

Open softwareliberationarmy opened this issue 3 years ago • 11 comments

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

softwareliberationarmy avatar Jul 06 '22 16:07 softwareliberationarmy

Addendum: to test out this theory, I tried calling Remove-PnPFile with both -Recycle and -Force (to disambiguate between the two possible ParameterSetNames), and I now see a different error:

Remove-PnPFile : Value does not fall within the expected range.
At D:\a\_temp\847b0583-f763-48b9-9fc6-d93218b7eac8.ps1:53 char:5
+     Remove-PnPFile -ServerRelativeUrl $file.ServerRelativeUrl -Recycl ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Remove-PnPFile], ServerException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.RemoveFile
PowerShell exited with code '1'.

softwareliberationarmy avatar Jul 06 '22 18:07 softwareliberationarmy

I can confirm this, we're seeing the same issue. Remove-PnPFile no longer works with release 1.11.0.

I reverted back to 1.10.0 this morning and it works again.

mattatabm avatar Jul 07 '22 09:07 mattatabm

Same issue here, downgrade to 1.10.0 allowed to bypass temporarily the problem

marc-pinyol avatar Jul 08 '22 10:07 marc-pinyol

Had a similar issue with Remove-PnPListItem. That was resolved in 1.10.50

JazBInKC avatar Jul 12 '22 13:07 JazBInKC

I can confirm this, we're seeing the same issue. Remove-PnPFile no longer works with release 1.11.0.

I reverted back to 1.10.0 this morning and it works again.

#metoo During testing: $pnpFileObj.ServerRelativeURL is valid: /PROJECTS/14/14452/03_Images/Construction/Windows/IMG_8431_a.JPG

Remove-PnPFile -Connection $Conn -ServerRelativeURL $pnpFileObj.ServerRelativeURL -Recycle -Force

Remove-PnPFile : Value does not fall within the expected range.
At C:\NBRS\OneDrive - NBRS\Scripts\_Sharepoint_Maint\NBRS_SPmaint_lib.v1.02.ps1:199 char:1
+ Remove-PnPFile -Connection $Conn -ServerRel ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Remove-PnPFile], ServerException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.RemoveFile

Remove-PnPFile -Connection $Conn -ServerRelativeURL $pnpFileObj.ServerRelativeURL -Recycle

Remove-PnPFile : Value does not fall within the expected range.
At C:\NBRS\OneDrive - NBRS\Scripts\_Sharepoint_Maint\NBRS_SPmaint_lib.v1.02.ps1:201 char:1
+ Remove-PnPFile -Connection $Conn -ServerRel ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Remove-PnPFile], ServerException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.RemoveFile

Remove-PnPFile -Connection $Conn -ServerRelativeURL $pnpFileObj.ServerRelativeURL -Force

Remove-PnPFile : Parameter set cannot be resolved using the specified named parameters.
At C:\NBRS\OneDrive - NBRS\Scripts\_Sharepoint_Maint\NBRS_SPmaint_lib.v1.02.ps1:203 char:1
+ Remove-PnPFile -Connection $Conn -ServerRel ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-PnPFile], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,PnP.PowerShell.Commands.Files.RemoveFile

Remove-PnPFile -Connection $Conn -ServerRelativeURL $pnpFileObj.ServerRelativeURL

Remove-PnPFile : Parameter set cannot be resolved using the specified named parameters.
At C:\NBRS\OneDrive - NBRS\Scripts\_Sharepoint_Maint\NBRS_SPmaint_lib.v1.02.ps1:205 char:1
+ Remove-PnPFile -Connection $Conn -ServerRel ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-PnPFile], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,PnP.PowerShell.Commands.Files.RemoveFile

Cadder avatar Jul 19 '22 16:07 Cadder

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

Name         Type Items/Size Last Modified         
----         ---- ---------- -------------         
IMG_1363.jpg File          0 21/03/2018 12:30:36 AM

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:

Remove-PnPFile : Value does not fall within the expected range.
At line:1 char:8
+ $del = Remove-PnPFile -Connection $Script:Conn -Se ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Remove-PnPFile], ServerException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.RemoveFile

INSTEAD: use Remove-PNPListitem. Thus: $deletedObj = Remove-PnPListitem -connection $Script:Conn -identity ($pnpFileObj.Listitemallfields) -Recycle -Force works and $deletedObj returns

RecycleBinItemId                    
----------------                    
f6c08141-e41b-4a29-af2b-e7d43d38671a

Cadder avatar Jul 23 '22 06:07 Cadder

My Workaround $web = Get-PnPWeb -Connection $site_connection -ErrorVariable error_web -ErrorAction SilentlyContinue; $path = $web.ServerRelativeUrl + "/SitePages/Home.aspx"; $file = $web.GetFileByServerRelativeUrl($path)
$file.DeleteObject() Invoke-PnPQuery -RetryCount 5 -Connection $site_connection;

BTW Pls enable Unit-Test again. thx.

vreezy avatar Jul 26 '22 15:07 vreezy

If I'm not mistaken, we can remove all parameter sets. I don't think they are very useful here. What do you think @KoenZomers @gautamdsheth?

milanholemans avatar Jul 27 '22 23:07 milanholemans

Same issue here with the 1.11.0, Remove-PnPFile does not work

damienvanrobaeys avatar Jul 28 '22 08:07 damienvanrobaeys

Same problem here. wont let me delete when using -serverrelativeurl OR -siterelativeurl Remove-PnPFile : Parameter set cannot be resolved using the specified named parameters.

Any news for a fix??

rbishop-etq avatar Aug 03 '22 17:08 rbishop-etq

I made a fix for this issue. Locally it works for me now. Just have to wait for a maintainer to review the changes.

milanholemans avatar Aug 04 '22 15:08 milanholemans

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.

gautamdsheth avatar Aug 17 '22 11:08 gautamdsheth

Hello, Nice new Any date for the new release including the fix ?

damienvanrobaeys avatar Aug 27 '22 12:08 damienvanrobaeys

Hello, Nice new Any date for the new release including the fix ?

Hi @damienvanrobaeys

Hard to say when the next major release will be. If I check the release history, there are 3 months on average between every release. Last release was published on July 1st.

This fix is already live in the beta version of PnP.PowerShell. To install this, run the following command:

Install-Module -Name PnP.PowerShell -AllowPrerelease -SkipPublisherCheck -AllowClobber

milanholemans avatar Aug 27 '22 13:08 milanholemans

Install-Module -Name PnP.PowerShell -AllowPrerelease -SkipPublisherCheck -AllowClobber

I have installed the beta. Unfortunately is received below error when using Remove-PnPFile

Remove-PnPFile : Parameter set cannot be resolved using the specified named parameters.

PS C:\WINDOWS\system32> Get-Module PnP.Powershell

ModuleType Version Name ExportedCommands


Manifest 1.11.60 PnP.PowerShell {Add-PnPAlert, Add-PnPApp, Add-PnPApplicationCustomizer, Add-PnPAvailableSiteClassification...}

JeffreyvanEijk avatar Aug 30 '22 12:08 JeffreyvanEijk

Hi @JeffreyvanEijk

Could you verify that you are indeed using the latest beta release? This can be done by running following command:

Get-InstalledModule -Name PnP.PowerShell

milanholemans avatar Aug 30 '22 12:08 milanholemans

Hi @milanholemans ,

Thanks for your quick reply!!

PS C:\WINDOWS\system32> Get-Module PnP.Powershell

ModuleType Version    Name                                ExportedCommands                                                                                                                                                           
---------- -------    ----                                ----------------                                                                                                                                                           
Manifest   1.11.60    PnP.PowerShell                      {Add-PnPAlert, Add-PnPApp, Add-PnPApplicationCustomizer, Add->PnPAvailableSiteClassification...}      

JeffreyvanEijk avatar Aug 30 '22 12:08 JeffreyvanEijk

Hi @JeffreyvanEijk

Strangely enough your version does not end on -nightly. When I install the nightly release, it looks like this:

image

What I usually do is completely uninstall the alpha release to prevent unexpected behavior and install the nightly release as follows:

Uninstall-Module -Name PnP.PowerShell
Install-Module -Name PnP.PowerShell -AllowClobber -AllowPrerelease -SkipPublisherCheck

After doing this, it works fine for me.

image

milanholemans avatar Aug 30 '22 14:08 milanholemans

Another workaround option, since @Cadder's workaround did not capture .ListItemAllFields for me:

$deleteMe = Get-PnPFile -Url 'Shared Documents/path/to/delete-me.docx' -AsListItem
Remove-PnPListItem -List Documents -Identity $deleteMe.Id   # Add -Force or -Recycle at your discretion

joshtransient avatar Oct 05 '22 12:10 joshtransient

I am also got an error while using Remove-PnPFile. I had to use SiteRelative Url and made a connection to the site before. Script:

Remove-PnPFile -Connection $Connection -SiteRelativeUrl $SourcePath -Recycle

i get the following error:

Remove-PnPFile : serverRelativePath Parameter name: Der angegebene Wert wird für den serverRelativePath-Parameter nicht unterstützt. At C:\Users\sharegate04\Documents\Archiving\PnPOnlyScript.ps1:14 char:3

  • Remove-PnPFile -Connection $Connection -SiteRelativeUrl $SourcePath ...
  • + CategoryInfo          : WriteError: (:) [Remove-PnPFile], ServerException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.RemoveFile
    
    

i am using PnP.PowerShell 1.12.0

Thomas-Drechsler avatar Feb 07 '23 12:02 Thomas-Drechsler

I don't think this error is related to this issue, but quick check, are you specifying a decoded URL?

milanholemans avatar Feb 07 '23 13:02 milanholemans

I tested it with encoded and decoded values. But the problem i would address is that i use a SiteRelativeUrl and the Error states that the value cannot be used for the serverRelativePath parameter. this makes no sense in my opinion. Do i miss something?

Thomas-Drechsler avatar Feb 07 '23 13:02 Thomas-Drechsler

no is not encoded. But the problem i would address is that i use a SiteRelativeUrl and the Error states that the value cannot be used for the serverRelativePath parameter. this makes no sense in my opinion. Do i miss something?

This is because in the code, SiteRelativeUrl is being translated to ServerRelativeUrl. I just tested it and it seems to be working for me:

Remove-PnPFile -SiteRelativeUrl "/TestLib/Sample-copy.pdf" -Recycle -Force

And this is also working for me:

Remove-PnPFile -SiteRelativeUrl "TestLib/Sample-copy.pdf" -Recycle -Force

milanholemans avatar Feb 07 '23 13:02 milanholemans

which version of pnp powershell you have used? i tested some different things but i got this error The url is correct encoded, an the statement is the same as yours... The error states that the Path is wrong and not the url. is this only a typo?

Thomas-Drechsler avatar Feb 07 '23 14:02 Thomas-Drechsler

I'm using version 1.12.0. I can tell you that your URL must be decoded, encoded URLs will fail for sure.

The error states that the Path is wrong and not the url. is this only a typo?

Within SharePoint, URLs can be encoded, Paths should be decoded. Can you show the exact value you are providing for this option?

milanholemans avatar Feb 07 '23 14:02 milanholemans

Freigegebene%20Dokumente%2fFolderA%2fFolderB%2fFilename.pptx

Thomas-Drechsler avatar Feb 09 '23 14:02 Thomas-Drechsler

Freigegebene%20Dokumente%2fFolderA%2fFolderB%2fFilename.pptx

Hi @Thomas-Drechsler, I think this is indeed the issue. The path you are providing is an URL encoded string which is not supported (anymore). If you provide -SiteRelativeUrl "Freigegebene Dokumente/FolderA/FolderB/Filename.pptx", this should work.

milanholemans avatar Feb 09 '23 14:02 milanholemans

i tested various combinations but it does not work. i have check the following:

  • connection
  • permissions
  • decoded input string (site relative url)
  • Remove-PnPFile -SiteRelativeUrl $SourcePath -> Error " Parameter set cannot be resolved using the specified named parameters."
  • Remove-PnPFile -SiteRelativeUrl $SourcePath -Recycle -> Error "Remove-PnPFile : serverRelativePath Parameter name: Der angegebene Wert wird für den serverRelativePath-Parameter nicht unterstützt."
  • Remove-PnPFile -ServerRelativeUrl $SourcePath -> Error "Remove-PnPFile : Parameter set cannot be resolved using the specified named parameters."

Thomas-Drechsler avatar Feb 09 '23 15:02 Thomas-Drechsler

i reduced my script to the essential part: $Connection = Connect-PnPOnline -Url "https://tenantname.sharepoint.com" -Tenant tenantname.onmicrosoft.com -Interactive $SourcePath = "Freigegebene Dokumente/test.docx" Remove-PnPFile -SiteRelativeUrl $SourcePath -Recycle -Force

Error-> Remove-PnPFile : serverRelativePath Parameter name: Der angegebene Wert wird für den serverRelativePath-Parameter nicht unterstützt. At line:1 char:1

  • Remove-PnPFile -SiteRelativeUrl $SourcePath -Recycle -Force
  •   + CategoryInfo          : WriteError: (:) [Remove-PnPFile], ServerException
      + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.RemoveFile
    

Thomas-Drechsler avatar Feb 09 '23 15:02 Thomas-Drechsler

Thomas –

I’ve a script (moves data from ODB to a SharePoint Archive) where I delete the files after copy. I’m using

PS C:\scripts> get-command Remove-pnpfile

CommandType Name Version Source


Cmdlet Remove-PnPFile 1.12.32 PnP.PowerShell

And I’m not having the issue that you’re experiencing – I too had problems with earlier interim version.

Regards,

jbw

@.https://ihs911.com/ @.https://twitter.com/IHS911marketing @.https://www.facebook.com/IHS911/ @.https://www.linkedin.com/company/ihs911 James Wood​​​​ Support Services ‑ Solution Specialist t: (913) 945-4711tel:(913)%20945-4711 f: (913) 945-4711fax:(913)%20945-4711 e: @.@.> | w: www.ihs911.comhttps://ihs911.com/ a: 7520 W. 160th St. Suite 101 , Overland Park , KS 66085

From: Thomas-Drechsler @.> Sent: Thursday, February 9, 2023 9:49 AM To: pnp/powershell @.> Cc: James Wood @.>; Comment @.> Subject: Re: [pnp/powershell] [BUG] Remove-PnpFile failing in v1.11.0 with parameter issue (Issue #2099)

Attention: This is an external email. Please do not click links or open attachments unless you recognize the source of this email and know the content is safe.

i reduced my script to the essential part: $Connection = Connect-PnPOnline -Url "https://tenantname.sharepoint.com" -Tenant tenantname.onmicrosoft.com -Interactive $SourcePath = "Freigegebene Dokumente/test.docx" Remove-PnPFile -SiteRelativeUrl $SourcePath -Recycle -Force

Error-> Remove-PnPFile : serverRelativePath Parameter name: Der angegebene Wert wird für den serverRelativePath-Parameter nicht unterstützt. At line:1 char:1

  • Remove-PnPFile -SiteRelativeUrl $SourcePath -Recycle -Force

· + CategoryInfo : WriteError: (:) [Remove-PnPFile], ServerException

·

· + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.RemoveFile

·

— Reply to this email directly, view it on GitHubhttps://github.com/pnp/powershell/issues/2099#issuecomment-1424411310, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AW7N6AVSXZT3GEZLWR7DFV3WWUGXLANCNFSM522KYLPQ. You are receiving this because you commented.Message ID: @.@.>>

JazBInKC avatar Feb 09 '23 16:02 JazBInKC