Skip to content

Commit

Permalink
add bosh-psmodules to stembuild
Browse files Browse the repository at this point in the history
- 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
XenoPhex committed Nov 19, 2020
1 parent a3ae3e5 commit 1d9be07
Show file tree
Hide file tree
Showing 47 changed files with 5,180 additions and 0 deletions.
47 changes: 47 additions & 0 deletions modules/BOSH.Account/BOSH.Account.Tests.ps1
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
21 changes: 21 additions & 0 deletions modules/BOSH.Account/BOSH.Account.psd1
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'
}
}
}
45 changes: 45 additions & 0 deletions modules/BOSH.Account/BOSH.Account.psm1
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
}
Loading

0 comments on commit 1d9be07

Please sign in to comment.