Skip to content

Improve error message for Get-Service with an invalid ComputerName #2747

Closed

Description

Filed on UserVoice: http://windowsserver.uservoice.com/forums/301869/suggestions/17193962

The error message for Get-Service returns a NoServiceFoundForGivenName exception even when the problem is an invalid -ComputerName, potentially misleading the user.

Steps to reproduce

Get-Service wuauserv -ComputerName MadeUpComputerName

Expected behavior

Get-Service : Cannot find any computer with name 'MadeUpComputerName'.
At line:1 char:1
+ Get-Service wuauserv -ComputerName MadeUpComputerName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (wuauserv:String) [Get-Service], ServiceCommandExce
   ption
    + FullyQualifiedErrorId : SomeOtherExceptionForComputerNameNotFound,Microsoft.PowerShell.Commands.GetService
   Command

Actual behavior

Get-Service : Cannot find any service with service name 'wuauserv'.
At line:1 char:1
+ Get-Service wuauserv -ComputerName MadeUpComputerName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (wuauserv:String) [Get-Service], ServiceCommandExce
   ption
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetService
   Command

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSRemotingProtocolVersion      2.3
PSVersion                      6.0.0-alpha
WSManStackVersion              3.0
BuildVersion                   3.0.0.0
SerializationVersion           1.1.0.1
PSEdition                      Core
GitCommitId                    v6.0.0-alpha.12
CLRVersion
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

Activity

  1. added
    WG-Cmdlets
    Up-for-Grabs
    on Nov 21, 2016
  2. SteveL-MSFT commented on Nov 21, 2016

    @SteveL-MSFT
    Member

    We should probably keep -computername for compat reasons, but maybe consider making it use PSRP under the covers

  3. joeyaiello commented on Nov 21, 2016

    @joeyaiello
    ContributorAuthor

    Steve Lee (@SteveL-MSFT) maybe, but I think that's a much larger discussion (and body of work) than the issue here.

  4. kvprasoon commented on Dec 11, 2016

    @kvprasoon
    Contributor

    This could completely break the logic of a script in case of error handling

  5. iSazonov commented on Dec 11, 2016

    @iSazonov
    Collaborator

    Yes,it can. But on the other hand, adding the new exception we improve the error handling.

  6. kvprasoon commented on Dec 12, 2016

    @kvprasoon
    Contributor

    Ilya (@iSazonov) An example use case is - If there is something to do based on the exception returned , end user/script will be misguided and won't be able to do what it is intended to do.

  7. Vivek2015 commented on Dec 12, 2016

    @Vivek2015

    You can handle the error like below, you can customize the same according to you needs
    `
    try
    {
    Get-Service wuauserv -ComputerName MadeUpComputerName -ErrorAction Stop
    }

    catch [Microsoft.PowerShell.Commands.ServiceCommandException]
    {

    get error record

    [Management.Automation.ErrorRecord]$e = $_

    retrieve information about runtime error

    $info = [PSCustomObject]@{
    Exception = $e.Exception.Message
    Reason = $e.CategoryInfo.Reason
    Target = $e.CategoryInfo.TargetName
    Script = $e.InvocationInfo.ScriptName
    Line = $e.InvocationInfo.ScriptLineNumber
    Column = $e.InvocationInfo.OffsetInLine
    }

    output information. Post-process collected info, and log info (optional)

    $info
    }

    `

  8. kvprasoon commented on Dec 22, 2016

    @kvprasoon
    Contributor

    Vivek2015 Problem here is not how to handle error , but giving right exception.

  9. KevinMarquette commented on Mar 7, 2017

    @KevinMarquette
    Contributor

    What if we used the same exception that Test-Connection gives?

    Testing connection to computer 'BAD_NAME' failed: No such host is known
    FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
    
  10. iSazonov commented on Mar 7, 2017

    @iSazonov
    Collaborator

    Kevin Marquette (@KevinMarquette) Feel free to make the PR. In code review time it can be changed.
    I would prefer something like GetServiceRemoteConnectionException

    Code point for fix.

    It seems we should also fix the exception when Get-Service is called without service name.

    PS C:\Program Files\PowerShell\6.0.0.16> get-service -ComputerName sadsad
    get-service : Cannot open Service Control Manager on computer 'sadsad'. This operation might require other privileges.
    At line:1 char:1
    + get-service -ComputerName sadsad
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-Service], InvalidOperationException
        + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetServiceCommand
    
    PS C:\Program Files\PowerShell\6.0.0.16> get-service wuauserv -ComputerName sadsad
    get-service : Cannot find any service with service name 'wuauserv'.
    At line:1 char:1
    + get-service wuauserv -ComputerName sadsad
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (wuauserv:String) [Get-Service], ServiceCommandException
        + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
  11. joandrsn commented on Oct 24, 2017

    @joandrsn
    Contributor

    This issue should probably be closed since issue #5090 which has been addressed in pull-request #5094

  12. iSazonov commented on Oct 24, 2017

    @iSazonov
    Collaborator

    We removed ComputerName parameter at all in #5094

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

    Resolution-FixedUp-for-GrabsWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions