The message relates to my previous calling functions post - I'd set that as solved without testing the results (silly - I know).
The threads query was "can I "Include" functions from seperate scripts to keep my script clean"
Two possible answers were suggested and both looked like they should work. Saddly not... Here's my working.
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
. ./func-email.ps1
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
Hi Tony, no problem.
It looks as if the forum SW is (again) the cause of the problem.
The
and
lines shouldn't be in the script.
Your script should start like this
#Main . ./func-email.ps1 write-host "Loaded Main"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Tony, no problem.
It looks as if the forum SW is (again) the cause of the problem.
The
and
lines shouldn't be in the script.
Your script should start like this
#Main . ./func-email.ps1 write-host "Loaded Main"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Further investigation...
Well it seems that if I change the syntax to
#Main
. .\func-email.ps1
write-host "Loaded Main"
$oReturned = sendemail "pstest@snsltd.co.uk" "tgent@snsltd.co.uk" "SubjectLine" "Text Line"
write-host $oReturned.
This works fine...
I'm begining to wonder if the elements were actually the web site messing the code up??
Still - a way of loading all the scripts with a specific prefix (func-*) would also be nice??
Thanks again Luc...
And I'm on a roll this AM...
The following code also works - it seems Hal had included some Script element that was not required for me - not sure if this is down to my environment??
Get-ChildItem .\func-*.ps1 | % { . $_
write-host "Loading library file:`t$($_.name)"
}
Here's is my attempt of simplifying the code above, please feel free to comment on my assumptions as to what's going on.
$IncFile = Get-ChildItem .\func-*.ps1
Foreach ($ifile in $incfile)
{
. $iFile
write-host "Loading library file: $iFile"
}
This is an explanation of the lines of code starting at the $incfile = Get-Children line.
read all the files with a func prefix into an object.
skip through the files one at a time and execute the code in the { } brackets.
use the '. ' (space is intended) to tell the system to read in the file listed in the iFile variable.
write an entry to the std-out to show we have read in the library file.
return the loop
Hope this makes sense :smileygrin:
Tony,
Hal apparently has a provider, called Scripts, that points to a directory where he stores his scripts.
It works +/- like a drive-letter.
Since you don't have that on your machine you get the error.
Your code looks good and so does the explanation.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hal apparently has a provider, called Scripts, that points to a directory where he stores his scripts.
That is correct. Sorry, I missed the question from Tony asking about it. Check out the help for new-psdrive. Having a scripts: drive is very handy!
Author of the upcoming book: Managing VMware Infrastructure with PowerShell
Co-Host, PowerScripting Podcast (http://powerscripting.net)