1

我有一个执行双重 dns 查找的 powershell 脚本。首先它将 IP 传递给服务器以返回主机名,然后获取主机名并将其发送回以返回 IP 地址。这用于确保 DNS 使用正确的主机名注册正确的 IP。

我的老板想要一些更复杂的东西。当我将输出推送到 HTML 文档时,我必须让它一次调用我们所有的作用域,然后显示一个摘要页面。摘要页面有一个指向每个子网结果页面的链接,应该一目了然地显示有效响应、无效响应、没有主机名的响应和总数。

我似乎无法让变量 $($valid) 正确显示在表格上,因为它一直给我一个 0。与其他人一样。我的代码如下:

#BEGIN
<#
.NOTES 
    ScriptName : iphostip.ps1 
    Created By : W. Alex Williams
    Contact    : 
    Date Coded : 5/15/2013 10:29pm 
.PURPOSE
    This script is used to check DNS records to make sure that the Hostname is registered to the same IP as what it shows in DNS.

#> 
#Gather the list of IPs...
$listofIPs = Get-Content .\IPList.txt

#Set the HTML Header
$header = @'
<style>
BODY{font-family:Verdana; background-color:black;background-image:url("../assets/bg.jpg");;background-repeat:no-repeat;background-attachment:fixed;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;width:75%;}
TH{font-size:1.3em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#8FD8D8}
TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:white}
H1{color:white; text-align:center;}
H3{color:white; text-align:center;}
</style>
<title> DNS Integrity Report</title>
<link rel=”icon” href=”assets/favicon.ico” type=”image/x-icon”&gt;
<link rel=”shortcut icon” href=”assets/favicon.ico” type=”image/x-icon”&gt;
'@
#Setting some misc variables....
$linktofile = "<h1>Results</h1>"
$Valid = "0"
$Invalid = "0"
$NoHostName = "0"
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$Timestamp = Get-Date -format yyyy.MM.dd.HH.mm.ss
$LogDirectory = $scriptPath + "\logs\" + $Timestamp
New-Item -ItemType directory -Path $LogDirectory | out-null

#Looping the function through the list:

$JobWork = {
param ($ipnet, $scriptPath, $header, $Timestamp, $LogDirectory)


$ResultList = @()
$ResultList += "<h1>DNS Integrity Report</h1>"
$ResultList += "<table align='center'><tbody><tr><th>Original IP</th><th>IsAlive</th><th>Hostname</th><th>Checked IP</th><th>Match?</th></tr>"



$i = 0
while ($i -lt 254) 
    {

    $i++
    $ip = $ipnet + $i

    $result = $null
    Write "Checking $($IP)..."
    $currentEAP = $ErrorActionPreference
    #Don't wanna wake the neighbors
    $ErrorActionPreference = "silentlycontinue"
    #Getting the Hostname
    $result = [System.Net.Dns]::gethostentry($ip)
    $ErrorActionPreference = $currentEAP
    $IsAlive = Test-Connection $ip -quiet -count 1
    If ($Result)
        {
        $IPCheck = "Unknown"
        #Getting the IP from the Hostname
        $IPCheck = @(([net.dns]::GetHostEntry($Result.Hostname)).AddressList)
        If ($IP -eq $IPCheck)
            {
                $TrueFalse = "<td style='background-color:green;'>True</td>"
                $Valid++
            }
        Else
            {
                #Something must be fishy....
                $TrueFalse = "<td style='background-color:red;'>False</td>"
                $Invalid++
            }
        $Resultlist += "<tr><td>$($IP)</td><td>$($IsAlive)</td><td>$([string]$Result.Hostname)</td><td>$($IPCheck)</td>$($TrueFalse)<tr>"
        }
    Else
        {
            #Well duh. If there was no hostname, of course it couldn't check for an IP
            $Resultlist += "<tr><td>$IP</td><td>$($IsAlive)</td><td>No Hostname Found</td><td>None</td><td style='background-color:Orange;'>N/A</td><tr>"
            $NoHostName++
        }
    }
$OutputName = "$($LogDirectory)\$($ipnet)0.htm"
$Count = $Valid + $Invalid + $NoHostName
$Resultlist += "</tbody></table><br /><p style='text-align:center;color:red;background-color:black;padding:3px;'><tr><td>Valid: $($Valid)  ||  </td><td>Invalid: $($Invalid)  ||  </td><td>No Host Name: $($NoHostName)  ||  </td><td>Total: $($Count)</td><tr>"
$resultlist += "<br /><p style='text-align:center;color:red;background-color:black;padding:3px;'>Results from IpHostIp.ps1 Script created by Salerno Systems Administration team. This program is free to use, but please do not claim ownership.</p>"
#Sending in the results:
ConvertTo-HTML -head $header -body $resultlist | Out-File $OutputName
}   
$linktofile += "<table align='center'><tbody><tr><th>Link to file</th><th>Valid</th><th>Invalid</th><th>No Hostname</th></tr>"
foreach ($ipnet in $listofIPs)
{
Start-Job -ScriptBlock $jobWork -ArgumentList $ipnet, $scriptPath, $header, $Timestamp, $LogDirectory
$linktofile += "<tr><td><a href='file:///$($LogDirectory)\$($ipnet)0.htm'>$($ipnet)0</a></td><td>$($Valid)</td><td>$($Invalid)</td><td>$($NoHostName)</td></tr>"
}

Get-Job | Wait-Job
Get-Job | Receive-Job
Get-Job | Remove-Job
$indexhead = @'
<style>
BODY{font-family:Verdana; background-color:black;background-image:url("../assets/bg.jpg");;background-repeat:no-repeat;background-attachment:fixed;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;width:75%;}
TH{font-size:1.3em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#8FD8D8}
TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:white}
H1{color:white; text-align:center;}
H3{color:white; text-align:center;}
</style>
<title> DNS Integrity Report</title>
<link rel=”icon” href=”assets/favicon.ico” type=”image/x-icon”&gt;
<link rel=”shortcut icon” href=”assets/favicon.ico” type=”image/x-icon”&gt;
'@
$linktofile += "</tbody></table>"
ConvertTo-HTML -head $indexhead -body $linktofile | Out-File "$($Timestamp).index.htm"
echo "Done."
#END

必须创建 IPList.txt 文件并设置它,只需输入 IP 范围的前 3 个八位字节(IE 192.168.1。)

任何帮助是极大的赞赏。

4

0 回答 0