【问题标题】:Return the IP address each binding resolves to in IIS返回每个绑定在 IIS 中解析到的 IP 地址
【发布时间】:2018-11-11 21:11:17
【问题描述】:

我正在编写一个脚本,它将告诉我服务器上的每个绑定以及绑定解析到的实时 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 对象的每个元素,我一直在崩溃脚本就是这样。如果我可以将 IP 地址保存回 $CSVvar.ResolveAddress 列,那对我来说很好。任何帮助表示赞赏!

【问题讨论】:

  • 站点绑定可以完全没有 IP 地址(例如,全部未分配)。所以你的脚本做了很多你没有在问题中透露的假设。除非您对其进行编辑以包含此类内容,否则无法回答。

标签: asp.net powershell iis iis-8.5


【解决方案1】:

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

### 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]}}

【讨论】:

  • 谢谢,是的,您的方法确实看起来更优雅。也许我给出了太多的上下文,但我真正遇到的问题是从附加到相应绑定来自的原始文档的 ConnectionTest 响应中获取 IPv4 信息,例如:#foo.csv google.com Code Executes...$CSVvar | export-csv -Path 'bar.csv' -Append -notypeinformation import-csv -Path 'bar.csv' #Results Name: google.com IPv4: 172.217.15.110
  • 但是与绑定关联的 IPA 在绑定字段中。为什么要尝试从测试连接结果中再次获取它?您可以像我为那个 Test-Connection 调用所做的那样从绑定字段中提取它。请查看我的更新。
  • 因为这是一个维护脚本,有助于确定实际上没有将其域指向该服务器的站点,以便可以删除该站点。不过,我想用它做什么并不重要,我需要的是绑定实际解析到的 IP 地址。如果它与 IIS 中的内容相匹配,那就太棒了。如果不是,则可以启动该站点。询问原因是没有帮助的。我的问题很清楚,解决方案仍然没有。
猜你喜欢
  • 2011-06-19
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2012-05-15
  • 2010-09-27
  • 2016-10-05
  • 2012-03-14
  • 2013-01-27
相关资源
最近更新 更多