Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to have my functions written first in my PowerShell script?

Tags:

powershell

I have a script that I am utilizing functions to wrap parts of the code that allow me to move through the sections at a specified point. What I have found is that I have to have the functions listed first in the script for it to run correctly.

Non-working example

$stepChoice = read-host 'Where would you like to start.'  switch($stepChoice) {     1{Step1}     2{Step2}     3{Step3}  }  # Steps.ps1  function Step1 {      'Step 1'      Step2  }  function Step2 {      'Step 2'      Step3  }  function Step3 {      'Step 3'      'Done!'  } 

Error

This give me the following error:

The term 'Step1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

 At C:\Tools\Scripts\functiontest.ps1:7 char:12   +     1{Step1 <<<< }   + CategoryInfo          : ObjectNotFound: (Step1:String) [], CommandNotFoundException   + FullyQualifiedErrorId : CommandNotFoundException* 

Working example

If I change the order around it works fine:

# Steps.ps1  function Step1 {      'Step 1'      Step2  }  function Step2 {      'Step 2'      Step3  }  function Step3 {      'Step 3'      'Done!'  }  #steps $stepChoice = read-host 'Where would you like to start.'  switch($stepChoice) {     1{Step1}     2{Step2}     3{Step3}  } 

Why?

I am guessing that it is because PS is not loading the functions.

Why is this and is there a better way to lay out this code structure?

like image 265
Dan Snell Avatar asked Oct 12 '10 17:10

Dan Snell


People also ask

Should PowerShell functions be at the top?

PowerShell is a script, not a compiled language. Therefore, it goes through the script line-by-line, top to bottom, (after tokenizing the script) and evaluates each command along the way.

What must you set before you can run a PowerShell script?

Before you can run a script on Windows, you need to change the default PowerShell execution policy. Execution policy does not apply to PowerShell running on non-Windows platforms. The default execution policy, Restricted , prevents all scripts from running, including scripts that you write on the local computer.

How do you write a function in a PowerShell script?

A simple functionA function in PowerShell is declared with the function keyword followed by the function name and then an open and closing curly brace. The code that the function will execute is contained within those curly braces. The function shown is a simple example that returns the version of PowerShell.

How do PowerShell functions work?

A function in PowerShell is a grouping of code that has an optional input and output. It's a way of collecting up a bunch of code to perform one or many different times by just pointing to it instead of duplicating that code repeatedly.


1 Answers

Remember that in general, what works in a script should work at the command line.

This was not true in CMD. GOTO and FOR %I IN (...) DO %%I are two examples.

In PowerShell, I can run commands at the command line until I get the result I want, then paste the history in to a script, then edit out the extraneous bits.

Also, I can take a script that isn't working correctly, paste it in to an interactive shell, and study the resulting state.

At the interactive command line, there's no way you could write this:

 F function F { "Hello, World!" } 

However, when reading a script, I want to read the top-level code first, and then see more detail as I scroll down. One approach is:

 function Main  {     F }  function F {     "Hello, World!" }  Main 
like image 115
Jay Bazuzi Avatar answered Sep 21 '22 01:09

Jay Bazuzi