Download via Powershell

Asked

Viewed 69 times

0

Hi, I’m trying to download Java via Invoke-Webrequest join with regex & match

My script is like this:

$url_download = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content | % { [regex]::Match($_, '(?:<a title="Download Java software for Windows Online" href=")(.*)(?:">)').Groups[1].Value }
Invoke-WebRequest -UseBasicParsing -OutFile java.exe $url_download
Start-Process .\java.exe '/s REBOOT=0 SPONSORS=0 AUTO_UPDATE=0' -wait
echo $?

but I am getting an invalid URL error

inserir a descrição da imagem aqui

some hint to solve this?

1 answer

1

This site does not initially load with the download links. What can be done is to emulate a browser and wait for the page to be fully loaded. After that, we take the link and proceed with download/installation.

See how it looked in my example below:

#Cria o objeto do Internet Explorer para simular a navegação para está página
$ie = New-Object -ComObject "InternetExplorer.Application"
#Navega até a página solicitada 
$ie.Navigate2("https://www.java.com/en/download/manual.jsp")

$anchor = $null
while($anchor -eq $null -or $anchor -eq "")
{
    #aguarda 1 segundo para que seja feito o carregamento da página
    start-sleep -m 1000
    #obtém o html do página
    $html = $ie.document.body.innerHTML 
    #aplica seu regex para identificar a anchar com o link para download 
    $anchor = [regex]::Match($html, '(?:<a title="Download Java software for Windows Online" href=")(.*)(?:">)').Groups[1].Value     
}

#a regex não retorna o link corretamente, por isso fiz o substring para pegar o link
$url_download = $anchor.Substring(0,$anchor.IndexOf(""""))
#Faz download utilizando o link que obtivemos
Invoke-WebRequest -UseBasicParsing -OutFile java.exe $url_download
#Inicia a instalaçãoa
Start-Process .\java.exe '/s REBOOT=0 SPONSORS=0 AUTO_UPDATE=0' -wait

Browser other questions tagged

You are not signed in. Login or sign up in order to post.