【问题标题】:Check for net.tcp binding in PowerShell在 PowerShell 中检查 net.tcp 绑定
【发布时间】:2015-03-27 16:39:37
【问题描述】:

我正在配置一个进程,该进程检查 Web 服务器上的 IIS 设置以查找特定网站的 net.tcp 绑定,如果它不存在,则创建它。我有这段代码要检查

$Websites = Get-ChildItem IIS:\Sites

foreach ($Site in $Websites) {

        if ($Site.name -eq "LOSSI") {

           $Binding = $Site.bindings
           foreach ($bind in $Binding.collection) {

                    if ($bind -eq "net.tcp 443:*")
                       {        
                            Write-Host $bind
                       }
            }
        }
}

但我从不落入最后一个条件句中。我已经手动验证绑定设置为

损失
3
开始
D:\损失
http *:63211: net.tcp 443:

我想我在做一些愚蠢的错误,但我无法弄清楚。有没有更简单的方法来检查网站的 tcp 绑定?

【问题讨论】:

    标签: powershell iis binding


    【解决方案1】:


    function Test-TcpPort {
        <#
            .SYNOPSIS
                Determine if computers have the specified ports open.
    
            .EXAMPLE
                PS C:\> Test-TcpPort -ComputerName web01,sql01,dc01 -Port 5985,5986,80,8080,443
            .NOTE
                Example function from PowerShell Deep Dives 2013.
        #>
        [CmdletBinding()]
        param(
            [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
            [Alias("CN","Server","__Server","IPAddress")]
            [string[]]$ComputerName = $env:COMPUTERNAME,
    
            [int[]]$Port = 23,
    
            [int]$Timeout = 5000
        )
    
        Process {
            foreach ($computer in $ComputerName) {
                foreach ($p in $port) {
                    Write-Verbose ("Checking port {0} on {1}" -f $computer, $p)
    
                    $tcpClient = New-Object System.Net.Sockets.TCPClient
                    $async = $tcpClient.BeginConnect($computer, $p, $null, $null)
                    $wait = $async.AsyncWaitHandle.WaitOne($TimeOut, $false)
                    if(-not $Wait) {
                        [PSCustomObject]@{
                            Computername = $ComputerName
                            Port = $P
                            State = 'Closed'
                            Notes = 'Connection timed out'
                        }
                    } else {
                        try {
                            $tcpClient.EndConnect($async)
                            [PSCustomObject]@{
                                Computername = $computer
                                Port = $p
                                State = 'Open'
                                Notes = $null
                            }
                        } catch {
                            [PSCustomObject]@{
                                Computername = $computer
                                Port = $p
                                State = 'Closed'
                                Notes = ("{0}" -f $_.Exception.Message)
                            }
                        }
                    }
                }
            }
        }
    }
    

    Microsoft reference script for check port
    中添加此功能 适用于 64 位的 powershell

    C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
    

    适用于 32 位的 powershell

    C:\Windows\SysWOW64\WindowsPowerShell\v1.0\profile.ps1
    

    然后打开powershell使用这个

    Test-TCPPort google.com -Port 80
    

    输出:

    True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多