2016-01-25 53 views
1

我對Powershell非常陌生。我一直在努力這個項目。我一直試圖獲得並保存在我的電腦中的這個文件。我嘗試不同的方式,並要求對堆棧溢出如何自動保存記事本文件並關閉Powershell中的記事本

當我執行該代碼

$ie = New-Object -ComObject 'internetExplorer.Application' 
$ie.Visible= $true 
$ie.Navigate("https://idp.appery.io/idp/") 

while ($ie.Busy -eq $true){Start-Sleep -seconds 1;} 

$usernamefield = $ie.Document.getElementByID('j_username') 
$usernamefield.value= $username 

$passwordfield = $ie.Document.getElementByID('j_password') 
$passwordfield.value = $password 

$Link=$ie.Document.getElementByID("loginBtn") 
$Link.click() 

while ($IE.Busy -eq $true) 

{ 

Start-Sleep -Milliseconds 2000; 

} 

$ie.Navigate("https://appery.io/bttttn/rest/1/admin/databases") 

當我執行上面的代碼,記事本會自動彈出的「database.json」,就找哪裏這個JSON文件在我的C盤,但我找不到它。這似乎剛剛打開文件, 所以我想

  1. 保存「database.json」我的文件夾

  2. 關閉記事本窗口,

我怎麼能這樣做?

非常感謝你!

+0

更改文件關聯。 –

回答

1

不幸的是,你不能自動化記事本! (你也許能夠擊鍵發送給它,但它會變得非常混亂,容易出現破損)

一個更好的方法是使用調用-WebRequest的登錄,然後下載文件

$request = Invoke-WebRequest $loginpage -SessionVariable "session" 
$form = $request.Forms[0] 
$form = $request.form['username'] 
$form = $request.form['password'] 
$request = Invoke-WebRequest -Uri ('$downloadlink + $form.Action) ` 
       -WebSession $session -Method POST ` 
       -Body $form.Fields -OutFile $filepath 

如果頁面不返回一個表單對象,你可以做

$form = @{ "username" = $username 
      "password = $password} 

你需要看看在登錄頁面並提取用戶名/密碼的變量名。

我也沒有測試過它!所以它是從記憶中獲得的,但它會讓你走上更可靠的軌道。

+0

我做到了,但它不工作,http://stackoverflow.com/questions/34954584/powershell-invoke-webrequest-form-where-is-the-form – Kazu

+1

@Kazu我已經修改了答案一點 –