header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Putting functions in thier place
Last Post 31 Oct 2007 03:56 PM by DonJ. 8 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
JosiahwwwUser is Offline
New Member
New Member
Posts:15
Avatar

--
29 Oct 2007 10:30 PM  

Does the code for functions have to come before before you use or call it. I'm getting an error when running the following script

$a = "comp1", "comp2"

foreach($i In $a)
{
 write $i
 #call function
 getnumber
}

function getnumber{return 10}

ERROR: The term 'getnumber' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try
 again.
At C:\Documents and Settings\jwoodhouse\My Documents\Scripts\Powershell\test.ps1:19 char:11
+     getnumber <<<<

However this works.

$a = "comp1", "comp2"

function getnumber{return 10}

foreach($i In $a)
{
 write $i
 #call function
 getnumber
}

What am i missing.

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
29 Oct 2007 10:35 PM  
functions must be defined before use. Its not like vbscript
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
JosiahwwwUser is Offline
New Member
New Member
Posts:15
Avatar

--
29 Oct 2007 10:52 PM  
I think i'm struggling with the mental leap from scripting back to shell scripting.
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
29 Oct 2007 10:58 PM  
I know how ya feel.

Don't think of it as scripting as much as typing a bunch of commands in a file and running it.
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
PoshoholicUser is Offline
PowerShell MVP
New Member
New Member
Posts:38
Avatar

--
30 Oct 2007 05:36 AM  

Just FYI, you can do this in PowerShell with a little bit of extra effort.  Someone pointed this out in the PowerShell newsgroup recently (I forget who posted it otherwise I would give credit where it's due).  In your PS1 file, you could have something like this:

function main {
   Write-Hello
   Write-World
}

function Write-Hello {
   Write-Host "Hello"
}

function Write-World {
   Write-Host "World"
}

main

This allows you to show the core logic at the top of the file and the functions that will be called below that core logic, which is what I believe you wanted to do.

-
Kirk Munro
Poshoholic
http://poshoholic.com

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
30 Oct 2007 01:10 PM  
I don’t know... While I can certainly appreciate the ingenuity that came up with that method, I am not sure it is wise to actually suggest it. The problem with new technology is that people try to squeeze into what they already know. I think it is absolutely critical to understand and use the product the way it was designed.

I think it is fundamentally important to understand (as a poster/blogger) that your suggestions could lead a new Powershell user down the wrong path with a fundamental misunderstanding of how the product works.

I may just be paranoid, but I think it is a valid point.
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
PoshoholicUser is Offline
PowerShell MVP
New Member
New Member
Posts:38
Avatar

--
30 Oct 2007 01:54 PM  
Isn't that overthinking it a little Brandon?  How is the suggestion not using the product like it was designed?  It isn't a hack, at least not in my opinion.  It's just a creative way to allow ps1 files to show their core logic at the top, and when functions names are self-documenting, this can simplify things when looking at long script files to see what they were intended to do.  I don't write my scripts this way but I understand that someone who is used to being able to do that might like that they can continue to do so in PowerShell.  PowerShell is very flexible and can be used in many different ways.  There's the whole verbose vs. pithy approach to using PowerShell.  It is important that script authors understand both so that they can use whatever method is appropriate to the task they are trying to accomplish and so that they can understand other examples and scripts that are on the web.

-
Kirk Munro
Poshoholic
http://poshoholic.com
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
30 Oct 2007 02:27 PM  
You may be correct on the overthinking, I don't think so :)

I do think it could be potentially dangerous and misleads a new user in the way the product works while possibly wasting hours of their time debugging something they fundamentally misunderstand.

For others that is ok but for folks like us, that are vocal in the community, it could be misleading. I think it is important to understand you will not always get a chance to explain what you’re doing and why. Sometimes it is left for the Powersheller down the road that finds your code to figure out.

It is ABSOLUTELY critical for those vocal in the community to understand who Powershell is direct towards. It is not developers. It is not even scripter’s. It is for admins. It should be as intuitive and simple as possible. One should not have to fully understand functions, scripts, variables or even objects to start using Powershell. Granted... for the general population now... this is ok because they are early adopters and normally understand these concepts, but when you write you should think of ALL the people, present and future, that may read this. Fundamentals are KEY!

Anyway... This is way OT and I will drop it now :)
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
DonJUser is Offline
PowerShell MVP
Basic Member
Basic Member
Posts:134
Avatar

--
31 Oct 2007 03:56 PM  
Posted By bsonposh on 10/29/2007 2:35 PM
functions must be defined before use. Its not like vbscript

Interestingly.... it really is. VBScript does require that functions be defined before they can be used. It's just that the interpreter does a pre-scan before executing your script and logically "floats" variable, sub, and function declarations to the top for you, behind the scenes. PowerShell exhibits a much more shell-like behavior and doesn't pre-scan - it just jumps in and starts executing.
- Don Jones
www.ConcentratedTech.com
Subscribe (RSS) or visit for weekly PowerShell tips and lessons
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer