Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

https://www.roelvanlisdonk.nl/2010/03/25/how-to-call-a-powershell-script-with-named-parameters-from-a-powershell-ps1-script/

https://stackoverflow.com/questions/50124489/how-do-i-get-named-but-undeclared-parameters-in-a-function-in-powershell

https://devblogs.microsoft.com/scripting/simplify-your-powershell-script-with-parameter-validation/

[Parameter(Mandatory=$false)]
[ValidateSet(“TRUE”,”FALSE”)] 
[string]$DOUBLESIDED = "False",

#Validats User Input Only, range is between 21,65
[ValidateRange(21,65)]
[Int]
$Age = 95

[ValidateSet(“1”,”2”,”3”,”4”,”6”,”9”)] #Specific Number Set to Validate vs Rng
Param(
    [Parameter(Mandatory=$false)]
     [string]
    $Computer = "LocalHost", #Default "LocalHost" if not specified
        
   [Parameter(Mandatory=$true)]
   [ValidateSet(“Tom”,”Dick”,”Jane”)] 
   [int]$UserID,				     #Mandatory, but default for [int] = 0

    [Parameter(Mandatory=$false)]
    [string]$UserName,				 #Optional, and can be omitted while stile using "UndefinedArgs"

    [Parameter(Mandatory=$false, ValueFromRemainingArguments=$true)]
    $UndefinedArgs
)


IF ($UndefinedArgs) {
 $UndefinedArgs | ForEach-Object {
  IF ($_ -like '-*') {
   $ObjName = $_ -replace '^-'
  } ELSE {
   Set-Variable -Name "$ObjName" -Value "$_"
   $UndefinedArgsArr = @()
   $UndefinedArgsArr + @([pscustomobject]@{VarName="$ObjName";VarVal="$_"})
  }
 }
}
$UndefinedArgsArr
$READONLY = [bool]$READONLY #https://stackoverflow.com/questions/63734078/collect-undefined-parameters-powershell
ECHO $Computer 
ECHO $UserID
ECHO $UserName
ECHO $FavColor
PAUSE
C:\tmp\call.ps1 -Computer "LocalZHost" -UserID 4574 -UserName "User4574" -FavColor FooBar
C:\tmp\call.ps1 -Computer "LocalZZHost" -UserID 4574 -FavColor FooBar
C:\tmp\call.ps1 -Computer "LocalZZZHost" -UserName "User4574" -FavColor FooBar
C:\tmp\call.ps1 -Computer "LocalZZHost" -UserID 4574 -test1 -FavColor FooBar -test
C:\tmp\call.ps1 -UserID 4574 -UserName "User4574" -FavColor FooBar
  • No labels