Automation

 View Only
  • 1.  Calling funcitons

    Posted Oct 02, 2008 09:18 AM

    Hi All,

    I'm working on a large script to do a selection of different tasks, however - I like clean code so really want to have everything nicedly functioned off. This is easy enough in one script - but is likely to make the script huge. Is there any way to Move out my functions into a seperate script and "Include" them as a Functions.ps1 file into my script??

    Thanks in anticipation.

    TG



  • 2.  RE: Calling funcitons

    Posted Oct 02, 2008 10:45 AM

    You could place your functions in one or more seperate PS1 files and then dot source these files from your main PS1 file.

    For example you could have:

    • functions1.ps1

    • functions2.ps2

    • main.ps1

    all in one directory and then in main.ps1 you could do

    <pre>
    . ./functions1.ps1
    . ./functions2.ps1
    </pre>
    

    That way you can call all the functions in the main.ps1 file.



  • 3.  RE: Calling funcitons

    Posted Oct 02, 2008 08:25 PM

    Saddly - not good this time guys.

    Ive created a new folder. and two files within the folder : Main.ps1 and func-emal.ps1.

    I then call main.ps1 from the PS shell with it set in the folder containing the file.

    #Main

    &lt;pre&gt;

    . ./func-email.ps1

    &lt;/pre&gt;

    write-host "Loaded Main"

    $oReturned = sendemail "pstest@wibble.co.uk" "tgent@wibble.co.uk" "SubjectLine" "Text Line"

    write-host $oReturned.

    #Func-email contains just :

    function sendemail {

    param ($sFrom, $sTo, $sSubj, $sTxt)

    $sEmailSvr = "alfred.server"

    $SmtpClient = New-Object system.net.mail.smtpClient

    $MailMessage = New-Object system.net.mail.mailmessage

    $SmtpClient.host = $sEmailSvr

    $MailMessage.from = $sFrom

    $MailMessage.To.add($sTo)

    $MailMessage.IsBodyHtml = 1

    $MailMessage.Subject = $sSubj

    $MailMessage.body = $sTxt

    #$MailMessage.Attachments.Add("c:\temp\report.txt")

    $oResult = $SmtpClient.Send($MailMessage)

    write-host $oResult

    return $oResult

    }

    When I run the Main from the power shell - it fails with the following error :

    The term '&lt;'&lt;' is not recognized as a cmdlet, function, operable program, or scri

    pt file. Verify the term and try again.

    At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\FuncInc\m

    ain.ps1:4 char:2

    If I replace Main1 with the following code :

    #Main

    Get-ChildItem scripts:\func-*.ps1 | % { . $_

    write-host "Loading library file:`t$($_.name)"

    }

    write-host "Loaded Main"

    $oReturned = sendemail "pstest@wibble.co.uk" "tgent@wibble.co.uk" "SubjectLine" "Text Line"

    write-host $oReturned.

    I get the following error :

    Get-ChildItem : Cannot find drive. A drive with name 'scripts' does not exist.

    At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\FuncInc\m

    ain.ps1:3 char:14

    + Get-ChildItem &lt;&lt;&lt;&lt; scripts:\func-*.ps1 | % { . $_

    Loaded Main

    The term 'sendemail' is not recognized as a cmdlet, function, operable program,

    or script file. Verify the term and try again.

    At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\FuncInc\m

    ain.ps1:8 char:23

    + $oReturned = sendemail &lt;&lt;&lt;&lt; "pstest@snsltd.co.uk" "tgent@snsltd.co.uk" "Subj

    ectLine" "Text Line"

    In short - neither attempt works. Am I missing something important. Please bare in mind - I'm a newb!

    Many thanks for your assistance.

    TG



  • 4.  RE: Calling funcitons
    Best Answer

    Posted Oct 02, 2008 12:24 PM

    You can also easily do this for a range of files e.g.

    Get-ChildItem scripts:\lib-*.ps1 | % {

    . $_

    write-host "Loading library file:`t$($_.name)"

    }






    Author of the upcoming book: Managing VMware Infrastructure with PowerShell

    Co-Host, PowerScripting Podcast (http://powerscripting.net)



  • 5.  RE: Calling funcitons

    Posted Oct 02, 2008 12:29 PM

    Man,

    You pair are amazing! - it's like the "Hal and Luc Show!" tm

    :smileyhappy:

    Both answers are spot on - going with yours Hal as it's more expansive.

    Thanks again.

    TG