8000 Added functionality to set credientials on Set-Service command by joandrsn · Pull Request #4844 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 9 commits into from
Sep 18, 2017
Merged

Added functionality to set credientials on Set-Service command #4844

merged 9 commits into from
Sep 18, 2017

Conversation

joandrsn
Copy link
Contributor
@joandrsn joandrsn commented Sep 15, 2017

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

@msftclas
Copy link

@joandrsn,
Thanks for your contribution.
To ensure that the project team has proper rights to use your work, please complete the Contribution License Agreement at https://cla.microsoft.com.

It will cover your contributions to all Microsoft-managed open source projects.
Thanks,
Microsoft Pull Request Bot

@joandrsn joandrsn changed the title PowerShell/PowerShell#4843 Credentials on Set-Service Credentials on Set-Service Sep 15, 2017
@msftclas
Copy link

@joandrsn, thanks for signing the contribution license agreement. We will now validate the agreement and then the pull request.

Thanks, Microsoft Pull Request Bot

Copy link
Collaborator
@iSazonov iSazonov left a 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;
Copy link
Collaborator

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
Copy link
Collaborator

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.

8000
@@ -1701,6 +1715,14 @@ protected override void ProcessRecord()
"bad StartupType");
break;
}

// set up the Credential parameter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove obvious comment.

@joandrsn
Copy link
Contributor Author

@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.
Unfortunatly I don't know the credentials of the computer that the tests will be running on, so I'm not sure how to write the testcase.
Do you have any documentation on what I can and can't write in the tests?

@iSazonov
Copy link
Collaborator
iSazonov commented Sep 15, 2017

@joandrsn Please see tests for New-Service
I think you should (1) create new test service (2) check that you can change its properties. (3) remove the test service.

Also you should add "[Feature]" in header first position of last commit that CI start the feature tests.

@joandrsn
Copy link
Contributor Author

@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 The account name is invalid or does not exist, or the password is invalid for the account name specified since the users username and username2 does not exist.

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 @(
Copy link
Collaborator

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
Copy link
Collaborator

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"

Copy link
Contributor Author

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

@iSazonov
Copy link
Collaborator

@joandrsn I think we should try to create a real user account for the test with "Log on as a service" privilege.

@joandrsn
Copy link
Contributor Author

@iSazonov
I figured out that I could use some built in users from Windows.
Currently I'm using .\Guest when creating the service and .\Administrator when changing the credentials. Would you rather have it create a user and delete the user again?

@iSazonov
Copy link
Collaborator

Guest and Administrator accounts haven't "Log on as a service" privilege and we don't know password. So we should create test users.

@joandrsn
Copy link
Contributor Author

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? Add-LocalUser has been disabled since #4272. I can't find any other test modules in the repository which creates and deletes a user.

@SteveL-MSFT
Copy link
Member

@joandrsn thanks for the contribution! Look at what I've done here to create my own user and removing at end of test

@iSazonov
Copy link
Collaborator

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?

@SteveL-MSFT
Copy link
Member

@iSazonov it seems the test case added by @joandrsn does a new and then set, were you thinking of something different?

$service = New-Service @parameters
$service | Should Not BeNullOrEmpty
$service = Get-CimInstance Win32_Service -Filter "name='$name'"
$service | Should Not BeNullOrEmpty
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Collaborator

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"}
Copy link
Collaborator

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)
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same about $null

< 10000 tr data-position="0">
@@ -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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

Copy link
Collaborator
@iSazonov iSazonov left a 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
Copy link
Collaborator

it seems the test case added by @joandrsn does a new and then set , were you thinking of something different?
@SteveL-MSFT I've analyzed these tests more closely.
I don't understand why we need test "SetServiceCommand can be used as API for '' with ''". It seems we should do the checks on real service by invoke.
It seems we don't check that New- and Set-Service ignore System and Boot in StartupType.

@SteveL-MSFT
Copy link
Member

@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.

@iSazonov iSazonov changed the title Credentials on Set-Service Added functionality to set credientials on Set-Service command Sep 18, 2017
@iSazonov iSazonov merged commit b7ec5da into PowerShell:master Sep 18, 2017
@iSazonov
Copy link
Collaborator

@joandrsn Many thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0