Check for net.tcp binding in PowerShell

坚强是说给别人听的谎言 提交于 2019-12-12 06:06:14

问题


I am configuring a process that checks IIS settings on a Web Server for net.tcp bindings for a particual Web Site, and if it does not exist, create it. I have this chunk of code to check

$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
                       }
            }
        }
}

But I never fall into the last conditional. I have validated by hand that the binding is set to

LOSSI
3
Started
D:\LOSSI
http *:63211: net.tcp 443:

I imagine I am doing something silly wrong, but I cannot figure it out. Is there an easier way to check a website for tcp 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 add this function in
for powershell 64 bit

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

for powershell 32 bit

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

then open powershell use this

Test-TCPPort google.com -Port 80

output :

True


来源:https://stackoverflow.com/questions/29305785/check-for-net-tcp-binding-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!