在 PowerShell 中检查 net.tcp 绑定
Check for net.tcp binding in PowerShell
我正在配置一个进程来检查 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:\LOSSI
HTTP *:63211: net.tcp 443:
我想我做错了什么傻事,但我想不通。有没有更简单的方法来检查网站的 tcp 绑定?
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
在
中添加此功能
对于 powershell 64 位
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
我正在配置一个进程来检查 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:\LOSSI
HTTP *:63211: net.tcp 443:
我想我做错了什么傻事,但我想不通。有没有更简单的方法来检查网站的 tcp 绑定?
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
在
中添加此功能
对于 powershell 64 位
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