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: [email protected] [mailto:[email protected]] On
Behalf Of Orlebeck, Geoffrey
Sent: Friday, September 23, 2016 2:35 PM
To: '[email protected]'
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: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Michael B. Smith
Sent: Friday, September 23, 2016 11:17 AM
To: [email protected]<mailto:[email protected]>
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: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Orlebeck, Geoffrey
Sent: Friday, September 23, 2016 1:38 PM
To: '[email protected]'
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.