You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve error message for Get-Service with an invalid ComputerName #2747
Resolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issues
The error message for Get-Service returns a NoServiceFoundForGivenName exception even when the problem is an invalid -ComputerName, potentially misleading the user.
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.
You can handle the error like below, you can customize the same according to you needs
`
try
{
Get-Service wuauserv -ComputerName MadeUpComputerName -ErrorAction Stop
}
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
Kevin Marquette (@KevinMarquette) Feel free to make the PR. In code review time it can be changed.
I would prefer something like GetServiceRemoteConnectionException
Resolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issues
Activity
SteveL-MSFT commentedon Nov 21, 2016
We should probably keep -computername for compat reasons, but maybe consider making it use PSRP under the covers
joeyaiello commentedon Nov 21, 2016
Steve Lee (@SteveL-MSFT) maybe, but I think that's a much larger discussion (and body of work) than the issue here.
kvprasoon commentedon Dec 11, 2016
This could completely break the logic of a script in case of error handling
iSazonov commentedon Dec 11, 2016
Yes,it can. But on the other hand, adding the new exception we improve the error handling.
kvprasoon commentedon Dec 12, 2016
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.
Vivek2015 commentedon Dec 12, 2016
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
}
`
kvprasoon commentedon Dec 22, 2016
Vivek2015 Problem here is not how to handle error , but giving right exception.
KevinMarquette commentedon Mar 7, 2017
What if we used the same exception that
Test-Connectiongives?iSazonov commentedon Mar 7, 2017
Kevin Marquette (@KevinMarquette) Feel free to make the PR. In code review time it can be changed.
I would prefer something like
GetServiceRemoteConnectionExceptionCode point for fix.
It seems we should also fix the exception when Get-Service is called without service name.
joandrsn commentedon Oct 24, 2017
This issue should probably be closed since issue #5090 which has been addressed in pull-request #5094
iSazonov commentedon Oct 24, 2017
We removed
ComputerNameparameter at all in #5094