Author Posts

June 19, 2015 at 3:28 am

Hi Guys

I am writing my first function in powershell but it does work when I run it from exchange management shell. No errors shown.

If I assign variables to $Inputfile & $output file in ISE and run the actual code it creates the file fine.

When i run it from a exchange management shell by running the below command it never creates the output file.

.\Get-ValidEmails.ps1 "C:\Brightsideemails.csv" "c:\brightsideValid.csv"
or
Get-ValidEmails.ps1 -Inputfile "C:\Brightsideemails.csv" -outputfile "c:\brightsideValid
.csv"

Please help, what am an doing wrong?

function Get-ValidEmails
{
 
    Param
    (
        # Input CSV File Path
     
         [string]
        $InputFile,

        # Output CSV File Path
        [string]
        $Outputfile
    )

$Input = import-csv $InputFile
foreach ($rec in $Input)
{
get-mailbox -identity $rec.Recipients -ErrorAction SilentlyContinue | select name, alias, severname |  export-csv $Outputfile -append
}
}

June 19, 2015 at 3:36 am

Karl,

You are saying this first: "but it does work when I run it from exchange management shell"

Then you are saying this: "When i run it from a exchange management shell by running the below command it never creates the output file."

I'm confused. 😛

I also see you made a typo in here: "select name, alias, severname"

Shouldn't it be: "select name, alias, servername"? You forgot to put the "r" in servername.

June 19, 2015 at 3:36 am

I don't see you running function in your Exchange Management Shell.
Try this:
– run Exchange Managemetn Shell
– go to directory where your Get-ValidEmails.ps1 is located
– run ". .\Get-ValidEmails.ps1"
– run "Get-ValidEmails -Inputfile "C:\Brightsideemails.csv" -outputfile "c:\brightsideValid.csv""

When you run Get-ValidEmails.ps1 you just run .ps1 file. This ps1 file declares function Get-ValidEmails but don't run it 🙂

June 19, 2015 at 3:41 am

You have written a function, but you are trying to execute the .ps1 file. You need to load the function into powershell using dot sourcing as seen below and then use the function. Looking at "about_Scripts" for more information will help you understand it better.

Just save your script as myscript.ps1 and do the following.


# dot sourcing. Note the preceding . (dot) followed by a space and then the .ps1 script. 
PS> . c:\myscript.ps1

# now the function is loaded into powershell and you should be able to use it.
PS> Get-ValidEmails -Inputfile "C:\Brightsideemails.csv" -outputfile "c:\brightsideValid.csv"

June 19, 2015 at 4:33 am

Thanks Guys

It was the dot sourcing bit that I was missing.

I was trying to run the command straight from the file rather than loading the function into powershell first.

Sorry if I didn't word the question quite right...