Skip to content

Invoke-WebRequest -OutFile "" strange behaviour on windows #19043

Closed

Description

@CarloToso

Prerequisites

Steps to reproduce

Invoke-WebRequest "https://example.com" -Method GET -OutFile ''
--> No error, no outfile

Invoke-WebRequest "https://example.com" -Method GET -OutFile ' '
--> Error

Same behaviour in PS5 and PS7

Expected behavior

Both or none should throw an error

Actual behavior

The two commands have different behaviours

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.2
PSEdition                      Core
GitCommitId                    7.3.2
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. added
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Jan 27, 2023
  2. mklement0 commented on Jan 27, 2023

    @mklement0
    Contributor

    It looks like -OutFile $null and -0utFile '' (empty string) are unexpectedly treated as if -OutFile hadn't been specified.

    Note that -OutFile ' ' actually succeeds on Unix-like platforms, where you can create a file whose name is a single space.
    While it makes sense to report an error on Windows, the error message is misleading: the space is in effect ignored, so that the current directory is targeted, and instead of complaining that you cannot save to a directory, it spuriously claims "Could not find part of a path"

    Not surprisingly, Invoke-RestMethod is equally affected.

  3. 237dmitry commented on Jan 27, 2023

    @237dmitry

    There are no errors if the correct file name is specified. If the argument to this parameter is a zero-length string, then the parameter is ignored.

    ArcoLinux_2023-01-27_17-01-26

  4. changed the title [-]Invoke-WebRequest -OutFile "" strange behaviour[/-] [+]Invoke-WebRequest -OutFile "" strange behaviour on windows[/+] on Jan 27, 2023
  5. CarloToso commented on Jan 27, 2023

    @CarloToso
    ContributorAuthor

    So, I guess we should have different behaviours for windows and unix

  6. 237dmitry commented on Jan 27, 2023

    @237dmitry

    I guess we should have different behaviours for windows and unix

    It depends on the capabilities of the file system. The error is most likely due to the impossibility of creating a file with that name. The same thing happens in Linux if you specify an invalid filename.

  7. mklement0 commented on Jan 27, 2023

    @mklement0
    Contributor

    Indeed - no platform-specific behavior needed. It comes down to this:

    • That -OutputFile $null / -OutputFile '' cause -OutputFile to be ignored is a bug: an error should be reported that these arguments aren't accepted, just like when you try to pass '' or $null as the file path to Set-Content, for instance.

    • Separately, there's a mostly cosmetic problem when an invalid path is passed (and what constitutes an invalid path is platform-specific, with very few constraints existing on Unix-like platforms): the currently misleading error message should be fixed.

  8. ghost added
    In-PRIndicates that a PR is out for the issue
    on Jan 27, 2023
  9. ghost removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Feb 1, 2023
  10. ghost removed
    In-PRIndicates that a PR is out for the issue
    on Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions