# encoding: ascii
# api: powershell
# title: Open Webmin
# description: Allows you to open a Webmin interface in IE from the command line, automatically skipping an SSL error and logging in with the provided username and password.  I use this script as an external tool in mRemoteNG with the following powershell arguments line: “-NoProfile -ExecutionPolicy RemoteSigned -WindowStyle Hidden -File “C:\Users\kim.shepherd\Scripts\Webmin-Login.ps1” -Username Username -Password Password -URL https://%Hostname%:10000”
# version: 0.1
# author: Kim Shepherd
# license: CC0
# x-poshcode-id: 5840
# x-archived: 2015-05-02T13:23:58
# x-published: 2015-05-01T15:35:00
#
#

Param(  
    [string]$Username,
    [string]$Password,
    [String]$URL
)

# Create a new IE instance
$ie = New-Object -com internetexplorer.application 
$ie.visible = $true

$ie.navigate($URL)

#wait for the page to load
while ($ie.ReadyState -ne 4)
{
    Start-Sleep -Seconds 1;
}

# If there is an SSL error, click past it.
if ($ie.document.url.contains("SSLError"))
{
    ($ie.Document.getElementsByTagName("a") | where { $_.innerText.toLower().Contains("continue to this website") } | select -first 1).click()

    Start-Sleep -Seconds 1;

    while ($ie.ReadyState -ne 4)
    {
        Start-Sleep -Seconds 1
    }
}

# Enter the username and password and click the 'login' button
$ie.Document.getElementById("user").value = $Username
$ie.Document.getElementByID("pass").value = $Password
($ie.Document.getElementsByTagName("input") | where {$_.Value -eq "Login"}).click()
