Help with converting a SKU parameter to a optional variable

ZM 211 Reputation points
2022-09-20T08:42:07.877+00:00

At the moment i have a SKU which is a parameter

[string]  
$CallSKU ="Standard_D32",  

but i want to convert this to a variable where it becomes optional. like if variable is present, i use that SKU otherwise use the default one. but im not sure how to do it properly, could you help me with that

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. SChalakov 10,576 Reputation points MVP Volunteer Moderator
    2022-09-20T09:38:22.93+00:00

    Hi @SakeriyeMohamud-4520,

    by default all Powershell parameters are optional, meaning that you don't have to provide a value by default. So, designing your param section like this, should accomplish the goal:

    From the same article, I've posted previously:

    By default, PowerShell parameters are optional. When a user does not submit arguments to a parameter, PowerShell uses its default value. If no default value exists, the parameter value is $null.

    The easiest way is to check each parameter for $null, then follow whatever logic you have from there. This will result in something like this:

     param(  
     [string]$CallNodeSKU,  
     )     
     if($CallNodeSKU -neq $null)  
     {  
         #Do stuff here  
     }  
    

    or you can use this one:

    $PSBoundParameters
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.2#psboundparameters

    Contains a dictionary of the parameters that are passed to a script or function and their current values. This variable has a value only in a scope where parameters are declared, such as a script or function. You can use it to display or change the current values of parameters or to pass parameter values to another script or function.

    and do it like this:

         param(  
         [string]$CallNodeSKU,  
         )     
         if($PSBoundParameters.ContainsKey("CallNodeSKU"))  
         {  
           #Value has been provided for the param CallNodeSKU  
          #Do stuff here  
         }  
    

    Hope this helps you out.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
    Regards
    Stoyan Chalakov


1 additional answer

Sort by: Most helpful
  1. SChalakov 10,576 Reputation points MVP Volunteer Moderator
    2022-09-20T08:57:38.743+00:00

    Hi,

    I am not quite sure that I fully understand the requirement, so I need to ask:

    You want to pass the value of the $CalNodeSKU paramter to a variable, but only if it is provided as an argument?
    What do you mean exactly with "like if variable is present, i use that SKU otherwise use the default one"?

    I often recommend this article when getting parmas better, it just might answer your quesion:

    Designing Professional Parameters
    https://powershell.one/powershell-internals/attributes/parameters#:~:text=By%20default%2C%20PowerShell%20parameters%20are,the%20parameter%20value%20is%20%24null.

    Has tons of exacmples too.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
    Regards
    Stoyan Chalakov


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.