Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Last active November 22, 2023 20:43
Show Gist options
  • Save IISResetMe/000abb51aaebfa9b4c25d15ec9fc47c9 to your computer and use it in GitHub Desktop.
Save IISResetMe/000abb51aaebfa9b4c25d15ec9fc47c9 to your computer and use it in GitHub Desktop.
function Get-AvailableLicenseTemplates {
[CmdletBinding()]
param ()
process {
Write-Host "`$PSScriptRoot:" $PSScriptRoot -ForegroundColor Green
# Define the parent directory containing the folders
$ParentDirectory = "$PSScriptRoot\..\Templates\LICENSE\"
# Get the list of folders
$Folders = Get-ChildItem -Path $ParentDirectory -Directory
# Create an array of custom objects
$FolderDetails = $Folders | ForEach-Object {
[PSCustomObject]@{
Name = $_.Name # Folder name
Path = $_.FullName # Full path of the folder
}
}
$FolderDetails | Sort-Object -Descending
}
}
-----------------------------------------------------------------------
using namespace System.Management.Automation
class AvailableLicenseTemplates : IValidateSetValuesGenerator {
[string[]] GetValidValues() {
$v = Get-AvailableLicenseTemplates | Sort-Object -Descending
$LicenseTitles = @()
foreach ($License in $v) {
$LicenseTitles += $License.Name
}
return $LicenseTitles
}
}
function Save-LicenseToFolder {
[CmdletBinding()]
param (
[Parameter(Mandatory,ValueFromPipeline)]
$Folder,
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[ValidateSet([AvailableLicenseTemplates])]
[String]
$LicenseType,
[Parameter(Mandatory=$false)]
[Switch]
$Force
)
begin {
$LicMaster = Get-AvailableLicenseTemplates | Sort-Object -Descending
foreach ($idx in $LicMaster) {
if($idx.Name -eq $LicenseType){
$CurrentLicense = $idx
break
}
}
}
process {
$LicenseExists = Test-Path -LiteralPath (Join-Path $Folder LICENSE) -PathType Leaf
$LicenseTemplatePath = Join-Path $CurrentLicense.Path LICENSE
if((!$LicenseExists) -or $Force){
if (!(Test-Path $Folder -PathType Container)) {
New-Item -ItemType Directory -Path $Folder -Force |Out-Null
}
Copy-Item $LicenseTemplatePath -Destination $Folder
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment