First function, Where am I going wrong

Tagged: ,

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Karl Forster Karl Forster 1 year, 9 months ago.

  • Author
    Posts
  • #26642
    Profile photo of Karl Forster
    Karl Forster
    Participant

    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
    }
    }
    
  • #26643
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    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.

  • #26644
    Profile photo of Ondrej Zilinec
    Ondrej Zilinec
    Participant

    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 🙂

  • #26645
    Profile photo of GJ
    GJ
    Participant

    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"
    
    
  • #26649
    Profile photo of Karl Forster
    Karl Forster
    Participant

    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...

You must be logged in to reply to this topic.