OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Variable existence check via $x vs. (Test-Path Variable:x)

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

Guest
In an effort to make our powershell scripts StrictMode compatible, I've made code changes such as:

Code:
[ValidateSet("Debug", "Release", IgnoreCase = $false)]
[Parameter(Mandatory = $false)]
[Alias('c')]    
[string]$Configuration = $Configuration ? $Configuration : "Release",

To this:

Code:
[ValidateSet("Debug", "Release", IgnoreCase = $false)]
[Parameter(Mandatory = $false)]
[Alias('c')]   
[string]$Configuration = (Test-Path Variable:Configuration) ? $Configuration : "Release",

In the param blocks of our scripts. Now I'm getting reports of these values not being properly propagated anymore when they are declared in a parent script (I find this practice a bit questionable, but this pattern is too ubiquitous to fix, for now). As of writing this, I don't have a minimum example to reproduce it as I'm still investigating where and how exactly it's happening, but shouldn't both of these behave the same when $Configuration is already defined? What am I missing?
<p>In an effort to make our powershell scripts StrictMode compatible, I've made code changes such as:</p>
<pre><code>[ValidateSet("Debug", "Release", IgnoreCase = $false)]
[Parameter(Mandatory = $false)]
[Alias('c')]
[string]$Configuration = $Configuration ? $Configuration : "Release",
</code></pre>
<p>To this:</p>
<pre><code>[ValidateSet("Debug", "Release", IgnoreCase = $false)]
[Parameter(Mandatory = $false)]
[Alias('c')]
[string]$Configuration = (Test-Path Variable:Configuration) ? $Configuration : "Release",
</code></pre>
<p>In the param blocks of our scripts. Now I'm getting reports of these values not being properly propagated anymore when they are declared in a parent script (I find this practice a bit questionable, but this pattern is too ubiquitous to fix, for now). As of writing this, I don't have a minimum example to reproduce it as I'm still investigating where and how exactly it's happening, but shouldn't both of these behave the same when $Configuration is already defined? What am I missing?</p>
Continue reading...
 

Latest posts

Top