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
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)
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
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)
Man,
You pair are amazing! - it's like the "Hal and Luc Show!" tm
Both answers are spot on - going with yours Hal as it's more expansive.
Thanks again.
TG
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
<pre>
. ./func-email.ps1
</pre>
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 '<'<' 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 <<<< 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 <<<< "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