Search code examples
powershellweb-scrapingbutton

PowerShell, how to press a button on a web form


The page https://schneegans.de/windows/unattend-generator is a form that lets you define what will be in the autounatted.xml to use with automated installations of Windows, and results in a URL that contains all of these options. Now that I have exactly what I want from the options in the form, I want to then press the Download file button that is on the form so that I can get the autounattend.xml onto my system. I've been trying the below without success. Maybe I am using the wrong approach to collect this XML file?

How can I use this URL and trigger the Download file button on this page (or any other way in PowerShell that I can collect the relevant XML data for the autounattend.xml)?

# URL of the page
$url = "https://schneegans.de/windows/unattend-generator/?LanguageMode=Unattended&UILanguage=en-US&UserLocale=en-GB&KeyboardLayout=0809%3A00000809&GeoLocation=242&ProcessorArchitecture=amd64&ComputerNameMode=Custom&ComputerName=Win10&TimeZoneMode=Implicit&PartitionMode=Unattended&PartitionLayout=GPT&EspSize=300&RecoveryMode=Partition&RecoverySize=600&WindowsEditionMode=Unattended&WindowsEdition=pro&UserAccountMode=Unattended&AccountName0=Boss&AccountPassword0=boss&AccountGroup0=Administrators&AccountName1=User&AccountPassword1=user&AccountGroup1=Users&AccountName2=&AccountName3=&AccountName4=&AutoLogonMode=Own&PasswordExpirationMode=Default&LockoutMode=Default&DisableSystemRestore=true&EnableLongPaths=true&EnableRemoteDesktop=true&HardenSystemDriveAcl=true&AllowPowerShellScripts=true&DisableLastAccess=true&NoAutoRebootWithLoggedOnUsers=true&TurnOffSystemSounds=true&DisableAppSuggestions=true&ProcessAudit=true&ProcessAuditCommandLine=true&WifiMode=Skip&ExpressSettings=DisableAll&SystemScript0=&SystemScriptType0=Cmd&SystemScript1=&SystemScriptType1=Ps1&SystemScript2=&SystemScriptType2=Reg&SystemScript3=&SystemScriptType3=Vbs&DefaultUserScript0=&DefaultUserScriptType0=Reg&FirstLogonScript0=&FirstLogonScriptType0=Cmd&FirstLogonScript1=&FirstLogonScriptType1=Ps1&FirstLogonScript2=&FirstLogonScriptType2=Reg&FirstLogonScript3=&FirstLogonScriptType3=Vbs&UserOnceScript0=&UserOnceScriptType0=Cmd&UserOnceScript1=&UserOnceScriptType1=Ps1&UserOnceScript2=&UserOnceScriptType2=Reg&UserOnceScript3=&UserOnceScriptType3=Vbs&WdacMode=Skip"

try {
    # Send request to the page
    $response = Invoke-WebRequest -Uri $url

    # Parse the HTML response
    $html = $response.ParsedHtml

    # Find the download button element
    $downloadButton = $html.querySelector('.btn.btn-primary')

    if ($downloadButton -eq $null) {
        Write-Host "Download button not found."
    }

    # Extract the download link
    $downloadLink = $downloadButton.getAttribute('href')

    # Download the file
    Invoke-WebRequest -Uri $downloadLink -OutFile "autounattend.xml"
    Write-Host "File downloaded successfully."
}
catch {
    Write-Host "Error occurred: $_"
}

Solution

  • No need for clicking a button – just use the view or download actions:

    $url = 'https://schneegans.de/windows/unattend-generator/view/?LanguageMode=...';
    Invoke-WebRequest -Uri $url -OutFile "$env:TEMP\autounattend.xml";
    

    Source: I am the author of that service.