Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell选择HTML文本_Powershell - Fatal编程技术网

Powershell选择HTML文本

Powershell选择HTML文本,powershell,Powershell,我使用以下powershell脚本在多台计算机上打开多个HTML文件以提取许可信息。正在查找提取其中id=2的innertext元素的帮助 希望提取以下内容: 产品、序列号、产品密钥、请求代码 HTML文件的示例 <table width="100%" cellspacing=0 cellpadding=0> <tr><td align=left id=1 width=15%>Product:</td><td align=left i

我使用以下powershell脚本在多台计算机上打开多个HTML文件以提取许可信息。正在查找提取其中id=2的innertext元素的帮助

希望提取以下内容:

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

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 ""

查看并尝试捕获什么,例如序列号:999-9999999?或者整件事的序列号:只要这个号码就好了。
# 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 ""