I am just getting started with Windows Powershell and I am try to create a function that parses a webpage. I have tested the individual calls in the function and they seem to be working correctly. Here is the function I have created.
function GoTo-Website ([string]website = "google.com")
{
$ie.navigate($website);
$image = @($ie.Document.getElementByName("main_image"))[0].href;
$title = @($ie.Document.getElementByTagName("h1"))[3].innerHTML;
$date = @($ie.Document.getElementByTagName("h3"))[0].innerHTML;
}
This is stored in powershellScript.ps1 in the current powershell directory.
The best error I can get when I call this function is
function goto-website “Website”
Missing function body in function declaration. At line:1 char:23 + function goto-website <<<< “website” + CategoryInfo : ParserError: (:) [], + FullyQualifiedErrorId : MissingFunctionBody
Is there anyway to modify my code to get pass this parser error?
I also was wondering if their is a way to call a function without putting “function” before the call?
Try this modified version of your code
which you can call with
or
No need to put “function” before the call.
See today’s post on the Scripting Guy Blog for a little more information on functions.