Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Problems calling a function

Reply
 
Old 11-07-2007   #1 (permalink)
Kryten


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Calling excel function VB Script
Re: Calling function from script PowerShell
Call depth reached when calling function PowerShell
Search function problems with VIsta Vista General
calling imapi2 function put_MultisessionInterfaces return error Vista General


Vista Forums 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 Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46