• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Fun with functions

C

casey.daniell

#1
Ok, I get that powershell functions are better for output rather then
processing in the purest sense of the word, however, what do you do
when you really have to call a function in a script? I have a trival
case and I want to know what I am getting what I get...My guess is
when I call the function and have it's "return" value stored into a
var that it's really storing the function call and parameter to pass,
and not calling the function and returning the result. Only when the
var is used again on the last line is the function executed.

How can I force this function call at the time the script is run and
store the result into the $User var? (Yes, this is a trivial case,
just trying to understand functions better.)

Script
----------------------------------------------------------------------------------------
function bar ($User) {

Write-Output "In function user -- Author: $User`n"
$User = "Casey"
return $User
}

#######
#Main
#######
$User = "Fred Flinstone"
Write-Output "BEGINNING Main Function -- User is: $User`n"

$User = bar $User

Write-Output "ENDING Main Function -- User is: $User`n"

----------------------------------------------------------------------------------------
Output
BEGINNING Main Fuction -- User is: Fred Flinstone


In function user -- Author: Fred Flinstone


Casey
ENDING Main Function -- User is: Fred Flinstone
 

My Computer

R

RichS

#2
I tried your code as your output wasn't what I was expecting and this is
what I got

BEGINNING Main Function -- User is: Fred Flinstone


ENDING Main Function -- User is: In function user -- Author: Fred Flinstone
Casey


which suggests that you are storing the function output in the variable

if I change your script to

function bar ($User) {

Write-Output "In function user -- Author: $User`n"
$User = "Casey"
return $User
}

#######
#Main
#######
$User = "Fred Flinstone"
Write-Output "BEGINNING Main Function -- User is: $User`n"

#$User = bar $User
bar $User

Write-Output "ENDING Main Function -- User is: $User`n"

Then I can duplicate your output. In this case the function is run, the
output is produced and the value of the variable $user is changed

--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"casey.daniell" wrote:

> Ok, I get that powershell functions are better for output rather then
> processing in the purest sense of the word, however, what do you do
> when you really have to call a function in a script? I have a trival
> case and I want to know what I am getting what I get...My guess is
> when I call the function and have it's "return" value stored into a
> var that it's really storing the function call and parameter to pass,
> and not calling the function and returning the result. Only when the
> var is used again on the last line is the function executed.
>
> How can I force this function call at the time the script is run and
> store the result into the $User var? (Yes, this is a trivial case,
> just trying to understand functions better.)
>
> Script
> ----------------------------------------------------------------------------------------
> function bar ($User) {
>
> Write-Output "In function user -- Author: $User`n"
> $User = "Casey"
> return $User
> }
>
> #######
> #Main
> #######
> $User = "Fred Flinstone"
> Write-Output "BEGINNING Main Function -- User is: $User`n"
>
> $User = bar $User
>
> Write-Output "ENDING Main Function -- User is: $User`n"
>
> ----------------------------------------------------------------------------------------
> Output
> BEGINNING Main Fuction -- User is: Fred Flinstone
>
>
> In function user -- Author: Fred Flinstone
>
>
> Casey
> ENDING Main Function -- User is: Fred Flinstone
>
 

My Computer

B

Bob Landau

#3
You are absolutely correct PowerShell is very good at pushing "stuff" down
the pipeline.

However the statement is PowerShell is not good for "processing in the
purest sense" is simply a misunderstanding on your part.

Don't worry I think anyone which has played around with PowerShell has made
the same mistake. Which means all the "high rollers" in this group.

1) Write-Output check the help file. This _writes_ to the pipeline. It did
processed _exactly_ what you had asked it to. It sent the entire contents of
that string through the pipe.

2) Use Write-Host instead. If your especially paranoid like me do the
following because there are times when its not nearly so obvious

> function bar ($User) {
$(

>
> Write-Output "In function user -- Author: $User`n"
> $User = "Casey"
) | Out-Null
$User If you had done this all the output other than what you really wanted $User
would have been discarded. I still don't recommend Write-Output for printing
to the screen.

3) Don't use the return statement; it's 98% unnecessary in Powershell. This
can bite you in other ways


bob

"casey.daniell" wrote:

> Ok, I get that powershell functions are better for output rather then
> processing in the purest sense of the word, however, what do you do
> when you really have to call a function in a script? I have a trival
> case and I want to know what I am getting what I get...My guess is
> when I call the function and have it's "return" value stored into a
> var that it's really storing the function call and parameter to pass,
> and not calling the function and returning the result. Only when the
> var is used again on the last line is the function executed.
>
> How can I force this function call at the time the script is run and
> store the result into the $User var? (Yes, this is a trivial case,
> just trying to understand functions better.)
>
> Script
> ----------------------------------------------------------------------------------------
> function bar ($User) {
>
> Write-Output "In function user -- Author: $User`n"
> $User = "Casey"
> return $User
> }
>
> #######
> #Main
> #######
> $User = "Fred Flinstone"
> Write-Output "BEGINNING Main Function -- User is: $User`n"
>
> $User = bar $User
>
> Write-Output "ENDING Main Function -- User is: $User`n"
>
> ----------------------------------------------------------------------------------------
> Output
> BEGINNING Main Fuction -- User is: Fred Flinstone
>
>
> In function user -- Author: Fred Flinstone
>
>
> Casey
> ENDING Main Function -- User is: Fred Flinstone
>
 

My Computer

K

Keith Hill [MVP]

#4
"casey.daniell" <casey.daniell@xxxxxx> wrote in message
news:9ca3a3db-5c07-410b-9a5c-ea1a4a005a8e@xxxxxx

> Ok, I get that powershell functions are better for output rather then
> processing in the purest sense of the word, however, what do you do
> when you really have to call a function in a script? I have a trival
> case and I want to know what I am getting what I get...My guess is
> when I call the function and have it's "return" value stored into a
> var that it's really storing the function call and parameter to pass,
> and not calling the function and returning the result. Only when the
> var is used again on the last line is the function executed.
>
> How can I force this function call at the time the script is run and
> store the result into the $User var? (Yes, this is a trivial case,
> just trying to understand functions better.)
In addition to Bob's excellent response, you might find this 'Effective
PowerShell: Understanding "Output"' blog post I did to be useful:

http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!811.entry

--
Keith
 

My Computer

H

Hal Rottenberg

#5
Bob Landau wrote:

> I still don't recommend Write-Output for printing to the screen.
And for the original poster, the corollary to Bob's statement is this:

Use Write-Output when you don't know where the output will be going, as Host
only goes to the screen. Remember that you or someone else may want to pipe the
output of your function/script to yet another block of code. Don't necessarily
restrict that later flexibility without good reason.

--

Hal Rottenberg
Blog: http://halr9000.com
Webmaster, Psi (http://psi-im.org)
Co-host, PowerScripting Podcast (http://powerscripting.net)
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)