简体   繁体   English

使用参数调用powershell函数

[英]call powershell function with parameters

I am just getting started with Windows Powershell and I am try to create a function that parses a webpage. 我刚刚开始使用Windows Powershell,我尝试创建一个解析网页的函数。 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. 它存储在当前powershell目录中的powershellScript.ps1中。

The best error I can get when I call this function is 我调用此函数时可以得到的最佳错误是

function goto-website "Website" 功能goto-website“网站”

Missing function body in function declaration. 函数声明中缺少函数体。 At line:1 char:23 + function goto-website <<<< "website" + CategoryInfo : ParserError: (:) [], + FullyQualifiedErrorId : MissingFunctionBody 在行: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 试试这个修改过的代码版本

function GoTo-Website
{
    Param ($website)
    $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;
}

which you can call with 你可以打电话给

GoTo-Website "http://www.google.com"

or 要么

GoTo-Website -website "http://www.google.com/"

No need to put "function" before the call. 无需在通话前输入“功能”。

See today's post on the Scripting Guy Blog for a little more information on functions. 有关功能的更多信息,请参阅Scripting Guy博客上的今日帖子

After following advice on how to add parameters [and realizing I have to copy and paste my function into powershell] I was able to call my function. 在遵循如何添加参数[并意识到我必须将我的函数复制并粘贴到powershell]的建议后,我能够调用我的函数。 Here is the modified version of the function 这是该功能的修改版本

function GoTo-Website

{ Param ($website)

  $global:ie.navigate($website);

  $global:image = @($ie.Document.getElementById("comic_image"))[0].href;

  $global:title = @($ie.Document.getElementsByTagName("h1"))[3].innerHTML

  $global:date = @($ie.Document.getElementsByTagName("h3"))[0].innerHTML;

}

I also had to change my parameters to global to expose the results and access an internet explorer variable that is activated. 我还必须将我的参数更改为全局以公开结果并访问已激活的Internet Explorer变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM