You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell触发网页表单Download file按钮获取autounattend.xml

问题

我使用一个Windows无人值守安装配置页面,完成所有配置后生成了包含全部参数的URL。尝试用以下PowerShell代码触发页面的「Download file」按钮来获取autounattend.xml,但执行失败。求可行的PowerShell方法获取对应的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: $_"
}
解决方案

原代码失败的原因是页面的「Download file」按钮没有直接的下载链接(href是JavaScript触发的前端下载逻辑),无法通过解析HTML获取有效下载地址。可以直接提取页面中已生成的XML内容,步骤如下:

  1. 访问配置URL获取页面内容
  2. 匹配页面中存储XML的textarea元素内容
  3. 解码HTML转义字符(如&lt;转成<)
  4. 将解码后的内容保存为XML文件

可行的PowerShell代码

# 配置好的URL
$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 {
    # 发起请求获取页面内容,使用UseBasicParsing提升效率
    $response = Invoke-WebRequest -Uri $url -UseBasicParsing

    # 匹配页面中id为xml的textarea内容
    $match = $response.Content | Select-String -Pattern '<textarea id="xml"[^>]*>([\s\S]*?)</textarea>'

    if ($match.Success) {
        # 提取匹配到的XML内容
        $xmlContent = $match.Matches.Groups[1].Value
        
        # 解码HTML实体字符
        [void][System.Reflection.Assembly]::LoadWithPartialName("System.Web")
        $decodedXml = [System.Web.HttpUtility]::HtmlDecode($xmlContent)

        # 保存为XML文件,编码设为UTF-8
        $decodedXml | Out-File -FilePath "autounattend.xml" -Encoding utf8
        Write-Host "autounattend.xml 已成功保存"
    }
    else {
        Write-Host "未找到XML内容"
    }
}
catch {
    Write-Host "执行出错: $_"
}

说明

  • 使用UseBasicParsing避免加载IE引擎,提升执行稳定性和速度
  • 通过正则匹配提取页面中已生成的XML内容,无需依赖前端按钮触发
  • 必须解码HTML实体,否则XML中的尖括号等特殊字符会保持转义状态,导致文件无效

内容的提问来源于stack exchange,提问作者YorSubs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 01:46:04