Forum Discussion
Return command is not coming out of function and not returning value
- Dec 10, 2019
SBVRaja I several gotchas which may explain the behavior you're seeing...
- Using recursive calls to CFile is probably not the best approach, especially from inside the catch {} block.
- The $Done variable is never properly initialized, and the only possible return value is "Good job" for $Required.
- I might recommend moving any statements referencing $JSONformat inside your try {} block.
- Your $title argument to the CFile function is never used.
- If possible, avoid using Write-Host; and instead try using either Write-Verbose, Write-Output, Write-Information, Write-Warning, etc.
- Since your Switch() statement only handles 2 conditions, an if() {} else {} block might be cleaner.
- Return($Done) is mostly equivalent to: $Done; Return; Since this is the last statement in your function, Return is really unnecessary.
That said, here's one possible re-write:
function Cfile { [cmdletbinding()] param([string]$title = 'Enter the path of the file') begin { $done = 'Bad job' } process { Do { try { $filepath = Read-host $title $JSONformat = (get-content -Path $filepath -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop) Write-Verbose "Valid JSON format`nEntered baseline version is: $($JSONformat.version)" } catch { Write-Warning 'Missing file, or provided text is not a valid JSON string' } $confirm = Read-Host 'Is that correct?? (Y/N)' if ('Y' -eq $confirm) { $done = 'Good job' } else { $title = 'Select Correct Config file' } } Until ('Bad job' -ne $done) $done } end {} } $Required = Cfile -Verbose Write-Output $Required
Hope this helps.
JRCigars Thanks for looking into this and your time JRCigars. I really appreciate that. My actual problem here is not to find alternate solutions (As mentioned, I already had an alternate solution and I also believe there are many other solutions too just like Do-While you used which is also a great approach which I didn't think). But my actual cause here is to understand the behavior of 'Return' and put a '?' at where ever I don't have a proper answer
Considering this, here are my responses to your 7 points with '?' .. 🙂
1) Using recursive calls to CFile is probably not the best approach, especially from inside the catch {} block.
Raja: Understood. I may agree with you on this. But WHY?
2) The $Done variable is never properly initialized, and the only possible return value is "Good job" for $Required.
Raja: I don't think it is a big issue (in fact not a issue at all). In fact, the code that i gave is very simplified version of actual bulk code. These $Done, $title, the function CFile itself are all its simplified versions.
3) I might recommend moving any statements referencing $JSONformat inside your try {} block
Raja: I completely agree. This is actually simplifying the issue completely.
4) Your $title argument to the CFile function is never used.
Raja: I don't think its of big issue (in fact not a issue at all). In actual code, I generally use this variable to display while reading $filepath in try{} block as this function 'Cfile' i use for multi purpose. In fact, the code that i gave is very simplified version of actual bulk code. These $Done, $title, the function CFile itself are all its simplified versions.
5) If possible, avoid using Write-Host; and instead try using either Write-Verbose, Write-Output, Write-Information, Write-Warning, etc.
Raja: I would consider it. I will study this alternate approach
6) Since your Switch() statement only handles 2 conditions, an if() {} else {} block might be cleaner
Raja: I agree. But in actual code, there are 3 more conditions that it actually verifies.
7) Return($Done) is mostly equivalent to: $Done; Return;
Raja: I agree. But with the use of Return why the engine is not moving out of function??