2012-07-09 32 views
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 

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

+0

这真的需要澄清 - 我不知道你是什么试图去做。 – 2012-07-09 21:53:04

+0

@TrevorSullivan我有数据结构'$ sites'来存储所有的网站相关信息。我不想保存某些控件的输入元素ID/Name ...('inputUserID','inputPassword'等),我想存储代码以获取这些控件,因为每个网站都可能有不同的方法来获取这些控件。 – ca9163d9 2012-07-09 22:24:37

回答

0

摆脱在PowerShell中脚本块一个封闭的方法是做到这一点:

{ #code here }.GetNewClosure() 

很难从代码告诉上面这将帮助任何,但它就是你想要的。

+0

不知道为什么downvote。原始问题询问如何从scriptblock获得封闭。 – 2012-07-11 03:47:35

0

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

$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) 
    } 
+0

什么是$ ie.someFunction? – ca9163d9 2012-07-12 04:50:04

+0

这是你用来访问页面DOM的任何函数。 – 2012-07-12 05:35:16