Sometimes the logic is more involved than file existence. Also just trying to illustrate returning multiple values to caller
On Sep 23, 2016, at 3:05 PM, Michael B. Smith <mich...@smithcons.com> wrote: Is there a particular reason that you don’t use the Validate*() variable decorators? From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On Behalf Of It's Mike Mitchell Sent: Friday, September 23, 2016 5:51 PM To: powershell@lists.myitforum.com Subject: [powershell] RE: Functions, Scoping, and Returns I often use a validation function to ensure the script’s parameters are correct. It looks for errors and sets a count and message when appropriate. I use the explicit scoping (“script:”) to ensure those values are available to the caller. $fileName = "bogus file name" $iError = 0 function ValidateParameters { $message = "" if (-not (test-path $fileName)) # $fileName is from calling scope { $message += "Yikes! Cannot find [$fileName]`n" $iError++ # this is a local copy of the globally known '$iError' } $script:iError = $iError # make the local values known $script:sError = $message } # ValidateParameters ValidateParameters if ($iError -gt 0) { "Error count: `t$iError" "Error message:`t$sError" } From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On Behalf Of Orlebeck, Geoffrey Sent: Friday, September 23, 2016 2:18 pm To: 'powershell@lists.myitforum.com' <powershell@lists.myitforum.com> Subject: [powershell] RE: Functions, Scoping, and Returns So adding the dot source to Caller.ps1 works from the standpoint of allowing me to call ‘Add-NewUser’ and it load the ‘Enable-UserMailbox’ function within the ‘Add-NewUser’ function. So I’ll read up some more about dot sourcing because why that works is not entirely clear to me yet. I am still facing the issue of returning the output. I’ve seen references (Don Jones) that Write-Host is awful. At the moment, Write-Host is the only way I can get the output to return to the shell. I was under the impression Write-Output is the better idea—or is that only if you need to pass it down the pipeline? Example—below has Write-Output and Write-Host: <image005.png> I also tried to have $MailboxResult returned to the parent, but it doesn’t show up in the output (along with the missing ‘Write-Output’). If I just misinterpreted Don Jones’s point, I can use Write-Host, though I’m still stumped on how to pass a pass/fail back to the parent function (via $MailboxResult): <image006.png> From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On Behalf Of Michael B. Smith Sent: Friday, September 23, 2016 1:17 PM To: powershell@lists.myitforum.com Subject: [powershell] RE: Functions, Scoping, and Returns Given the way you are trying to do this, you also need to dot source caller.ps1. That is . .\caller.ps1 And then you can call the function individually. From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On Behalf Of Orlebeck, Geoffrey Sent: Friday, September 23, 2016 4:08 PM To: 'powershell@lists.myitforum.com' Subject: [powershell] RE: Functions, Scoping, and Returns So I just tried that: <image007.png> But when I run the Caller.ps1 I got this: <image008.png> So I tried calling it as “.\add-newuser” but nothing happened. In both scripts I have the functions, but I wasn’t calling them within the script. When I added the command to the end of each .ps1 file it ran the Add-NewUser but Enable-UserMailbox was not recognized because it had not loaded into memory I assume: <image009.png> Do I need to dot source the Enable-UserMailbox call within the Add-NewUser function? Or is there something else I’m missing. Attaching the script files that generated the above. From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On Behalf Of Michael B. Smith Sent: Friday, September 23, 2016 12:05 PM To: powershell@lists.myitforum.com Subject: [powershell] RE: Functions, Scoping, and Returns ISE is your issue. It doesn’t hand write-host the way you expect (personally, I consider it buggy, but the PG disagrees). Create a file “caller.ps1” consisting of two lines: . .\file1.ps1 . .\file2.ps1 And run it from a PowerShell session .\caller And see if it doesn’t behave as you expect. From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On Behalf Of Orlebeck, Geoffrey Sent: Friday, September 23, 2016 2:35 PM To: 'powershell@lists.myitforum.com' Subject: [powershell] RE: Functions, Scoping, and Returns For now each .ps1 file is loaded in a PowerShell ISE session for testing. Once I was comfortable with logic and error handling I want to create a module. I haven’t gotten to that step/goal yet, but if that changes the answer, let’s work from the premise these will be loaded as a module. From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On Behalf Of Michael B. Smith Sent: Friday, September 23, 2016 11:17 AM To: powershell@lists.myitforum.com Subject: [powershell] RE: Functions, Scoping, and Returns ATTENTION: This email came from an external source. DO NOT open attachments or click on links from unknown senders or unexpected emails. How are you calling these functions? From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On Behalf Of Orlebeck, Geoffrey Sent: Friday, September 23, 2016 1:38 PM To: 'powershell@lists.myitforum.com' Subject: [powershell] Functions, Scoping, and Returns I have two functions, one for creating an AD account, another for creating a mailbox. The AD account function has a switch for mailbox creation which calls the create mailbox function. The issue I am running into is passing information from the create mailbox back to the parent user creation function. Our Helpdesk staff are not strong with PowerShell and I’m trying to provide text output so they can validate the script worked. The problem I’m encountering is getting both the outputting the ‘write-output’ steps in the mailbox function and returning a success/fail result. If I store the function in a variable, I can get it to return the success/failure, but it won’t output the text as the function runs. If I don’t perform a ‘$Result = create mailbox function’, the various outputs from the mailbox function are visible, but then I have no way of validating it actually worked (for screen output purposes of pass/fail.) I’m attaching both functions (scrubbed of any sensitive data), but I’m wondering if this is where scoping gets involved with respect to global/local/script? I’m reading up on the concept but I’m not sure that’s the appropriate path. Any help is appreciated. Thank you. Confidentiality Notice: This is a transmission from Community Hospital of the Monterey Peninsula. This message and any attached documents may be confidential and contain information protected by state and federal medical privacy statutes. They are intended only for the use of the addressee. If you are not the intended recipient, any disclosure, copying, or distribution of this information is strictly prohibited. If you received this transmission in error, please accept our apologies and notify the sender. Thank you.