-
Notifications
You must be signed in to change notification settings - Fork 8.1k
[Feature]Added Remove-service to Management module #4858
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
Conversation
|
@joandrsn, |
| { | ||
| hScManager = NativeMethods.OpenSCManagerW( | ||
| ServiceComputerName, | ||
| null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use named parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite follow you. Did you want to use the variable ComputerName ? Since the method supports both a Name on the local computer and a servicecontroller on a remote machine, I believe I have to use the ServiceComputerName variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes the code more readable, particularly where null is passed.
| @@ -1,4 +1,5 @@ | |||
| Describe "Set/New-Service cmdlet tests" -Tags "Feature", "RequireAdminOnWindows" { | |||
| Import-Module ".\Microsoft.PowerShell.Commands.Management.dll" | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be required. Can you remove this?
| finally { | ||
| Get-CimInstance Win32_Service -Filter "name='$servicename'" | Remove-CimInstance -ErrorAction SilentlyContinue | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add tests which validates the error cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add tests for pipeline input.
| /// This class implements the set-service command | ||
| /// </summary> | ||
| [Cmdlet(VerbsCommon.Remove, "Service", SupportsShouldProcess = true, DefaultParameterSetName = "Name")] | ||
| [OutputType(typeof(ServiceController))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically Remove* cmdlets do not return anything.
| finally { | ||
| Get-CimInstance Win32_Service -Filter "name='$servicename'" | Remove-CimInstance -ErrorAction SilentlyContinue | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add tests for pipeline input.
markekraus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor formatting and comment/summary rewording suggestions.
|
|
||
| #region Overrides | ||
| /// <summary> | ||
| /// Create the service |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be Remove the service?
|
|
||
| #region RemoveServiceCommand | ||
| /// <summary> | ||
| /// This class implements the set-service command |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be Remove-Service?
| #region Parameters | ||
|
|
||
| /// <summary> | ||
| /// Name of the service to create |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be Name of the service to remove?
|
|
||
| /// <summary> | ||
| /// The following is the definition of the input parameter "InputObject". | ||
| /// Specifies ServiceController object representing the services to be stopped. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be the services to be removed.?
| continue; | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove extra line.
| @@ -1,4 +1,5 @@ | |||
| Describe "Set/New-Service cmdlet tests" -Tags "Feature", "RequireAdminOnWindows" { | |||
| Import-Module "C:\Users\jsa\code\PowerShell\src\powershell-win-core\bin\Debug\netcoreapp2.0\win7-x64\Microsoft.PowerShell.Commands.Management.dll" | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please Remove
| { Remove-Service -Name "testremoveservice" -ErrorAction 'Stop' } | Should Throw | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove extra line
|
@joandrsn it looks like the cmdlet doesn't export. I can repro on win10 x64. No clue why, though. |
|
@markekraus If I import the cmdlet with my import statement that I previously committed (and removed again), I can get it to work. Somehow the travis build gets the module imported (it doesn't fail). |
|
@joandrsn I guess you need to add them to the exports here: |
| /// <summary> | ||
| /// Name of the service to remove | ||
| /// </summary> | ||
| /// <value></value> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should probably remove this
| protected override void ProcessRecord() | ||
| { | ||
| ServiceController service = null; | ||
| string ServiceComputerName = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for consistency, should use camelCasing
| { | ||
| hScManager = NativeMethods.OpenSCManagerW( | ||
| ServiceComputerName, | ||
| null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes the code more readable, particularly where null is passed.
| WriteNonTerminatingError( | ||
| service, | ||
| exception, | ||
| "CouldNotRemoveService", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to the right error. According to the docs, CloseServiceHandle only fails if it's an invalid handle which is probably never going to the be case here. Anyways, even if this fails, it would seem that the service was still deleted. Maybe this should just be a diagnostic assert?
| bool succeeded = NativeMethods.CloseServiceHandle(hScManager); | ||
| if (!succeeded) | ||
| { | ||
| int lastError = Marshal.GetLastWin32Error(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above, perhaps just an assert?
| } | ||
| $service = New-Service @parameters | ||
| $service | Should Not BeNullOrEmpty | ||
| Get-Service -Name $servicename | Remove-Service -ErrorAction SilentlyContinue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you want -ErrorAction SilentlyContinue there in case something fails
| Diagnostics.Assert(!String.IsNullOrEmpty(Name), "null ServiceName"); | ||
| // "new ServiceController" will succeed even if | ||
| // there is no such service. This checks whether | ||
| // the service actually exists. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we put the comment before Diagnostics.Assert?
And please reformat the comment - we can use wider lines.
|
|
||
| #region RemoveServiceCommand | ||
| /// <summary> | ||
| /// This class implements the remove-service command |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Case - Remove-Service.
| bool objServiceShouldBeDisposed = false; | ||
| try | ||
| { | ||
| if (_ParameterSetName.Equals("InputObject", StringComparison.OrdinalIgnoreCase) && InputObject != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we using _ParameterSetName in the file (twice)? In all other files we use ParameterSetName.
I believe we should fix this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like line 1604 used this and @joandrsn was just following this pattern. Agree that we should change it to just ParameterSetName (including line 1604) for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we could remove _ParameterSetName.Equals("InputObject", StringComparison.OrdinalIgnoreCase) at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. InputObject check should be sufficient
| } | ||
| catch (ArgumentException ex) | ||
| { | ||
| //Cannot use WriteNonterminatingError as service is null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add space // Cannot.
| } | ||
| catch (InvalidOperationException ex) | ||
| { | ||
| //Cannot use WriteNonterminatingError as service is null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add space // Cannot
|
@joandrsn Please add |
| "Cmdlet", "Set-PSDebug", , $($FullCLR -or $CoreWindows -or $CoreUnix) | ||
| "Cmdlet", "Set-PSSessionConfiguration", , $($FullCLR -or $CoreWindows -or $CoreUnix) | ||
| "Cmdlet", "Set-Service", , $($FullCLR -or $CoreWindows ) | ||
| "Cmdlet", "Remove-Service", , $($FullCLR -or $CoreWindows ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this up in the list so it is in alphabetical order? Should go between Remove-PSSnapin and Remove-TypeData
…e test which used the function class directly
|
I think I figured it out. |
|
I think this probably needs to be reverted. I believe |
|
Do you have a suggestion on how I would be able to use |
|
I think it was using Looking for guidance from other cmdlets, many of them are using if (InputObject != null)and if (Name != null)Example I lean towards either switching to the null checks or just leaving it as @SteveL-MSFT @iSazonov thoughts? |
|
Seems like the best solution here (preferring simplicity) is to revert back to deriving from |
|
Oops, I again got an artifact 😄 |
…d rewrite test which used the function class directly" This reverts commit da41f7d.
…ut in Set-Service
SteveL-MSFT
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
adityapatwardhan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
markekraus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
@joandrsn Thanks for your contribution! |
|
What vesion of PowerShell is Remove-service available for? What about version 4? |
|
@PingPongSet Windows PowerShell is frozen. All new features will be only added to PowerShell Core. Feel free download latest version, try and feedback. |
|
@iSazonov What vesion of PowerShell is Remove-service available for? |
|
@PingPongSet I don' remember the version but you could look release notes for PowerShell Core 6.0. Also make only sense download latest PowerShell Core 6.0 version. |
I have added the function
Remove-Serviceto the Management module.The functions
New-ServiceandSet-Servicewere already present, I thought it would be a good idea to have aRemove-ServiceIssue #4717