返回每个绑定解析为IIS的IP地址

时间:2018-11-11 21:11:17

标签: asp.net powershell iis iis-8.5

我正在努力收集一个脚本,该脚本将告诉我服务器上的每个绑定以及该绑定解析到的实时IPv4地址。我非常接近,但是由于我将结果从IIS导出到CSV,然后在该CSV的每个绑定上运行测试连接,因此无法将结果添加到数组并将完成的结果导出到CSV文件。这是脚本:

Import-Module WebAdministration
$hostname = hostname
$Websites = Get-ChildItem IIS:\Sites
$date = (Get-Date).ToString('MMddyyyy')
foreach ($Site in $Websites) {
    $Binding = $Site.bindings
    [string]$BindingInfo = $Binding.Collection
    [string[]]$Bindings = $BindingInfo.Split(" ")#[0]
    $n = 1
    $i = 0
    $status = $site.state
    $path = $site.PhysicalPath
    $fullName = $site.name
    $state = ($site.name -split "-")[0]
    $Collection = ($site.name -split "-")[1]
    $status = $site.State
    $anon = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/AnonymousAuthentication -Name Enabled -PSPath IIS:\sites -Location $site.name | select-object Value
    $basic = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/BasicAuthentication -Name Enabled -PSPath IIS:\ -location $site.name | select-object Value
    Do{
        if( $Bindings[($i)] -notlike "sslFlags=*"){
            [string[]]$Bindings2 = $Bindings[($i+1)].Split(":")
            $obj = New-Object PSObject
            $obj | Add-Member Date $Date
            $obj | Add-Member Host $hostname
            $obj | Add-Member State $state
            $obj | Add-Member Collection $Collection
            $obj | Add-Member SiteName $Site.name
            $obj | Add-Member SiteID $site.id
            $obj | Add-member Path $site.physicalPath
            $obj | Add-Member Protocol $Bindings[($i)]
            $obj | Add-Member Port $Bindings2[1]
            $obj | Add-Member Header $Bindings2[2]
            #$obj | Add-member ResolveAddress $result
            $obj | export-csv "c:\temp\$date-$hostname.csv" -Append -notypeinformation
            $i=$i+2
        }
        else{$i=$i+1}
    } while ($i -lt ($bindings.count))
}

$CSVvar = import-csv "c:\temp\$date-$hostname.csv"
$i=0
foreach ($v in $CSVvar){
If ($v.Header -ne '') {
$result = Test-Connection $v.Header -ErrorAction SilentlyContinue
if ($result) {
    $IP = ($result.IPV4Address).IPAddressToString
    echo $IP
    $v.ResolveAddress = $IP
}
}
}
$CSVvar | export-csv "c:\temp\$date-$hostname.csv" -Append -notypeinformation

我认为最好是在第一次导出CSV文件之前对每个绑定执行ping操作,但是我想不出一种方法来使它单独尝试Header对象的每个元素,而且我一直崩溃这样的脚本。如果我可以获取要保存回$ CSVvar.ResolveAddress列的IP地址,那对我来说很好。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

这似乎是很多代码,只是为了做到这一点。这种方法怎么样?

### Get website info

Import-Module -Name WebAdministration
Get-Website | select name,id,state,physicalpath,
@{n="Host"; e= { $env:COMPUTERNAME }},
@{n="Bindings"; e= { ($_.bindings | select -expa collection) -join ';' }},
@{n="LogFile";e={ $_.logfile | select -expa directory}}, 
@{n="attributes"; e={($_.attributes | % { $_.name + "=" + $_.value }) -join ';' }}, 
@{n="ConnectionTest"; e={(Test-Connection -ComputerName ($Bindings -split 'http |:')[1] -Count 1 -Quiet)}} | 
Export-Csv -NoTypeInformation -Path 'C:\temp\my_list.csv'

Import-Csv -Path 'C:\temp\my_list.csv'

# Results

name           : Default Web Site
id             : 1
state          : Started
physicalPath   : %SystemDrive%\inetpub\wwwroot
Host           : IIS01
Bindings       : http *:80:;https *:443: sslFlags=0
LogFile        : %SystemDrive%\inetpub\logs\LogFiles
attributes     : name=Default Web Site;id=1;serverAutoStart=True;state=1
ConnectionTest : True

name           : kcd
id             : 2
state          : Started
physicalPath   : C:\inetpub\kcd
Host           : IIS01
Bindings       : http 192.168.7.11:80:kcd.contoso.com
LogFile        : %SystemDrive%\inetpub\logs\LogFiles
attributes     : name=kcd;id=2;serverAutoStart=True;state=1
ConnectionTest : True

当然,可以根据需要分解/挑选其他所有收集的片段/信息到单独的计算属性中。

OP更新

  

...但实际上我遇到的所有麻烦是从以下位置获取IPv4信息   附加到原始文档的ConnectionTest响应...

只需使用与测试连接相同的代码即可。

Import-Module -Name WebAdministration
Get-Website | select name,id,state,physicalpath,
@{n="Host"; e= { $env:COMPUTERNAME }},
@{n="Bindings"; e= { ($_.bindings | select -expa collection) -join ';' }},
@{n="LogFile";e={ $_.logfile | select -expa directory}}, 
@{n="attributes"; e={($_.attributes | % { $_.name + "=" + $_.value }) -join ';' }}, 
@{n="ConnectionTest"; e={(Test-Connection -ComputerName ($Bindings -split 'http |:')[1] -Count 1 -Quiet)}},
@{n="SiteIPA"; e={($Bindings -split 'http |:')[1]}}