I am using Invoke-RestMethod to post a file to an API. If the file is writeable, the following works (example):
Access to the path 'C:\some\file\path\some.file' is denied.
There is no need for Invoke-RestMethod to write anything to the file.
Line |
-- | … Invoke-RestMethod -Uri "$someUri" …
| ~~~~~~~~~~~~~~~~~~~~~
| Access to the path 'C:\some\file\path\some.file' is denied.
Exception :
Type : System.UnauthorizedAccessException
TargetSite :
Name : CreateFile
DeclaringType : [Microsoft.Win32.SafeHandles.SafeFileHandle]
MemberType : Method
Module : System.Private.CoreLib.dll
Message : Access to the path 'C:\some\file\path\some.file' is denied.
Source : System.Private.CoreLib
HResult : -2147024891
StackTrace :
at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize,
Nullable`1 unixCreateMode)
at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize,
Nullable`1 unixCreateMode)
at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64
preallocationSize, Nullable`1 unixCreateMode)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetMultipartFileContent(Object fieldName, FileInfo file)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.AddMultipartContent(Object fieldName, Object fieldValue, MultipartFormDataContent formData, Boolean enumerate)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.FillRequestStream(HttpRequestMessage request)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], UnauthorizedAccessException
FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
InvocationInfo :
MyCommand : Invoke-RestMethod
ScriptLineNumber : --
OffsetInLine : --
HistoryId : 42
ScriptName : C:\some\file\path\script.ps1
Line : $response = Invoke-RestMethod -Uri "$someUri" `
Statement : Invoke-RestMethod -Uri "$someUri" `
-Method Post `
-Form $formData
PositionMessage : At C:\some\file\path\script.ps1:-- char:--
+ … $response = Invoke-RestMethod -Uri "$someUri …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot : C:\some\file\path
PSCommandPath : C:\some\file\path\script.ps1
InvocationName : Invoke-RestMethod
CommandOrigin : Internal
ScriptStackTrace : at SomeFunction, C:\some\file\path\script.ps1: line --
at <ScriptBlock>, C:\some\file\path\caller-script.ps1: line --
at <ScriptBlock>, <No file>: line 1
Prerequisites
Steps to reproduce
I am using Invoke-RestMethod to post a file to an API. If the file is writeable, the following works (example):
If the file is readonly, it fails with:
There is no need for Invoke-RestMethod to write anything to the file.
Expected behavior
*it works*Actual behavior
Error details
Environment data
Visuals
No response