Thibault
Thibault

Reputation: 892

End a function from a sourced script without exiting script

I have a script with many other scripts sourced. Each script sourced have a function.

One of my function need to stop according to a if statement. I tried continue, break, throw, evrything I saw on internet but either my main script completely end or my function continue.

Function code:

function AutoIndus-DeployUser($path){

    $yaml=Get-Content $path | ?{!$_.StartsWith("#")}

    $UsersPwd=$false

    $user="";$password="";$groups="";$description=""; $options=""

    $yaml | %{

        if (($_-match "}") -and ($UsersPwd -eq $true)){Write-Host "function should stop"; return}

        *actions*
    }
}

Main script:

#functions list and link to extern scripts
Get-ChildItem -Path $PSScriptRoot\functions\ | %{$script=$_.Name; . $PSScriptRoot\functions\$script}

myfunction-doesnotwork

*some code*

Edit: I saw that return should stop the function without stoping the rest of the script, unfortunatly it does not stop anything at all:

Output:

Config file found 

 --- 

Changing user _usr-dba password 

Changing user _usr-dba2 password 

function should stop
Changing user _usr-dba3 password 

function should stop
function should stop
function should stop
function should stop
function should stop
Disabling user DisableAccounts:{ 

Disable-LocalUser : User DisableAccounts:{ was not found.
At C:\Scripts\AWL-MS-AUTOMATION_AND_TOOLS\functions\AutoIndus-DisableAccount.ps1:25 char:13
+             Disable-LocalUser -Name $disable
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (DisableAccounts:{:String) [Disable-LocalUser], UserNotFoundException
    + FullyQualifiedErrorId : UserNotFound,Microsoft.PowerShell.Commands.DisableLocalUserCommand

Disabling user _usr-dba 

To help you understanding, my script read a file and do actions according to each line it is reading. It start acting when it encounter something like User{ and should stop when it encounter }, _usr-dba3 being out of the curvy brackets it should not be treated. This way all my other function use the same one file. (changing user password and diasabling user are two different functions/sourced scripts)

Like you can see return does not do its job, maybe I'm missing one point in the use of return but I don't get it right now.

Upvotes: 0

Views: 118

Answers (2)

Thibault
Thibault

Reputation: 892

I needed a fast solution so I changed my functions, now there is no return/continue/... but a value that turn true. It is initialized to false and the action on each yaml lines are done when the value is equal to false.


Edit: So finally the issue was I didn't used function correctly. Return only works if you return into another function. So I putted almost the entire sctipt into a function that calls the function AutoIndus-DeployUser. Now it works perfectly !

Upvotes: 0

LPG
LPG

Reputation: 434

To prematurely exit out of a function before you reach the end of it, use

return

it works like break to where it will stop execution but instead of stopping the whole thread it will return back to the line below where you called the function.

Also keep in mind if your function has an output set return will return it back, for example with the function below:

function Get-ReturnValue() {

$returnValue = "this is my output"

return $returnValue

}

if you called it like this:

$receivedReturnValue = Get-ReturnValue

then the variable receivedReturnValue would be loaded with the output of the function.

Upvotes: 1

Related Questions