Powershell 选择 HTML 文本

2024-01-07

我有以下 powershell 脚本来打开多台计算机上的多个 HTML 文件以提取许可信息。寻找一些帮助提取内部文本元素<td> where id=2.

寻找提取:

产品、序列号、产品密钥、请求代码

ForEach ($system in (Get-Content C:\temp\computers.txt)) {
  $folder = "\\$system\c`$\ProgramData\Autodesk\AdLM\"
  Get-ChildItem $folder *.html  |
  ForEach-Object {
    # Get current filename without .html
    $c = $_.BaseName
    # Create new Internet Explorer Object
    $ie=New-Object -ComObject InternetExplorer.Application
    # $_ (dollar underscore) 'THIS' token. Typically refers to the item inside a foreach loop. Open current HTML file.
    $ie.Navigate("$_")
    while ($ie.busy -eq $true) {
      Start-Sleep -Milliseconds 500
    }

    $doc=$ie.Document
    $tables=$doc.getElementsByTagName('table')
     foreach ($table in $tables) {
        if ($div.innertext -match "Product: ") 
        if ($div.innertext -match "Serial number: ")
        if ($div.innertext -match "Product Key: ")
        if ($div.innertext -match "Request code: ")
    }
    $elements.innerText | ForEach-Object { Add-Content -path c:\temp\results.csv "$c,$system,$para1" }
  }
}

HTML 文件示例

<table width="100%" cellspacing=0 cellpadding=0>
    <tr><td align=left id=1 width=15%>Product:</td><td align=left id=2 width=35%><!--PREVINFO_START-->Autodesk Building Design Suite Premium 2016<!--PREVINFO_END--></td><td align=left id=1>&nbsp;</td><td align=left id=2>&nbsp;</td></tr>
    <tr><td align=left id=1 width=15%>Serial number:</td><td align=left id=2><!--PREVINFO_START-->999-9999999<!--PREVINFO_END--></td></tr>
</table>
<table width="100%" cellspacing=0 cellpadding=0>
    <tr><td align=left id=1 width=15%>Product key:</td><td align=left id=2>424242</td><td align=left id=1>&nbsp;</td><td align=left id=2>&nbsp;</td></tr>
</table>
<table width="100%" cellspacing=0 cellpadding=0>
    <tr><td align=left id=1 width=15%>Request code:</td><td align=left id=3 style='word-break:break-all'>7777 7777 7777 7777</td></tr>
    <tr><td align=left id=1>&nbsp;</td><td align=left id=3 style='word-break:break-all'>7777 7777 7777 7777</td></tr>
</table>

我的解决方案,

# Script to extract Autocad Licensing informatoin from HTML files
# 1. Add the computers you want to access into computers.txt 
# 2. Download the Script and execute.
# 3. Results will be saved to the $file variable (default is c:\tools\results.csv)

#File Paths
$file="C:\tools\results.csv";
$computerfile="C:\tools\computers.txt";
$computerfoldername="\c`$\ProgramData\Autodesk\AdLM\";

# Create empty computers array
$computers = @() 


ForEach ($system in (Get-Content $computerfile)) {
# Append computers with \\computer\foldername
$computers += "\\" + $system + $computerfoldername
}
 write-host "=========================================================" -foreground "red"
 write-host "MULTI-HTML FILE PARSING AND APPENDING TO CSV SCRIPT "
 write-host "Script Executing at "  (Get-Date).ToString()
 #folders in which html files are kept



write-host "Folders To Parse"
for ($i = 0; $i -lt $computers.Count ; $i++) {
  write-host $computers[$i]

}

write-host "Initializing Script";

# Create new Internet Explorer Object
$ie = new-object -com "InternetExplorer.Application"

# Iterate through each \\computer\folder in the list
for ($i = 0; $i -lt $computers.Count ; $i++) {

write-host ""
write-host "=========================================="
write-host ""
write-host "COMPUTER/FOLDER" + $computers[$i] -foreground "yellow"
write-host ""

# Get HTML files within current \\computer\folder
Get-ChildItem $computers[$i] *.html  |
ForEach-Object {
# For each HTML file within current \\computer\folder
 $innerfile = $computers[$i] + $_.BaseName + $_.Extension
 write-host "=========================================="


write-host "Fetching" + $innerfile
# The easiest way to accomodate for slowness of IE
Start-Sleep -Seconds 1
# open current HTML file
$ie.Navigate($innerfile)
# The easiest way to accomodate for slowness of IE
Start-Sleep -Seconds 1
$doc = $ie.Document

write-host "IE Initiated for file " + $innerfile ;
# Get all <tr> elements in teh HTML file.
$trs=$doc.getElementsByTagName('tr')
$requestcodeflag=0;

foreach ($table in $trs) {

        $machine=$innerfile;
        # Product Key    
        Try {
            if ($table.innertext.Substring(0,8) -eq "Product:") {
                write-host "Parsed Product ID " + $table.innertext.Substring(8)
                $product=$table.innertext.Substring(8);
            }
        }
        Catch{}


        # Serial number
        Try {
             if ($table.innertext.Substring(0,14) -eq "Serial number:") {
                write-host "Parsed Serial Number  " + $table.innertext.Substring(14)
                $serialno=$table.innertext.Substring(14);
            }
        }
        Catch{}



        # Product Key
        Try {
            if ($table.innertext.Substring(0,12) -eq "Product key:") {
                write-host "Parsed Product Key  " + $table.innertext.Substring(12)
                $productkey=$table.innertext.Substring(12);
            }
        }
        Catch{}


        # Request code 2
        if($requestcodeflag -eq "1") {
        Try {
            $requestcodeflag=2;
            $requestcode= $requestcode + " | " + $table.innertext;
            write-host "Parsed 2nd Request Code  " +  $table.innertext
            }
        Catch{}
        }


        # Request code 1st
        Try{
            if ($table.innertext.Substring(0,13) -eq "Request code:") {
                write-host "Parsed Request Code  " +  $table.innertext.Substring(13)
                $requestcode=$table.innertext.Substring(13);
                $requestcodeflag=1;
            }
        }
        Catch{}


}



write-host "Writing to CSV file " + $file
$NewLine = "{0},{1},{2},{3},{4}" -f $machine,$product,$serialno,$productkey,$requestcode
$NewLine | add-content -path $file
}
}
$ie.Quit()
write-host "=========================================="
write-host "Data is  appended to csv file" + $file
write-host "Executed Thank You "
write-host "=========================================="
write-host ""
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Powershell 选择 HTML 文本 的相关文章

  • 为管道重用自定义对象时出现意外结果

    A while ago I changed my Join Object https stackoverflow com a 45483110 1701026 cmdlet which appeared to cause a bug whi
  • 全局变量用例

    我有几个脚本和模块 它们使用全局变量来完成很多事情 我的日志记录可以采用以下三种形式之一 简洁 详细和验证 没有实际操作的详细日志记录 仅验证提供的数据 我还有许多函数 它们根据运行的上下文 用户或机器 而做出不同的响应 并且正在执行的操作
  • SQL Server:删除除系统数据库之外的所有数据库

    在 PowerShell 中 我使用以下代码删除所有非系统 SQL Server 数据库 invoke sqlcmd ServerInstance sqlInstanceName U sqlUser P sqlPass Query EXEC
  • 如何使用PowerShell脚本远程启动/停止IIS 6.0/7.0?

    我有两台服务器服务器 A 和服务器 B 我想使用 Powershell 脚本从服务器 B 远程停止服务器 A 最简单的方法之一就是使用命令行执行PsExec http www microsoft com technet sysinterna
  • PowerShell Trim 字符串包含“< char >$”的错误?

    如果我使用Trim 包含字符串的方法 char repeated char 例如 BL LA 或 LA AB Trim 删除之后的重复字符 以及 例如 a BL LA b a Trim BL returns A not LA but a B
  • PSExec 中的会话 ID

    Psexec 无法在远程会话上为我显示记事本 GUI 因此 我尝试获取会话 ID 如下所示 c Users Amitra Downloads PSTools gt PsExec u administrator p force 135 20
  • 具有相关动态参数的 PowerShell 函数

    我试图定义一个函数 其中动态参数相互依赖 例如 我可能有 1 个参数和两个动态参数 rootPath 驱动器盘符 rootPathItem 第一个文件夹 在驱动器盘符内 rootPathChildItem 第二个文件夹 第一个之内 问题是
  • 使用 powershell 版本 2 查询 AD

    我们有由 Windows 7 和 Windows 10 组成的混合桌面操作系统 我有一个登录脚本 该脚本从每次用户登录时运行的 powershell 脚本收集各种信息 Windows 7 powershell 仅是版本 2 这意味着我无法使
  • 使用 Powershell SQL 将数据提取到 Excel

    我想使用 powershell 将数据从 SQL Server 提取到新的 excel 文件 对于小型数据集 我的代码可以工作 但某些表的行数超过 100 000 行 这将需要很长时间 我不在 SQl 服务器中使用该实用程序的原因是因为我想
  • 如何在 Powershell 中自动对提示回答“是”?

    如何在 PowerShell 会话中输入 是 作为交互式问题的答案 我知道 在 Bash 中 Yes是在提示上回答 是 的工具 在我的情况下 我无法抑制提示 我正在运行的脚本停止于 如果您想继续 请回复 是 powershell 如何运行脚
  • Get-AzureStorageBlob 抛出找不到您的 azure 存储凭据

    我刚刚开始使用 Azure 在使用 PowerShell cmdlet 处理我的存储帐户时遇到问题 我已经创建了一个存储帐户并在该存储帐户中创建了一个容器 接下来 我安装了 Azure Powershell SDK 和命令让等 并导入了pu
  • Powershell v5.1 Invoke-RestMethod 并绕过代理

    我目前使用的是 Powershell V5 1 并且希望在 Invoke RestMethod 命令上绕过 Internet Explorer 代理 在 Powershell V6 中 有 NoProxy 选项 指示 cmdlet 将不使用
  • 无法索引空数组

    我正在使用一个模板 该模板根据服务器备份是否成功的条件设置单元格颜色 我有下面的代码 它不断抛出错误 无法索引到空数组 Cannot index into a null array At C Users admin Desktop new
  • 在Windows中根据修改日期删除数百万个文件的最有效方法

    目标 使用脚本运行 500 万至 1000 万个 XML 文件并评估其日期 如果超过 90 天则删除该文件 该脚本将每天运行 问题 使用 powershell Get ChildItem recurse 会导致脚本锁定并无法删除任何文件 我
  • 具有多个范围的 Powershell 整数参数验证

    我知道您可以声明一个仅接受特定范围内的值的整数参数 Parameter ValidateRange 1024 66535 Port 是否可以验证多个范围的参数输入 例如 假设我希望允许端口输入为 1 到 80 135 到 445 以及 10
  • 是否可以在 PowerShell 中使 IndexOf 不区分大小写?

    我在终端服务器中由查询会话命令组成的数组中搜索索引时遇到问题 这是有问题的脚本 Array of logged users in terminal servers a Get RDUsersession CollectionName BLA
  • 如何在不安装 AWS SDK 的情况下通过 Powershell 从 S3 下载文件?

    我想使用 Windows Powershell 从我的 AWS S3 存储桶下载文件 我无法安装任何 AWS 软件 需要创建一个 API 才能访问 AWS S3 中的文件 我使用Postman测试该文件是否可访问并且成功 鉴于这一成功 我尝
  • 如何并行执行PowerShell函数多次?

    我不确定是否需要将其称为多线程 基于作业或异步的需求 但基本上我有一个 Powershell 脚本函数 它需要多个参数 并且我需要使用不同的参数多次调用它并让它们运行在平行下 目前 我这样调用该函数 Execute param1 param
  • VSTS 任务组 Powershell 参数

    我使用具有四个参数的 Azure Powershell 任务内联脚本创建了 VSTS 任务组 我已将此任务组添加到发布定义并配置参数 当我尝试释放时失败并出现以下错误 2018 03 23T10 28 42 2811600Z 错误 在 C
  • Powershell从字符串中获取数字

    大家好 我正在尝试使用 powershell 从用户 ID 中获取号码 我们使用的格式是名字的第一个字母 姓氏的前四个字母和学生 ID 因此名为 John Smith ID 123456 的学生将是 jsmit123456 如果用户的名字少

随机推荐