2017-04-11 34 views
0

所以我想使自動化使用PowerShell的做這個簡單的操作:獲取信息從頁面中使用PowerShell

  • 到我的網站
  • 填寫「USERNAME」與「MYNAME」
  • 檢查「PERSONNEL_NBR 「複選框
  • 提交
  • 檢索PERSONNEL_NBR並將其存儲在一個變量

注意:任何解決辦法是罰款,我

我想用調用-的WebRequest,因爲它沒有推出合適的IE瀏覽器會話中工作。這將不勝感激!

此代碼:

$ie = $null 
$ie = new-object -com internetexplorer.application 
$ie.navigate('http://example.com/') 
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.name -eq "username"}; $link.value = "myname" 
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.value -eq "PERSONNEL_NBR"}; $link.click() 
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.type -eq "submit"}; $link.click() 
sleep 2 
$personnel_nbr = ((($ie.Document.body.outerHTML | findstr /L /C:">PERSONNEL_NBR<") -split "<SPAN class=tx>" | Select -Index 1).split("</SPAN>",2) | Select -Index 0) 
$ie.quit() 

作品,但只有當我打開PowerShell和從那裏發射的命令。如果我將它保存爲腳本並啓動它,它不起作用。

PS:降低IE安全性是不可行的,無論如何它不能解決問題。

PPS:上述版本是我使用的代碼的簡化版本,沒有超出主題的內容。完整的版本正在等待瀏覽器,但它仍然不起作用(可能是由於cookie問題?如果有人可以提出建議,我很樂意解決這個問題。我已經有關於cookie的信息,我已經重新編號我只是不知道如何在我的腳本中使用這些信息。)

PPPS:如果問題鏈接到交互模式,我不能在另一個powershell中執行所有這些命令模擬交互式會話?這將工作。任何人都知道嗎?


版本二


這是另一種嘗試。沒有錯誤,但它返回頁面的內容。我甚至不確定請求是否通過。

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession 
$cookie = New-Object System.Net.Cookie 
$cookie.Name = "mycookiename" 
$cookie.Value = "mycookievalue" 
$cookie.Path = "/" 
$cookie.Domain = "example.com" 
$session.Cookies.Add($cookie) 
$uri = "http://example.com" 
$body = @{username="myname";fields="PERSONNEL_NBR"} 
#THESE ARE UNSUCCESSFUL ATTEMPTS 
#$r = Invoke-WebRequest -Uri $uri -WebSession $session 
#$r.InputFields[34].innerHTML = "true" 
#$r.InputFields[34].innerText = "true" 
#$r.Forms[0].Fields["fields"] = "PERSONNEL_NBR" 
#$r.Forms[0].Fields["PERSONNEL_NBR"] = "true" 
#$r.InputFields[0].innerText="myname" 
#$r.Forms[0].Fields["username"] = "myname" 
#$r = Invoke-WebRequest -Uri $uri -WebSession $session -Method Post -Body $r 
$r = Invoke-WebRequest -Uri $uri -WebSession $session -Method Post -Body $body 
$r.RawContent 
+0

您可能需要爲您的站點創建一個完整的POST請求,以在沒有IE作爲應用程序的情況下執行此操作。如果需要的話,那個應該包含auth cookie,以及點擊「PERSONNEL_NBR」鏈接(我期望這個填充表單中的一些字段,最有可能是檢查名稱操作)時在客戶端收集的任何表單數據。順便提一下,cookie的存在可能會阻止腳本在交互式Powershell會話之外工作。 – Vesper

+0

很抱歉,我無法訪問該網站以幫助...如果我們有權訪問,我們可以做什麼就是記錄Fiddler跟蹤並查看此站點如何爲POST請求構建有效負載,然後將其轉換爲PowerShell。 – FoxDeploy

+0

@Vesper所以有什麼辦法可以使用我發佈的代碼從powershell請求cookie? –

回答

0

好了,所以..防止腳本的工作是不是既不是交互模式還是餅乾的問題。問題出在管理員權限。

所以這個代碼工作完美:

$ie = $null 
$ie = new-object -com internetexplorer.application 
$ie.navigate('http://example.com') 
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.name -eq "username"}; $link.value = "myname" 
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.value -eq "PERSONNEL_NBR"}; $link.click() 
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.type -eq "submit"}; $link.click() 
while ($ie.Busy -eq $true -and $timer -lt 10) { 
    sleep 1 
    $timer++ 
    echo "Waiting for example.com..." 
} 
if ($timer -gt 10) { 
    while ($confirmation -ne "y" -and $confirmation -ne "n") { 
     $confirmation = Read-Host "`nConnection timed out. Do you want to try to grab it manually? (y/n)" 
     if ($confirmation -eq 'y') { 
      echo "`nPlease go to this page:`nhttp://example.com and put `"$username`" in the `"USERNAME`" field, then check the `"PERSONNEL_NBR`" checkbox and then submit." 
      Read-Host "`nPERSONNEL_NBR" 
     } 
     elseif ($confirmation -eq 'y') { 
      echo "`nRemember to add the PERSONNEL_NBR later!" 
     } 
     else { 
      echo "`nInvalid option." 
     } 
    } 
    $confirmation=$null 
} 
$personnel_nbr = ((($ie.Document.body.outerHTML | findstr /L /C:">PERSONNEL_NBR<") -split "<SPAN class=tx>" | Select -Index 1).split("</SPAN>",2) | Select -Index 0) 
echo "The PERSONNEL_NBR is $personnel_nbr" 
$ie.quit() 

你只需要運行它管理員。感謝所有人,我感謝你的幫助和時間。

0

有與使用IE通訊對象一些煩人的事情,但他們大多可超過來,比如使用.visible = $false將隱藏的IE窗口。 檢查.Busy以查看頁面何時實際加載。你可能會發現你的腳本沒有作爲一個獨立腳本正在執行的原因是它運行得很快。

$ie = New-Object -com internetexplorer.application; 
$ie.visible = $false; 
$ie.navigate('http://mypioneer.phibred.com/ews/'); 

while ($ie.Busy -eq $true) 
{ 
    Start-Sleep -Seconds 2; 
} 
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.name -eq "username"}; $link.value = "medada" 
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.value -eq "PERSONNEL_NBR"}; $link.click() 
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.type -eq "submit"}; $link.click() 

while ($ie.Busy -eq $true) 
{ 
    Start-Sleep -Seconds 2; 
} 

$personnel_nbr = ((($ie.Document.body.outerHTML | findstr /L /C:">PERSONNEL_NBR<") -split "<SPAN class=tx>" | Select -Index 1).split("</SPAN>",2) | Select -Index 0) 
Write-Host $personnel_nbr 

$ie.Quit() 
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie) 
Remove-Variable ie 

另一種方法是嘗試捕捉提交表單POST數據和上市here

+0

嗨,尼克,謝謝你的回答。不,我的腳本實際上是在等待瀏覽器。它給我的錯誤是: '被調用的對象已經與其客戶端斷開連接。' 可能是由於缺少一個cookie –

+0

Hi @Lafaguette這是一個奇怪的問題,我一直在使用IE com對象很長一段時間,這個錯誤聽起來很熟悉。我不認爲它與cookies相關聯,IE com對象通常可以正常使用cookie。我認爲當com對象失去與IE引擎的同步時我看到了這個錯誤。也許有一些奇怪的JavaScript重定向或頁面上發生的事情。 – Nick

0

的工具之一重新創建POST傳輸。如果您需要在POST請求使用cookie,您NEET創建Cookie對象,將其添加到WebRequestSession對象,並把它作爲調用-的WebRequest的WebSession參數:

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession 

$cookie = New-Object System.Net.Cookie 

$cookie.Name = "cookieName" 
$cookie.Value = "cookieValue" 
$cookie.Domain = "cookieDomain" 

$session.Cookies.Add($cookie) 

Invoke-WebRequest -Uri $uri -WebSession $session -Method Post -Body $body 

$body變量是POST請求的內容,所以你需要使用我你收集關於創建它的發佈請求的信息。它會是這個樣子:

$body = @{ 
username = "medada" 
input name = value 
... 
} 
+0

這將是最好的情況下,如果只有我可以管理它的工作!我在原來的帖子中添加了我在這種情況下使用的代碼。沒有錯誤,但它沒有做任何事情,我不明白我在丟失什麼 –

+0

@Lafaguette cookie中必須有一個域值(可能是phibred.com)。在Chrome中,您可以在此處查找cookie:chrome:// settings/cookies將所需的原始cookie中的所有數據複製到新的cookie對象中。關於'fields =「PERSONNEL_NBR」',我不認爲這是你需要的正確名稱和價值。您可以分享您使用Chrome開發工具記錄的POST信息嗎? –

+0

是的,我根據您的要求更新了帖子 –