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:
[cid:image001.png@01D215B5.F36FCCE0]

But when I run the Caller.ps1 I got this:
[cid:image002.png@01D215B5.F36FCCE0]

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:
[cid:image003.png@01D215B5.F36FCCE0]

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> 
[mailto:listsad...@lists.myitforum.com] On Behalf Of Michael B. Smith
Sent: Friday, September 23, 2016 12:05 PM
To: powershell@lists.myitforum.com<mailto: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> 
[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> 
[mailto:listsad...@lists.myitforum.com] On Behalf Of Michael B. Smith
Sent: Friday, September 23, 2016 11:17 AM
To: powershell@lists.myitforum.com<mailto: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> 
[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.








Reply via email to