Samples/ArgDemo.ps1
#Load standard arguments from file (even evaluate code) #Run code from PSM-file? #scriptfilename -> domain name/server name -> global settings [CmdletBinding(SupportsShouldProcess = $True)] param ( $pstring, [string]$one, [int64]$isint, [string[]]$two, #[ValidateScript({ $_.GetType().FullName -eq 'System.Management.Automation.PSScriptCmdlet' })] $sessionParam ) #region Init function Get-LocalDefaultVariables { <# .Synopsis Load default arguemts for this PS-file. .DESCRIPTION Get setting files according to load order and set variables. Command prompt arguments will override any file settings .PARAMETER CallerInvocation $MyInvocation of calling code session .PARAMETER defineNew Add ALL variables found in setting files .PARAMETER overWriteExisting Turns the table for variable handling file content will override command line arguments #> [CmdletBinding(SupportsShouldProcess = $False)] param( [parameter(Position=0,mandatory=$true)] $CallerInvocation, [switch]$defineNew, [switch]$overWriteExisting ) foreach($settingsFile in (Get-SettingsFiles $CallerInvocation ".json")) { if (Test-Path $settingsFile) { Write-Verbose "$($MyInvocation.Mycommand) reading: [$settingsFile]" $DefaultParamters = Get-Content -Path $settingsFile -Encoding UTF8 | ConvertFrom-Json | Set-ValuesFromExpressions ForEach($property in $DefaultParamters.psobject.properties.name) { #Exclude PSDefaultParameterValues ("functionName:Variable":"Value") if (($property).IndexOf(':') -eq -1) { $var = Get-Variable $property -ErrorAction SilentlyContinue $value = $DefaultParamters.$property if (!$var) { if ($defineNew) { Write-Verbose "New Var: $property" $var = New-Variable -Name $property -Value $value -Scope 1 } } else { #We only overwrite non-set values if not forced if (!($var.Value) -or $overWriteExisting) { try { Write-Verbose "Var: $property" $var.Value = $value } Catch { $ex = $PSItem $ex.ErrorDetails = "Err adding $property from $settingsFile. " + $PSItem.Exception.Message throw $ex } } } } } } else { Write-Verbose "File not found: [$settingsFile]" } } } #Load default arguemts for this script. #Command prompt arguments will override file settings function GetLocalDefaultsFromDfpFiles($CallerInvocation) { #Load script default settings foreach($settingsFile in (Get-SettingsFiles $CallerInvocation ".dfp")) { Write-Verbose "File: [$settingsFile]" if (Test-Path $settingsFile) { $settings = Get-Content $settingsFile #Enumerate settingsfile rows foreach($row in $settings) { #Remarked lines are not processed if (($row -match "=") -and ($row.Trim().SubString(0,1) -ne "#")) { $key = $row.Split('=')[0] $var = Get-Variable $key -ErrorAction SilentlyContinue if ($var -and !($var.Value)) { try { Write-Verbose "Var: $key" $var.Value = Invoke-Expression $row.SubString($key.Length+1) } Catch { $ex = $PSItem $ex.ErrorDetails = "Err adding $key from $settingsFile. " + $PSItem.Exception.Message throw $ex } } #Write-Host "$($var.Value)" } } } } } get-module PSJumpStart | Remove-Module; Import-Module PSJumpStart -Force #Get Local variable default values from external DFP-files #GetLocalDefaultsFromDfpFiles($MyInvocation) #Get Local variable default values from external JSON-files Get-LocalDefaultVariables $MyInvocation -Verbose #Get global deafult settings when calling modules #$PSDefaultParameterValues = Get-GlobalDefaultsFromDfpFiles($MyInvocation) $PSDefaultParameterValues = Get-GlobalDefaultsFromJsonFiles $MyInvocation -Verbose #endregion Msg "Start Execution" Write-Verbose "Value for One is $one" Msg ("Valuew for Two: " + $two) if ($sessionParam) { Msg ("Notepad PID(s): " + $sessionParam.Id) } else { Msg "No notepad is running" -Type Warning } #Should still be [int64] Write-Verbose "$($isint.GetType().FullName) ; $isint" #$Root = [ADSI]"LDAP://RootDSE" #$Root.rootDomainNamingContext #Get-PSSessionConfiguration Msg "End Execution" |