LoginSignup
0
1

More than 3 years have passed since last update.

Webサイトにログインするpowershellスクリプト

Posted at

これはなに

WebGUIログインを自動化したかったので、powershellで作りました。
ホスト名、ID、アクセス先を引数で渡し、パスワードはローカルのテキストファイルから読み込みします。

スクリプト


# パラメータ 
Param(
    [string]$hostname, #ホスト名
    [string]$userid, #ユーザーID
    [string]$ipaddr #ipアドレス 
    )
$url = "https://" + $ipaddr + ":82/" #ログインページのURL
$userpass = Get-Content -Path "Path" | Select-String -Pattern $hostname #パスワード抽出
$userpass = $passwd -replace $hostname, "" #パスワード抽出

# シェルオブジェクト
$shell = New-Object -ComObject Shell.Application
# IEオブジェクト
$ie = New-Object -ComObject InternetExplorer.Application
# ブラウザオープン
$ie.Visible = $true
# HWND
$hwnd = $ie.HWND
# URLオープン(キャッシュ無効)
$ie.Navigate($url, 4)

# ここで再度IEを取得 これやらないとページを読み込めない
while ($ie.Document -isnot [mshtml.HTMLDocumentClass]) {
  $ie = $shell.Windows() | ? { $_.HWND -eq $hwnd }
}

#メソッドの再定義 これやらないと、存在しないメソッドみたいなエラーが返ってくる
function OverrideMethod ([mshtml.HTMLDocumentClass]$Document)   {
  $doc = $Document | Add-Member -MemberType ScriptMethod -Name "getElementById" -Value {
     param($Id)
     [System.__ComObject].InvokeMember(
         "getElementById",
         [System.Reflection.BindingFlags]::InvokeMethod,
         $null,
         $this,
         $Id
     ) | ? { $_ -ne [System.DBNull]::Value }
  } -Force -PassThru
  $doc | Add-Member -MemberType ScriptMethod -Name "getElementsByClassName" -Value {
     param($ClassName)
     [System.__ComObject].InvokeMember(
         "getElementsByClassName",
         [System.Reflection.BindingFlags]::InvokeMethod,
         $null,
         $this,
         $ClassName
     ) | ? { $_ -ne [System.DBNull]::Value }
  } -Force
  $doc | Add-Member -MemberType ScriptMethod -Name "getElementsByTagName" -Value {
     param($TagName)
     [System.__ComObject].InvokeMember(
         "getElementsByTagName",
         [System.Reflection.BindingFlags]::InvokeMethod,
         $null,
         $this,
         $TagName
     ) | ? { $_ -ne [System.DBNull]::Value }
  } -Force
  return $doc
}

#オーバーライドメソッドを実行し、WEBページの中身を得る
$document = OverrideMethod($ie.Document)

#ページの中から、フォームに割当てられたFieldというクラスを持つ要素を抜き出す
#ログインしたいページによって異なる
#IDが割り当てられている場合は$document.getElementByIdを使った方がよい

#非常に重要なエラーハンドリング
#きちんと変数に情報が格納されたことを確認しないと、無限にエラーが返ってくる
while ($true) {
  $textbox_id = $document.getElementById("input__mailtel") #ログインidを入力するテキストボックスのid
  if ($textbox_id) { break }
  Start-Sleep -Milliseconds 100
}

while ($true) {
    $textbox_pwd = $document.getElementById("input__password") #パスワードを入力するテキストボックスのid
    if ($textbox_pwd) { break }
    Start-Sleep -Milliseconds 100
  }

while ($true) {
  @($textbox_id)[0].value = $userid
  @($textbox_pwd)[0].value = $userpass
  if (@($textbox_id)[0].value){
    if (@($textbox_pwd)[0].value){
      { break }
    }
  }
  Start-Sleep -Milliseconds 100
}

#ログインボタンの取得とクリック
while ($true) {
  $button = $document.getElementById("login__submit") #ログインボタンのid
  if ($button) { break }
  Start-Sleep -Milliseconds 100
}
$button.click()

参考

https://qiita.com/flasksrw/items/a1ff5fbbc3b660e01d96
https://yukituna.com/2417/

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1