![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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. know the script is processed sequentially so if it hasn't reached the definition yet then it does not exist |
My System Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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. |
My System Specs![]() |
![]() |
| 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 | |||