Function to change variable values

Welcome Forums General PowerShell Q&A Function to change variable values

Viewing 6 reply threads
  • Author
    Posts
    • #269033
      Participant
      Topics: 9
      Replies: 22
      Points: 110
      Rank: Participant

      The below function works as intended when called within the script but doesn’t when called from the command line outside of it. Let me explain.

      This is my function:

      Currently I’m running the following command within the script to select which variable set to use Select-Environment -Tier1. The idea behind this is that I need to use the same variable names to run against some other functions I have in place like the following:

      I need to be able to isolate and call the individual tier variables with different criteria like above rather than the same criteria against the whole tier.

      As mentioned, this works fine when called within the script but when I run the following externally it complains that the ComputerName is null or empty and so doesn’t work: . .\powershellscript.ps1 -Select-Environment -Tier1

      I guess I need to change the function to require a value rather than just a switch?

      Hopefully that makes more sense and gives an idea of what I’m trying to do.

    • #269042
      Participant
      Topics: 9
      Replies: 22
      Points: 110
      Rank: Participant

      I found this thread which says that I need to define a parameter block for the script which accepts (at a minimum) the same parameters I want to pass to the function which makes sense: https://powershell.org/forums/topic/calling-a-function-with-parameters/

      I’ve added the following to the script, outside of the function:

      However, this doesn’t appear to make any difference to calling this outside the script still.

    • #269090
      Participant
      Topics: 9
      Replies: 22
      Points: 110
      Rank: Participant

      I’ve changed my approach in the function:

      This now works by using Select-Environment -Env Tier1 from within the script, returning the correct set of servers but still can’t get this to work outside the script using: . .\powershellscript.ps1; Select-Environment -Env Tier1

      What am I missing?

    • #269099
      Participant
      Topics: 16
      Replies: 1855
      Points: 3,568
      Helping Hand
      Rank: Community Hero

      Switch isn’t really the best choice for this type of parameter. Take a look at Get-Verb, which will show you the verbs recommended by cmdlets\functions with context, which is why it is a recommendation to use ‘Get’ rather than ‘Select’. Using a ValidateSet you can specify the allowed values and then set the parameter to mandatory to ensure that someone has to define that parameter when calling the function. Setting Global variables is almost always bad. A function typically returns a PSObject, not set a global variable. The only time I set a global variable in a function is when a session is created with an API and it would have to be passed to every other function, so it’s persisted globally. Here is another approach:

      Output:

    • #269159
      Participant
      Topics: 9
      Replies: 22
      Points: 110
      Rank: Participant

      Hi Rob,

      I’ve actually moved away from a switch statement and opted for IF statements that compare a string value from a command line param set outside the function now. This works as expected now but thanks for your input anyway.

      I can see how your function is working but your having to specify the -Tier value in the script still whereas I’m trying to do this from the command line while calling the script.

    • #269162
      Participant
      Topics: 16
      Replies: 1855
      Points: 3,568
      Helping Hand
      Rank: Community Hero

      If you want to pass it as a parameter, it just needs to be added to the script:

      Then the parameter can be passed externally:

    • #269264
      Participant
      Topics: 9
      Replies: 22
      Points: 110
      Rank: Participant

      Thanks for that. Very handy to have another (probably better) solution to what I ended up using. I appreciate the help.

Viewing 6 reply threads
  • You must be logged in to reply to this topic.