Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PS 7.2] operator && doesn't recognize exit #17122

Closed
5 tasks done
Raffaello opened this issue Apr 8, 2022 · 5 comments
Closed
5 tasks done

[PS 7.2] operator && doesn't recognize exit #17122

Raffaello opened this issue Apr 8, 2022 · 5 comments
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Duplicate The issue is a duplicate.

Comments

@Raffaello
Copy link

Raffaello commented Apr 8, 2022

Prerequisites

Steps to reproduce

Using PS 7.2

for eg running this 1 line script:

PS C:\> $host.Version | Where-Object {$_.Major -eq 7} && exit 1  

it will result in the following error output:

Major  Minor  Build  Revision
-----  -----  -----  --------
7      2      2      -1
exit: The term 'exit' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again

Expected behavior

Major  Minor  Build  Revision
-----  -----  -----  --------
7      2      2      -1
[process exited with code 1 (0x00000001)]

Actual behavior

It doesn't recognize exit keyword

Error details

PS C:\>Get-Error

Exception             :
    Type        : System.Management.Automation.CommandNotFoundException
    ErrorRecord :
        Exception             :
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : The term 'exit' is not recognized as a name of a cmdlet, function, script file, or executable
program.
                      Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
            HResult : -2146233087
        TargetObject          : exit
        CategoryInfo          : ObjectNotFound: (exit:String) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : CommandNotFoundException
        InvocationInfo        :
            ScriptLineNumber : 1
            OffsetInLine     : 50
            HistoryId        : 1
            Line             : $host.Version | Where-Object {$_.Major -eq 7} && exit
            PositionMessage  : At line:1 char:50
                               + $host.Version | Where-Object {$_.Major -eq 7} && exit
                               +                                                  ~~~~
            InvocationName   : exit
            CommandOrigin    : Internal
        ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    CommandName : exit
    TargetSite  :
        Name          : LookupCommandInfo
        DeclaringType : System.Management.Automation.CommandDiscovery, System.Management.Automation,
Version=7.2.2.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message     : The term 'exit' is not recognized as a name of a cmdlet, function, script file, or executable
program.
                  Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
    Data        : System.Collections.ListDictionaryInternal
    Source      : System.Management.Automation
    HResult     : -2146233087
    StackTrace  :
   at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes,
SearchResolutionOptions searchResolutionOptions, Com

Environment data

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.2
PSEdition                      Core
GitCommitId                    7.2.2
OS                             Microsoft Windows 10.0.19044
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

@Raffaello Raffaello added the Needs-Triage The issue is new and needs to be triaged by a work group. label Apr 8, 2022
@Raffaello Raffaello changed the title operator && doesn't recognize exit [PS 7.2] operator && doesn't recognize exit Apr 8, 2022
@SeeminglyScience SeeminglyScience added Resolution-Duplicate The issue is a duplicate. Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels Apr 8, 2022
@SeeminglyScience
Copy link
Collaborator

Duplicate of #10967

@SeeminglyScience SeeminglyScience marked this as a duplicate of #10967 Apr 8, 2022
@Raffaello
Copy link
Author

Raffaello commented Apr 8, 2022

well actually through the duplicate i found that in this way works:

 $host.Version | Where-Object {$_.Major -eq 7} && $(exit 1)

Major  Minor  Build  Revision
-----  -----  -----  --------
7      2      2      -1

[process exited with code 1 (0x00000001)]

so, yes it could be more a design discusion rather than a bug.

I am closing this ticket as i am not considering it a bug anymore, but rather it was my lack of knowledge in PS. :)

@jhoneill
Copy link

jhoneill commented Apr 8, 2022

What comes after && must be a valid as pipeline so if/while/do/switch/exit/return statements won't work - There's plenty at #10967 but if it doesn't work with the | it won't work with || or &&

I was about to post the $() solution but as I was typing your reply appeared

You do realise that where-object successfully running includes returning nothing so the right hand side always runs in your example :-)

@Raffaello
Copy link
Author

What comes after && must be a valid as pipeline so if/while/do/switch/exit/return statements won't work - There's plenty at #10967 but if it doesn't work with the | it won't work with || or &&

I was about to post the $() solution but as I was typing your reply appeared

You do realise that where-object successfully running includes returning nothing so the right hand side always runs in your example :-)

yeah right. My example it was really too much Bash biased.
But i tried different exmaples also wrapping the whole expression and using count etc, but thre is no way to make return error code or something if something you are looking for is missing, it will be just missing and operation succesful.
the only way is to have multiple lines to achieve the same result so that operator && is quite not so much useful yet, but still nice introduction in v7.

in brief:
Using powershell for some simple operations to return != 0 to report something not ok, it is really challenging or might be verbose.

But it could be also the case that I have still a lot to learn around PS. 😄

@jhoneill
do you know, based on my silly example, how to achieve that kind of results?
for eg, when powershell is major version 7 return an exit code of 1.

@jhoneill
Copy link

jhoneill commented Apr 8, 2022

I'd use an if - anything which isn't null, 0, empty or $false will be treated as true. (So it's rare to need .count etc)

if ($host.Version.Major -eq 7) { exit 1 }
or better
if ($host.Version -ge 6.0.0 ) { exit 1 }

This will exit if the version is 5.0, 6.0, or 7.0
if (-not $host.Version.Minor) { exit 1 }

If it is something complicated I can put a pipeline in the ()

if ($host.Version | Where-Object {$_.Major -eq 7}) { exit 1 }

This pattern is quite common
$fileList = get-item *.ps1
if (-not $fileList)
followed by one of

{Write-Warning "No files " ; return}
{throw "no files" }
{exit 2}  # error for 'file not found'

and then

foreach ($f in $fileList}

Exit as some strange behaviour have a look at the end of this which I wrote after someone I was talking to was going made working out why they didn't get an exit code back. https://jhoneill.github.io/powershell/2019/08/21/ExitThrowReturnEtc.html

PowerShell (or PWSH if you prefer). Devops (especially Azure Devops), Photography, and general thoughts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Duplicate The issue is a duplicate.
Projects
None yet
Development

No branches or pull requests

3 participants