Код:
param (
#[parameter(Mandatory=$true)]
[string]$outfile = 'D:\PowerShell\temp\общий.csv',
[string]$list = 'D:\PowerShell\list_Net.txt',
$enc1 = 65001,
$enc2 = 28591,
[int[]]$SelectTable = 1
)
Get-WmiObject Win32_NetworkAdapter -Filter 'AdapterTypeId = 0 and MACAddress is not null' |
select @{n='Системный блок';e={$_.macaddress}}| convertto-csv -d ';' -notype|
out-file $outfile -enc utf8
$ofs = ''
gwmi wmimonitorid -Namespace root\wmi |
Select @{n='UserFriendlyName';e={
([string][Char[]]$_.UserFriendlyName).SubString(0,$_.UserFriendlyNameLength)
}}, @{n='SerialNumberID';e={[string][Char[]]$_.SerialNumberID}} | convertto-csv -d ';' -notype|
out-file $outfile -enc utf8 -app
#---Эту секцию можно удалить после однократного запуска скрипта от имени админа ------------------
# Установка дополнительного модуля PowerHTML, для независимого парсинга HTML
# Может потребоваться ручное подтверждение установки.
if (!(get-module -list powerhtml)) {
write-verbose "Installing PowerHTML module for the current user..."
install-module powerhtml #-scope currentuser
}
#-----------------------------------------------------------------------------------------
function convert ($from, $to){
begin{
$fenc = [text.encoding]::getencoding($from)
$tenc = [text.encoding]::getencoding($to)
}
process{
$a = $tenc.getbytes($_)
$fenc.getstring($a)
}
}
[net.servicepointmanager]::securityprotocol = 'ssl3,tls,tls11,tls12'
[array]$arr = gc $list
$ips = 130..190 |%{$n=$_;$arr|%{"$_"+$n}}|sort|?{test-connection $_ -count 1 -q}
$ips|%{
if (test-connection $_ -count 1 -q){
try {
$a = (iwr $_).content
$content = if ($a -match [char]208){
if ($a -match 'windows-1251'){$enc1,$enc2 = 1251,1252}
$a|convert $enc1 $enc2
} else {$a}
$html = [net.webutility]::htmldecode($content)|convertfrom-html
[array]$tables = $html.selectnodes('//table')
# Исключение вложенных таблиц:
$tables = $tables|?{$_.innerhtml -notmatch 'table'}
if ([string]::isnullorempty($selecttable)){
$number = 0..($tables.count-1)
} else {$number = $selecttable}
$tbl = $tables[$number]|%{$n=0}{
$tr = if($_.element('tbody')){
$_.element('tbody').elements('tr')
} else {$_.elements('tr')}
$headers = @()
if($headers = $tr|?{$_.element('th') -ne $null}|
%{$_.elements('th')}|select -exp innertext){
$headers=$headers.trim()
} else {
$headers = 1..(
[linq.parallelenumerable]::max(
[linq.parallelenumerable]::asparallel(($tr|
%{($_.elements('td')|? innertext -ne ''|%{$_}).count}))
)
)|%{"H$_"}
}
$rowind = ,1 * $headers.count
$tr|?{$_.elements('td') -ne ''}|%{
$row = $_.elements('td')|? innertext -ne ''|%{
$attr = $_.attributes
if ($attr){
$rowspan = ($attr|? name -eq 'rowspan'|select value).value
$colspan = ($attr|? name -eq 'colspan'|select value).value
}
[pscustomobject]@{
'InnerText' = $_.innertext
'RowSpan' = if($rowspan){[int]$rowspan} else {1}
'ColSpan' = if($colspan){[int]$colspan} else {1}
}
}
$str = [ordered]@{}
$k=0
foreach ($item in $row){
if ($rowind[$k] -gt 1){
while ($rowind[$k] -gt 1){
$str[$headers[$k]] = $null
$rowind[$k] -= 1
$k++
}
}
if (($colspan = $item.colspan) -gt 1) {
$str[$headers[$k]] = if($item.innertext){
$item.innertext.trim()
} else {$null}
if ($item.rowspan -gt 1){$rowind[$k] = $item.rowspan}
$k++
while ($colspan -gt 1){
$str[$headers[$k]] = $null
$colspan -=1
if ($rowind[$k] -gt 1){$rowind[$k]-=1}
$k++
}
} else {
$str[$headers[$k]] = if($item.innertext){
$item.innertext.trim()
} else {$null}
if ($item.rowspan -gt 1){$rowind[$k] = $item.rowspan}
$k++
}
}
[pscustomobject]$str
}
}
[pscustomobject]@{
'IP' = $_
'MAC-адрес' = if ([array]::indexof($tbl.h1,'MAC-адрес') -ne -1){
$tbl[([array]::indexof($tbl.h1,'MAC-адрес'))].h2
} else {$tbl[([array]::indexof($tbl.h1,'MAC Address'))].h2}
'Номер телефона' = if ([array]::indexof($tbl.h1,'Номер телефона') -ne -1){
$tbl[([array]::indexof($tbl.h1,'Номер телефона'))].h2
} elseif ([array]::indexof($tbl.h1,'Номер телефона 1') -ne -1){
$tbl[([array]::indexof($tbl.h1,'Номер телефона 1'))].h2
} else {
$tbl[([array]::indexof($tbl.h1,'Phone DN'))].h2
}
'Серийный номер' = if ([array]::indexof($tbl.h1,'Серийный номер') -ne -1){
$tbl[([array]::indexof($tbl.h1,'Серийный номер'))].h2
} else {$tbl[([array]::indexof($tbl.h1,'Serial Number'))].h2}
'Номер модели' = if ([array]::indexof($tbl.h1,'Номер модели') -ne -1){
$tbl[([array]::indexof($tbl.h1,'Номер модели'))].h2
} else {$tbl[([array]::indexof($tbl.h1,'Model Number'))].h2}
}
} catch {
write-host Ошибка: $_ -for red
}
}
} | convertto-csv -d ';' -notype|out-file $outfile -enc utf8 -app