VMware Cloud Community
tonygent
Enthusiast
Enthusiast
Jump to solution

Calling funcitons

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

Reply
0 Kudos
1 Solution

Accepted Solutions
halr9000
Commander
Commander
Jump to solution

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)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

halr9000
Commander
Commander
Jump to solution

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)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
tonygent
Enthusiast
Enthusiast
Jump to solution

Man,

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

Smiley Happy

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

Thanks again.

TG

Reply
0 Kudos
tonygent
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos