我有以下 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> </td><td align=left id=2> </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> </td><td align=left id=2> </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> </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(使用前将#替换为@)