-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Added functionality to set credientials on Set-Service command #4844
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, It will cover your contributions to all Microsoft-managed open source projects. |
@joandrsn, thanks for signing the contribution license agreement. We will now validate the agreement and then the pull request. |
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.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the new code, it is preferable to use auto property. Especially because credential
is not used.
@@ -1679,9 +1693,9 @@ protected override void ProcessRecord() | |||
continue; | |||
} | |||
|
|||
// modify startup type or display name | |||
// modify startup type or display name or credential |
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 start comments with capitalized char. We shouldn't add bad patterns.
@@ -1701,6 +1715,14 @@ protected override void ProcessRecord() | |||
"bad StartupType"); | |||
break; | |||
} | |||
|
|||
// set up the Credential parameter |
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 obvious comment.
@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. |
@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. |
@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? |
@@ -110,6 +110,39 @@ Describe "Set/New-Service cmdlet tests" -Tags "Feature", "RequireAdminOnWindows" | |||
$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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add "-Force -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.
Neither Get-CimInstance
nor Remote-CimInstance
has a force parameter. But I can add -ErrorAction SilentlyContinue
@joandrsn I think we should try to create a real user account for the test with "Log on as a service" privilege. |
@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? |
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? |
$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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
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.
BeExactly?
$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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add new line to split into logical blocks.
} | ||
finally { | ||
$service = Get-CimInstance Win32_Service -Filter "name='$name'" -ErrorAction SilentlyContinue | ||
if ($service -ne $null) { | ||
$service | Remove-CimInstance -ErrorAction SilentlyContinue | ||
} | ||
$null = net user $startUsername /delete | ||
$null = net user $endUsername /delete |
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.
The same about $null
@@ -110,6 +110,40 @@ Describe "Set/New-Service cmdlet tests" -Tags "Feature", "RequireAdminOnWindows" | ||
$newServiceCommand.$parameter | Should Be $value | ||
} | ||
|
||
It "Set-Service can change credentials of a service" { | ||
param() |
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.
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.
@joandrsn Many thanks!
I'll merge on next week.
|
@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. |
@joandrsn Many thanks for your contribution! |
Added functionality to set credientials on
Set-Service
command.Using this you can now specifiy the
-Credential
parameter which makes the service you have specfied change the log-on account.Issue #4843