-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- did not include files related to 2012R2 and 1803 Source: https://github.com/cloudfoundry-attic/bosh-psmodules/ [#175550580](https://www.pivotaltracker.com/story/show/175550580) Where should bosh-psmodules live?
- Loading branch information
Showing
47 changed files
with
5,180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Remove-Module -Name BOSH.Account -ErrorAction Ignore | ||
Import-Module ./BOSH.Account.psm1 | ||
|
||
Remove-Module -Name BOSH.Utils -ErrorAction Ignore | ||
Import-Module ../BOSH.Utils/BOSH.Utils.psm1 | ||
|
||
Describe "Account" { | ||
|
||
Context "when username is not provided" { | ||
It "throws" { | ||
{ Add-Account } | Should Throw "Provide a user name" | ||
} | ||
} | ||
|
||
Context "when password is not provided" { | ||
It "throws" { | ||
{ Add-Account -User hello } | Should Throw "Provide a password" | ||
} | ||
} | ||
|
||
Context "when the username and password are valid" { | ||
$timestamp=(get-date -UFormat "%s" -Millisecond 0) | ||
$user = "TestUser_$timestamp" | ||
$password = "Password123!" | ||
|
||
BeforeEach { | ||
$userExists = !!(Get-LocalUser | Where {$_.Name -eq $user}) | ||
if($userExists) { | ||
Remove-LocalUser -Name $user | ||
} | ||
} | ||
|
||
It "Adds and removes a new user account" { | ||
Add-Account -User $user -Password $password | ||
mkdir "C:\Users\$user" -ErrorAction Ignore | ||
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME" | ||
$existing = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user } | ||
$existing | Should Not Be $null | ||
Remove-Account -User $user | ||
$existing = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user } | ||
$existing | Should Be $null | ||
} | ||
} | ||
} | ||
|
||
Remove-Module -Name BOSH.Account -ErrorAction Ignore | ||
Remove-Module -Name BOSH.Utils -ErrorAction Ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@{ | ||
RootModule = 'BOSH.Account' | ||
ModuleVersion = '0.1' | ||
GUID = 'ebdf7a79-39df-46ce-a046-42dd10de82ce' | ||
Author = 'BOSH' | ||
Copyright = '(c) 2017 BOSH' | ||
Description = 'Commands for creating a new Windows user' | ||
PowerShellVersion = '4.0' | ||
RequiredModules = @('BOSH.Utils') | ||
FunctionsToExport = @('Add-Account','Remove-Account') | ||
CmdletsToExport = @() | ||
VariablesToExport = '*' | ||
AliasesToExport = @() | ||
PrivateData = @{ | ||
PSData = @{ | ||
Tags = @('BOSH') | ||
LicenseUri = 'https://github.com/cloudfoundry-incubator/bosh-windows-stemcell-builder/blob/master/LICENSE' | ||
ProjectUri = 'https://github.com/cloudfoundry-incubator/bosh-windows-stemcell-builder' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<# | ||
.Synopsis | ||
Add Windows user | ||
.Description | ||
This cmdlet adds a Windows user | ||
#> | ||
function Add-Account { | ||
Param( | ||
[string]$User = $(Throw "Provide a user name"), | ||
[string]$Password = $(Throw "Provide a password") | ||
) | ||
Write-Log "Add-Account" | ||
|
||
Write-Log "Creating new local user $User." | ||
& NET USER $User $Password /add /y /expires:never | ||
|
||
$Group = "Administrators" | ||
|
||
Write-Log "Adding local user $User to $Group." | ||
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME" | ||
Write-Log $adsi | ||
$AdminGroup = $adsi.Children | where {$_.SchemaClassName -eq 'group' -and $_.Name -eq $Group } | ||
Write-Log $AdminGroup | ||
$UserObject = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $User } | ||
Write-Log $UserObject | ||
$AdminGroup.Add($UserObject.Path) | ||
Write-Log "Completed adding $User to $Group" | ||
} | ||
|
||
<# | ||
.Synopsis | ||
Remove Windows user | ||
.Description | ||
This cmdlet removes a Windows user | ||
#> | ||
function Remove-Account { | ||
Param( | ||
[string]$User = $(Throw "Provide a user name") | ||
) | ||
Write-Log "Remove-Account" | ||
Write-Log "Removing local user $User." | ||
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME" | ||
$adsi.Delete('User', $User) | ||
Move-Item -Path "C:\Users\$User" -Destination "$env:windir\Temp\$User" -Force -ErrorAction Ignore | ||
} |
Oops, something went wrong.