Utilities/Environment.ps1
function Test-Environment { param ( [ValidateSet('Variable','Registry','File','Folder','Executable','Module')] [string]$Type, [string]$Name ) switch ($Type) { 'Variable' { return [bool]${env:$Name} } 'Registry' { return Test-Path -Path $Name } 'File' { return Test-Path -Path $Name -PathType Leaf } 'Folder' { return Test-Path -Path $Name -PathType Container } 'Executable' { return [bool](Get-Command $Name -ErrorAction SilentlyContinue) } 'Module' { return [bool](Get-Module -Name $Name -ListAvailable) } } } function Ensure-Environment { param ( [ValidateSet('Variable','Registry','File','Folder','Executable','Module')] [string]$Type='Module', [string]$Name, [switch]$SetIfMissing = $false, [string]$Value = $null ) if (-not (Test-GsEnvironment -Type $Type -Name $Name)) { Write-GsLog -Message "$Type '$Name' not found." -LogLevel 'Warning' if ($SetIfMissing) { if ( (-not $Value) -and ($Type -ne 'Module') -and ($Type -ne 'Folder')) { Write-GsLog -Message "Value is required to set $Type '$Name'." -LogLevel 'Error' return $false } Set-GsEnvironment -Type $Type -Name $Name -Value $Value Write-GsLog -Message "$Type '$Name' created." return $true } return $false } return $true } Export-ModuleMember -Function '*' |