Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # all FAIL if $var is undefined under "Set-StrictMode -version latest"
- if ( !$var ) { $var = "new-value"; }
- if ( $var -eq $null ) { $var = "new-value"; }
- if (!(test-path variable:var)) {$var = $null} # test for EXISTENCE & create
- if ( !$var ) { $var = "new-value"; } # test the VALUE
- $var = & { Set-StrictMode -off; switch( $var ) { $null { "new-value" } default { $var } }}
- & { Set-StrictMode -off; if (!$var) { set-variable -scope 1 var "new-value" }}
- function set-Variable-IfMissingOrNull ($name, $value)
- {
- $isMissingOrNull = !(test-path ('variable:'+$name)) -or ((get-variable $name -value) -eq $null)
- if ($isMissingOrNull) { set-variable -scope 1 $name $value }
- }
- set-alias ?? set-Variable-IfMissingOrNull
- #...
- ## in use, must not have a leading $ or the shell attempts to read the possibly non-existant $var
- set-VarIfMissingOrNull var "new-value"
- ?? varX 1
- function test-variable
- {# return $false if variable:$name is missing or $null
- param( [string]$name )
- $isMissingOrNull = (!(test-path ('variable:'+$name)) -or ((get-variable -name $name -value) -eq $null))
- return !$isMissingOrNull
- }
- set-alias ?-var test-variable
- if (!(?-var var)) {$var = "default-value"}
- Set-Variable
- -- ReadOnly: Cannot be deleted or changed without the Force parameter.
- -- Constant: Cannot be deleted or changed. Constant is valid only when
- creating a new variable. You cannot set the Constant option on an
- existing variable.
Add Comment
Please, Sign In to add comment