PoshCode Archive  Artifact [3b12d5aa57]

Artifact 3b12d5aa576cdf6543467c9e15c841e746b0e2babfc0514c5a26c20944287585:

  • File Protect-Variable.ps1 — part of check-in [ef0e8f1d3b] at 2018-06-10 13:18:21 on branch trunk — A function to make it easier (in PowerShell 2) to enforce rules about variables (without making them parameters). (user: Joel Bennett size: 2759)

# encoding: ascii
# api: powershell
# title: Protect-Variable
# description: A function to make it easier (in PowerShell 2) to enforce rules about variables (without making them parameters).
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Protect-Variable
# x-poshcode-id: 2991
# x-archived: 2015-05-29T08:54:36
# x-published: 2012-10-06T21:42:00
#
#
function Protect-Variable {
param(
[Parameter(Mandatory=$true,Position=0)][String]$Name, [Int]$Scope = 1,
[Parameter(ParameterSetName="ValidateScriptBlock")][ScriptBlock]$ScriptBlock,
[Parameter(ParameterSetName="ValidateCount")][Int]$MinCount,
[Parameter(ParameterSetName="ValidateCount")][Int]$MaxCount,
[Parameter(ParameterSetName="ValidateLength")][Int]$MinLength,
[Parameter(ParameterSetName="ValidateLength")][Int]$MaxLength,
[Parameter(ParameterSetName="ValidatePattern")][Regex]$Pattern,
[Parameter(ParameterSetName="ValidateRange")][Int]$MinValue,
[Parameter(ParameterSetName="ValidateRange")][Int]$MaxValue,
[Parameter(ParameterSetName="ValidateSet")][String[]]$Set,
[Parameter(ParameterSetName="ValidateNotNull")][Switch]$NotNull,
[Parameter(ParameterSetName="ValidateNotNullOrEmpty")][Switch]$NotEmpty
)
   $Variable = Get-Variable $Name -Scope 1
   
   if($ScriptBlock) {
      $Attribute = new-object System.Management.Automation.ValidateScriptAttribute $ScriptBlock
      $Variable.Attributes.Add($Attribute)
   }
   if($MinCount -or $MaxCount) {
      if(!$MinCount) { $MinCount = [Int]::MinValue }
      if(!$MaxCount) { $MaxCount = [Int]::MaxValue }
      $Attribute = new-object System.Management.Automation.ValidateCountAttribute $MinCount, $MaxCount
      $Variable.Attributes.Add($Attribute)
   }
   if($MinLength -or $MaxLength) {
      $Attribute = new-object System.Management.Automation.ValidateLengthAttribute $MinLength, $MaxLength
      $Variable.Attributes.Add($Attribute)
   }
   if($Pattern) {
      $Attribute = new-object System.Management.Automation.ValidatePatternAttribute $Pattern
      $Variable.Attributes.Add($Attribute)
   }
   if($MinValue -or $MaxValue) {
      $Attribute = new-object System.Management.Automation.ValidateRangeAttribute $MinValue, $MaxValue
      $Variable.Attributes.Add($Attribute)
   }
   if($Set) {
      $Attribute = new-object System.Management.Automation.ValidateSetAttribute $Set
      $Variable.Attributes.Add($Attribute)
   }
   if($NotNull) {
      $Attribute = new-object System.Management.Automation.ValidateNotNullAttribute
      $Variable.Attributes.Add($Attribute)
   }
   if($NotEmpty) {
      $Attribute = new-object System.Management.Automation.ValidateNotNullOrEmptyAttribute
      $Variable.Attributes.Add($Attribute)
   }
}