Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Problems calling a function

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-07-2007   #1 (permalink)
Kryten
Guest


 

Problems calling a function

Hi,
I'm having trouble calling a function from within a bunch of if
statements. Basically if the user
enters 50 then I want to break out and start the function
"listallaccounts" which is found about 120 lines
further down the script. I dont want to pass any arguments to the
listallaccounts function as it contains the
code to get the required input.

It looks like this:-

if ($servname -eq "20") {do this etc}
if ($servname -eq "21") {do this etc}
if ($servname -eq "50") {listallaccounts}
if ($servname -eq "50") {do this etc}



Function listallaccounts {
cls
Write-Host "This will retrieve information about user accounts on the
remote server..."
Write-Host
$retrievefrom = Read-Host "Enter the computername that you want to
scan?"
$usethisaccountforthescan = Read-Host "Enter the user account on that
server which authorises you to do this?"
$a=get-wmiobject -ComputerName $retrievefrom -class Win32_UserAccount -
credential $usethisaccountforthescan
cls
$a | format-list -Property name
exit
}

However no matter what I seem to try the script just trips over the
call to the function and goes onto the next line
and never gets to the function I wanted.


Would appreciate any help!

Also has anyone got the new Lee Holmes PowerShell Cookbook yet? Is it
any good?

Regards,
K


My System SpecsSystem Spec
Old 11-07-2007   #2 (permalink)
Bob Butler
Guest


 

Re: Problems calling a function

"Kryten" <Kryten68@xxxxxx> wrote in message
news:1194450397.999710.302480@xxxxxx
Quote:

> Hi,
> I'm having trouble calling a function from within a bunch of if
> statements. Basically if the user
> enters 50 then I want to break out and start the function
> "listallaccounts" which is found about 120 lines
> further down the script.
try moving the function definition to a place before the call;as far as I
know the script is processed sequentially so if it hasn't reached the
definition yet then it does not exist




My System SpecsSystem Spec
Old 11-07-2007   #3 (permalink)
Maximilian Hänel
Guest


 

Re: Problems calling a function

Hi Kryten,
Quote:

> It looks like this:-
>
> if ($servname -eq "20") {do this etc}
> if ($servname -eq "21") {do this etc}
> if ($servname -eq "50") {listallaccounts}
> if ($servname -eq "50") {do this etc}
>
>
>
> Function listallaccounts {
[...]

As an alternative to what Bob said you can organize your script like
this conventional schema:

function Main()
{
if ($servname -eq "20") {do this etc}
if ($servname -eq "21") {do this etc}
if ($servname -eq "50") {listallaccounts}
if ($servname -eq "50") {do this etc}
}

function listallaccounts()
{
}

.. Main

This frees you from the needs to place all your helper function at the
top of the script.

hth

Max
My System SpecsSystem Spec
Old 11-07-2007   #4 (permalink)
Kryten
Guest


 

Re: Problems calling a function

lol What an embarassment!
Thanks.....works perfectly now.

$idiot=note-self ("put your functions first dopey!")

Thanks again,
K

My System SpecsSystem Spec
Old 11-07-2007   #5 (permalink)
Bob Butler
Guest


 

Re: Problems calling a function

"Maximilian Hänel" <ngSpam@xxxxxx> wrote in message
news:OSi8c1VIIHA.280@xxxxxx
<cut>
Quote:

> This frees you from the needs to place all your helper function at the top
> of the script.
Good idea!


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Call depth reached when calling function Victag PowerShell 1 06-03-2008 11:55 PM
Uninstall program function problems SSI Vista General 10 07-15-2007 10:09 AM
Search function problems with VIsta Chuck Vista General 4 05-03-2007 07:35 PM
calling imapi2 function put_MultisessionInterfaces return error caijx Vista General 0 04-24-2007 11:40 PM
BUG: Redirecting function contents to a file truncates function lines at the width of the console Adam Milazzo PowerShell 2 08-11-2006 10:58 PM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008