Unable to delete a file using the Rest API from a Powershell script. The user has permission to delete from the repository and is able to delete files from the repositroy using the UI. However, a (403) Forbidden is returned when deleteing file through the API.
$credentials = New-Object System.Management.Automation.PSCredential ($user_name, $password)
Invoke-RestMethod -Method Delete -Uri "http://10.2.94.239:8081/artifactory/pla-nuget-dev-local/pla.188.8.131.52.nupkg" -Credential $credentials
Invoke-RestMethod : The remote server returned an error: (403) Forbidden.
At C:\PowerShell\ShowArtifactoryUser.ps1:13 char:1
+ Invoke-RestMethod -Method Delete -Uri "http://10.2.94.239:8081/artifa ...
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Looking at the artifactory logs it appears that the credentials are not being passed as expected. The logs show that the delete api is being made by anonymous. Artifactory logs here:
2019-09-23 14:17:09,527 [http-nio-8081-exec-8731] [WARN ] (o.a.r.ArtifactoryResponseBase:125) - Sending HTTP error code 403: Not enough permissions to delete/overwrite artifact 'pla-nuget-dev-local:pla-testArtifactoryRepo.184.108.40.206.nupkg' (user 'anonymous' needs DELETE permission).
I needed to use the -Header for authentication instead of -Credentials with Invoke-RestMethod