脚本块作为字典的值?

时间:2012-07-09 21:10:59

标签: powershell closures powershell-v2.0

是否可以实现以下powershell脚本?

目的是稍后调用脚本块来获取html控件。

$sites = @{
    site1= @{ 
        url = "......."; 
        inputUserID = { $doc.getElementsByID("username"]) }; #Some code to get the html input control
        inputPassword = { $doc.getElementsByName("passowrd"]) }; #Some code to get the html input control
    };
    .....
}

$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.navigate($sites[$site]["url"])
$ie.visible = $true
while ($ie.busy) { start-sleep -milliseconds 1000; }

... #Get the input controls and fill the values

问题已更新。它够清楚了吗?它应该不难理解。

2 个答案:

答案 0 :(得分:0)

从PowerShell中的scriptblock获取闭包的方法是:

{  #code here  }.GetNewClosure()

很难从上面的代码中看出这是否会对你有所帮助,但这就是你所要求的。

答案 1 :(得分:0)

您需要遍历每个网站的信息。我不确定你是如何使用IE COM对象的DOM选择器的,所以我猜测了部分代码:

$sites = @{
    site1= @{ 
        url = "......."; 
        inputUserID = ( 'doc.getElementsByID("username")' ); #Some code to get the html input control
        inputPassword = ( 'doc.getElementsByName("passowrd")' ); #Some code to get the html input control
    };
    .....
}

$sites.Keys | 
    ForEach-Object {
        $siteInfo = $sites[$_]
        $ie = New-Object -ComObject "InternetExplorer.Application"
        $ie.navigate($siteInfo.url)
        $ie.visible = $true
        while ($ie.busy) { start-sleep -milliseconds 1000; }
        ... #Get the input controls and fill the values
        $usernameElement = $ie.someFunction( $siteInfo.inputUserID )
        $passwordElement = $ie.someFunction( $siteInfo.inputPassword )
    }
相关问题