Skip to content

Commit

Permalink
DSOS-2581: active directory and rd license pwsh (#558)
Browse files Browse the repository at this point in the history
* Add ModPlatformRemoteDesktop pwsh module

* add RDLicensing and DomainController powershell

* test

* fix

* fix

* formatting

* fix exit code

* fix
  • Loading branch information
drobinson-moj authored Feb 14, 2024
1 parent 63401c5 commit 72072e4
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 12 deletions.
12 changes: 6 additions & 6 deletions powershell/Modules/ModPlatformAD/ModPlatformADComputer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ function Rename-ModPlatformADComputer {
Rename-Computer -NewName $NewHostname -DomainCredential $ModPlatformADCredential -Force
}
Write-Host "INFO: Renaming EC2 instance to $NewHostname and then rebooting"
return $NewHostname
Return $NewHostname
} else {
return $null
Return $null
}
}

Expand Down Expand Up @@ -96,7 +96,7 @@ function Add-ModPlatformADComputer {
if ((Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain) {
$ExistingDomain = (Get-WmiObject -Class Win32_ComputerSystem).Domain
if ($ExistingDomain -eq $DomainNameFQDN) {
return $false
Return $false
}
}

Expand All @@ -110,7 +110,7 @@ function Add-ModPlatformADComputer {

Write-Host "INFO: Joining $env:COMPUTERNAME to ${DomainNameFQDN} domain"
Add-Computer -DomainName $DomainNameFQDN -Credential $ModPlatformADCredential -Verbose -Force
return $true
Return $true
}

function Remove-ModPlatformADComputer {
Expand Down Expand Up @@ -142,7 +142,7 @@ function Remove-ModPlatformADComputer {

# Do nothing if host not part of domain
if (-not (Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain) {
return $false
Return $false
}

# Install powershell features if missing
Expand All @@ -155,7 +155,7 @@ function Remove-ModPlatformADComputer {
$DomainNameFQDN = (Get-WmiObject -Class Win32_ComputerSystem).Domain
Write-Host "INFO: Removing $env:COMPUTERNAME from ${DomainNameFQDN} domain"
Remove-Computer -Credential $ModPlatformADCredential -Verbose -Force
return $true
Return $true
}

Export-ModuleMember -Function Rename-ModPlatformADComputer
Expand Down
2 changes: 1 addition & 1 deletion powershell/Modules/ModPlatformAD/ModPlatformADConfig.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function Get-ModPlatformADConfig {
break
}
}
return $ConfigCopy
Return $ConfigCopy
} else {
Write-Error "No matching configuration for domain ${Key}"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
function Get-ModPlatformRDLicensingCompanyInformation() {
<#
.SYNOPSIS
Return hashtable with RD Licensing company information
.OUTPUTS
hashtable
#>
[CmdletBinding()]
param ()

$ErrorActionPreference = "Stop"

$CompanyInformation = @{
"FirstName" = "Modernisation"
"LastName" = "Platform"
"Company" = "Ministry of Justice"
"CountryRegion" = "United Kingdom"
}
$CompanyInformation
}

function Add-ModPlatformRDLicensingActivation() {
<#
.SYNOPSIS
Activate RDLicensing server if it isn't already activated
.PARAMETER CompanyInformation
HashTable containing company information to set
.EXAMPLE
Add-ModPlatformRDLicensingActivation (Get-ModPlatformRDLicensingCompanyInformation)
#>
[CmdletBinding()]
param (
[hashtable]$CompanyInformation
)

$ErrorActionPreference = "Stop"
$LicenseServerWMIObject = Get-WMIObject Win32_TSLicenseServer
$LicenseServerWMIClass = [wmiclass]($LicenseServerWMIObject.__PATH.Split("=")[0])
if ($LicenseServerWMIClass.GetActivationStatus().ActivationStatus -ne 0) {
if ($CompanyInformation) {
$CompanyInformation.Keys | ForEach-Object {
$LicenseServerWMIObject[$_] = $CompanyInformation[$_]
}
$LicenseServerWMIObjectPut = $LicenseServerWMIObject.Put()
}
$Activated = $LicenseServerWMIClass.ActivateServerAutomatic()
}
}

function Remove-ModPlatformRDLicensingActivation() {
<#
.SYNOPSIS
De-activate RDLicensing server if it isn't already de-activated
.EXAMPLE
Remove-ModPlatformRDLicensingActivation
#>
[CmdletBinding()]
param ()

$ErrorActionPreference = "Stop"
$LicenseServerWMIObject = Get-WMIObject Win32_TSLicenseServer
$LicenseServerWMIClass = [wmiclass]($LicenseServerWMIObject.__PATH.Split("=")[0])
if ($LicenseServerWMIClass.GetActivationStatus().ActivationStatus -eq 0) {
$Deactivated = $LicenseServerWMIClass.DeactivateServerAutomatic()
}
}

Export-ModuleMember -Function Get-ModPlatformRDLicensingCompanyInformation
Export-ModuleMember -Function Add-ModPlatformRDLicensingActivation
Export-ModuleMember -Function Remove-ModPlatformRDLicensingActivation
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#
# Module manifest for module 'ModPlatformRemoteDesktop'
#
# Generated by: Ministry of Justice
#
# Generated on: 13/02/2024
#

@{

# Script module or binary module file associated with this manifest.
# RootModule = ''

# Version number of this module.
ModuleVersion = '1.0.0.0'

# Supported PSEditions
# CompatiblePSEditions = @()

# ID used to uniquely identify this module
GUID = 'b0315bc0-a20c-4c40-a6c1-c4db9507f111'

# Author of this module
Author = 'Ministry of Justice'

# Company or vendor of this module
CompanyName = 'Ministry of Justice'

# Copyright statement for this module
Copyright = '(c) 2024 Crown Copyright (Ministry of Justice)'

# Description of the functionality provided by this module
Description = 'Modernisation Platform ModPlatformRemoteDesktop module'

# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '4.0'

# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @('ModPlatformRDLicensing.psm1')

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Get-ModPlatformRDLicensingCompanyInformation',
'Add-ModPlatformRDLicensingActivation',
'Remove-ModPlatformRDLicensingActivation'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = '*'

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()

# A URL to the license for this module.
# LicenseUri = ''

# A URL to the main website for this project.
# ProjectUri = ''

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false

# External dependent modules of this module
# ExternalModuleDependencies = @()

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

9 changes: 9 additions & 0 deletions powershell/Modules/ModPlatformRemoteDesktop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Provides Remote Desktop related functions

For example,

```
Get-ModPlatformRDLicensingCompanyInformation - get RD licensing company information
Add-ModPlatformRDLicensingActivation - activate RD licensing
Remove-ModPlatformRDLicensingActivation - deactivate RD licensing
```
4 changes: 2 additions & 2 deletions powershell/Scripts/ModPlatformAD/Join-ModPlatformAD.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ $ADCredential = Get-ModPlatformADCredential -ModPlatformADConfig $ADConfig -Acco
$Renamed = Rename-ModPlatformADComputer -NewHostname $NewHostname -ModPlatformADCredential $ADCredential
if ($Renamed) {
Write-Output "Renamed computer to ${Renamed}"
exit 3010 # triggers reboot if running from SSM Doc
Exit 3010 # triggers reboot if running from SSM Doc
}
if (Add-ModPlatformADComputer -ModPlatformADConfig $ADConfig -ModPlatformADCredential $ADCredential) {
exit 3010 # triggers reboot if running from SSM Doc
Exit 3010 # triggers reboot if running from SSM Doc
}
2 changes: 1 addition & 1 deletion powershell/Scripts/ModPlatformAD/Leave-ModPlatformAD.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ $ErrorActionPreference = "Stop"
$ADConfig = Get-ModPlatformADConfig -DomainNameFQDN $DomainNameFQDN
$ADCredential = Get-ModPlatformADCredential -ModPlatformADConfig $ADConfig -AccountIdsSSMParameterName $AccountIdsSSMParameterName
if (Remove-ModPlatformADComputer -ModPlatformADCredential $ADCredential) {
exit 3010 # triggers reboot if running from SSM Doc
Exit 3010 # triggers reboot if running from SSM Doc
}
5 changes: 3 additions & 2 deletions powershell/Scripts/Run-GitScript.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ $GitRepo = "modernisation-platform-configuration-management"

if (-not (Get-Command "git" -ErrorAction SilentlyContinue)) {
Write-Error "Please install git, e.g. choco install git.install -y"
exit 1
Exit 1
}

if (-Not $GitCloneDir) {
if (-not $GitCloneDir) {
$GitCloneDir = [System.IO.Path]::GetTempPath()
}

Expand Down Expand Up @@ -66,6 +66,7 @@ if ($Script) {
$ScriptFilename = Split-Path -Leaf $Script
Set-Location -Path "powershell/Scripts/$RelativeScriptDir"
. ./$ScriptFilename @ScriptArgs
Exit $LASTEXITCODE
} else {
Set-Location -Path powershell/Scripts
}
8 changes: 8 additions & 0 deletions powershell/Scripts/UserDataScripts/DomainController.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$ErrorActionPreference = "Stop"

. ../ModPlatformAD/Join-ModPlatformAD.ps1
if ($LASTEXITCODE -ne 0) {
Exit $LASTEXITCODE
}

Install-WindowsFeature AD-Domain-Services -IncludeAllSubFeature -IncludeManagementTools
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
. ../ModPlatformAD/Join-ModPlatformAD.ps1
Exit $LASTEXITCODE
12 changes: 12 additions & 0 deletions powershell/Scripts/UserDataScripts/RDLicensing.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$ErrorActionPreference = "Stop"

. ../ModPlatformAD/Join-ModPlatformAD.ps1
if ($LASTEXITCODE -ne 0) {
Exit $LASTEXITCODE
}

Install-WindowsFeature RDS-Licensing -IncludeAllSubFeature -IncludeManagementTools

Import-Module ModPlatformRemoteDesktop -Force
$CompanyInformation = Get-ModPlatformRDLicensingCompanyInformation
Add-ModPlatformRDLicensingActivation $CompanyInformation

0 comments on commit 72072e4

Please sign in to comment.