Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 1d9be07

Browse files
committed
add bosh-psmodules to stembuild
- 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?
1 parent a3ae3e5 commit 1d9be07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+5180
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Remove-Module -Name BOSH.Account -ErrorAction Ignore
2+
Import-Module ./BOSH.Account.psm1
3+
4+
Remove-Module -Name BOSH.Utils -ErrorAction Ignore
5+
Import-Module ../BOSH.Utils/BOSH.Utils.psm1
6+
7+
Describe "Account" {
8+
9+
Context "when username is not provided" {
10+
It "throws" {
11+
{ Add-Account } | Should Throw "Provide a user name"
12+
}
13+
}
14+
15+
Context "when password is not provided" {
16+
It "throws" {
17+
{ Add-Account -User hello } | Should Throw "Provide a password"
18+
}
19+
}
20+
21+
Context "when the username and password are valid" {
22+
$timestamp=(get-date -UFormat "%s" -Millisecond 0)
23+
$user = "TestUser_$timestamp"
24+
$password = "Password123!"
25+
26+
BeforeEach {
27+
$userExists = !!(Get-LocalUser | Where {$_.Name -eq $user})
28+
if($userExists) {
29+
Remove-LocalUser -Name $user
30+
}
31+
}
32+
33+
It "Adds and removes a new user account" {
34+
Add-Account -User $user -Password $password
35+
mkdir "C:\Users\$user" -ErrorAction Ignore
36+
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
37+
$existing = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user }
38+
$existing | Should Not Be $null
39+
Remove-Account -User $user
40+
$existing = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user }
41+
$existing | Should Be $null
42+
}
43+
}
44+
}
45+
46+
Remove-Module -Name BOSH.Account -ErrorAction Ignore
47+
Remove-Module -Name BOSH.Utils -ErrorAction Ignore
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@{
2+
RootModule = 'BOSH.Account'
3+
ModuleVersion = '0.1'
4+
GUID = 'ebdf7a79-39df-46ce-a046-42dd10de82ce'
5+
Author = 'BOSH'
6+
Copyright = '(c) 2017 BOSH'
7+
Description = 'Commands for creating a new Windows user'
8+
PowerShellVersion = '4.0'
9+
RequiredModules = @('BOSH.Utils')
10+
FunctionsToExport = @('Add-Account','Remove-Account')
11+
CmdletsToExport = @()
12+
VariablesToExport = '*'
13+
AliasesToExport = @()
14+
PrivateData = @{
15+
PSData = @{
16+
Tags = @('BOSH')
17+
LicenseUri = 'https://github.com/cloudfoundry-incubator/bosh-windows-stemcell-builder/blob/master/LICENSE'
18+
ProjectUri = 'https://github.com/cloudfoundry-incubator/bosh-windows-stemcell-builder'
19+
}
20+
}
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<#
2+
.Synopsis
3+
Add Windows user
4+
.Description
5+
This cmdlet adds a Windows user
6+
#>
7+
function Add-Account {
8+
Param(
9+
[string]$User = $(Throw "Provide a user name"),
10+
[string]$Password = $(Throw "Provide a password")
11+
)
12+
Write-Log "Add-Account"
13+
14+
Write-Log "Creating new local user $User."
15+
& NET USER $User $Password /add /y /expires:never
16+
17+
$Group = "Administrators"
18+
19+
Write-Log "Adding local user $User to $Group."
20+
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
21+
Write-Log $adsi
22+
$AdminGroup = $adsi.Children | where {$_.SchemaClassName -eq 'group' -and $_.Name -eq $Group }
23+
Write-Log $AdminGroup
24+
$UserObject = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $User }
25+
Write-Log $UserObject
26+
$AdminGroup.Add($UserObject.Path)
27+
Write-Log "Completed adding $User to $Group"
28+
}
29+
30+
<#
31+
.Synopsis
32+
Remove Windows user
33+
.Description
34+
This cmdlet removes a Windows user
35+
#>
36+
function Remove-Account {
37+
Param(
38+
[string]$User = $(Throw "Provide a user name")
39+
)
40+
Write-Log "Remove-Account"
41+
Write-Log "Removing local user $User."
42+
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
43+
$adsi.Delete('User', $User)
44+
Move-Item -Path "C:\Users\$User" -Destination "$env:windir\Temp\$User" -Force -ErrorAction Ignore
45+
}

0 commit comments

Comments
 (0)