检查PowerShell中的net.tcp绑定

时间:2015-03-27 16:39:37

标签: powershell iis binding

我正在配置一个进程,检查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
                       }
            }
        }
}

但我从未陷入最后的条件。我已经手动验证绑定设置为

LOSSI
 3
入门
d:\ LOSSI
http *:63211:net.tcp 443:

我想我正在做一些愚蠢的错事,但我无法弄明白。是否有更简单的方法来检查网站的tcp绑定?

1 个答案:

答案 0 :(得分: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 在中添加此功能 for powershell 64 bit

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

for powershell 32 bit

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

然后打开powershell使用此

Test-TCPPort google.com -Port 80

输出:

True