Repository navigation
Added functionality to set credientials on Set-Service command - #4844
Conversation
|
@joandrsn, It will cover your contributions to all Microsoft-managed open source projects. |
|
Jonas B. Andersen (@joandrsn), thanks for signing the contribution license agreement. We will now validate the agreement and then the pull request. |
Ilya (iSazonov)
left a comment
There was a problem hiding this comment.
Jonas B. Andersen (@joandrsn) Many thanks for your contribution!
Please add a description in the PR.
Also please add tests (see Set-Service.Tests.ps1).
| get { return credential; } | ||
| set { credential = value; } | ||
| } | ||
| internal PSCredential credential = null; |
There was a problem hiding this comment.
For the new code, it is preferable to use auto property. Especially because credential is not used.
| } | ||
|
|
||
| // modify startup type or display name | ||
| // modify startup type or display name or credential |
There was a problem hiding this comment.
Please start comments with capitalized char. We shouldn't add bad patterns.
| break; | ||
| } | ||
|
|
||
| // set up the Credential parameter |
There was a problem hiding this comment.
Please remove obvious comment.
|
Ilya (@iSazonov) thank you for the feedback. I have updated my pull request. But I still need to create tests for the code I have written. |
|
Jonas B. Andersen (@joandrsn) Please see tests for New-Service Also you should add "[Feature]" in header first position of last commit that CI start the feature tests. |
|
Ilya (@iSazonov) I have now added the test, but when executing the test with what it is written within the test, you will receive an error I have tested with my own 2 users and it seems to work, but is this okay? |
| $newServiceCommand.$parameter | Should Be $value | ||
| } | ||
|
|
||
| It "Set-Service can create a new service called '<name>' and change the credentials" -TestCases @( |
There was a problem hiding this comment.
"Set-Service can create a new service" - it is incorrect.
| finally { | ||
| $service = Get-CimInstance Win32_Service -Filter "name='$name'" | ||
| if ($service -ne $null) { | ||
| $service | Remove-CimInstance |
There was a problem hiding this comment.
Please add "-Force -ErrorAction SilentlyContinue"
There was a problem hiding this comment.
Neither Get-CimInstance nor Remote-CimInstance has a force parameter. But I can add -ErrorAction SilentlyContinue
|
Jonas B. Andersen (@joandrsn) I think we should try to create a real user account for the test with "Log on as a service" privilege. |
|
Ilya (@iSazonov) |
|
Guest and Administrator accounts haven't "Log on as a service" privilege and we don't know password. So we should create test users. |
|
They don't have the access, but the test passes. I guess that might not be a good way to test (using invalid credentials to validate a test). Do you have suggestion on how you would add the user? |
|
Jonas B. Andersen (@joandrsn) thanks for the contribution! Look at what I've done here to create my own user and removing at end of test |
|
For PowerShell team: We haven't tests to really create new service and change its properties. So we should refactor all tests after the PR will be merged. Maybe create tracking Issue? |
|
Ilya (@iSazonov) it seems the test case added by Jonas B. Andersen (@joandrsn) does a |
| $service = New-Service @parameters | ||
| $service | Should Not BeNullOrEmpty | ||
| $service = Get-CimInstance Win32_Service -Filter "name='$name'" | ||
| $service | Should Not BeNullOrEmpty |
There was a problem hiding this comment.
this check isn't necessary as the next line will fail
| $service | Should Not BeNullOrEmpty | ||
| $service = Get-CimInstance Win32_Service -Filter "name='$name'" | ||
| $service | Should Not BeNullOrEmpty | ||
| $service.StartName | Should Be $creds.UserName |
| $creds = [pscredential]::new(".\$endUsername", $password) | ||
| Set-Service -Name $name -Credential $creds | ||
| $service = Get-CimInstance Win32_Service -Filter "name='$name'" | ||
| $service | Should Not BeNullOrEmpty |
There was a problem hiding this comment.
This check isn't necessary
| $service.StartName | Should Be $creds.UserName | ||
| } | ||
| finally { | ||
| $service = Get-CimInstance Win32_Service -Filter "name='$name'" -ErrorAction SilentlyContinue |
There was a problem hiding this comment.
You can simply this to just:
Get-CimInstance ... | Remove-CimInstance -ErrorAction SilentlyContinue| try { | ||
| $testPass = "Secret123!" | ||
| $null = net user $startUsername $testPass /add | ||
| $null = net user $endUsername $testPass /add |
There was a problem hiding this comment.
Minor comment - we usually use the pattern net user $startUsername $testPass /add > $null
| (ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force)); | ||
| endCredential = [System.Management.Automation.PSCredential]::new(".\Administrator", | ||
| (ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force))} | ||
| @{name = "testsetcredential"; startUsername = "user1"; endUsername = "user2"} |
There was a problem hiding this comment.
It is single test and we should remove TestCases.
| $service.StartName | Should Be $startCredential.UserName | ||
| Set-Service -Name $name -Credential $endCredential | ||
| $service.StartName | Should Be $creds.UserName | ||
| $creds = [pscredential]::new(".\$endUsername", $password) |
There was a problem hiding this comment.
Please add new line to split into logical blocks.
| $service | Remove-CimInstance -ErrorAction SilentlyContinue | ||
| } | ||
| $null = net user $startUsername /delete | ||
| $null = net user $endUsername /delete |
There was a problem hiding this comment.
The same about $null
| } | ||
|
|
||
| It "Set-Service can change credentials of a service" { | ||
| param() |
Ilya (iSazonov)
left a comment
There was a problem hiding this comment.
Jonas B. Andersen (@joandrsn) Many thanks!
I'll merge on next week.
|
|
Ilya (@iSazonov) The "use as API" tests are specifically for covering the getter/setters for the parameters which is only used when called as an API and not when used as cmdlet parameter. If we're missing some variations, we should add those, but perhaps as a different PR. |
|
Jonas B. Andersen (@joandrsn) Many thanks for your contribution! |
…Shell#4844) Now we can specifiy the -Credential parameter on Set-Service cmdlet to change a service's logon account.
Added functionality to set credientials on
Set-Servicecommand.Using this you can now specifiy the
-Credentialparameter which makes the service you have specfied change the log-on account.Issue #4843