[TOC]

0x00 网络连接测试

Test-Connection 命令 - 向一台或多台计算机发送ICMP回显请求数据包或ping

描述:可以类比于cmd中的nbtstat明进行获取局域网中的指定计算机名的IPv4/6地址信息以及MAC地址;

基础语法:

1
2
3
4
5
PS > (Get-Command *-Connection).Name
Test-Connection

# 语法
Test-Connection [-ComputerName] <System.String[]> [-AsJob] [-BufferSize <System.Int32>] [-Count <System.Int32>] [-DcomAuthentication {Default | None | Connect | Call | Packet | PacketIntegrity | PacketPrivacy | Unchanged}] [-Delay <System.Int32>] [-Impersonation {Default | Anonymous | Identify | Impersonate | Delegate}] [-Protocol {DCOM | WSMan}] [-ThrottleLimit <System.Int32>] [-TimeToLive <System.Int32>] [-WsmanAuthentication {Default | Basic | Negotiate | CredSSP | Digest | Kerberos}] [<CommonParameters>]


基础示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 1.获取本机计算机名相关信息
PS C:\Users\WeiyiGeek> Test-Connection -ComputerName WeiyiGeek
# Source Destination IPV4Address IPV6Address Bytes Time(ms)
# ------ ----------- ----------- ----------- ----- --------
# WEIYIGEEK WeiyiGeek 10.10.10.107 fe80:::fe6c:10bf:4244%15 32 0
# 获取指定的计算机名称相关信息
Test-Connection -ComputerName Server01, Server02, Server12
# Source Destination IPV4Address IPV6Address Bytes Time(ms)
# ------ ----------- ----------- ----------- ----- --------
# WEIYIGEEK Server01 10.20.172.102 fe80::f5d4:6342:8038:f1b9%12 32 0
# WEIYIGEEK Server01 10.20.172.102 fe80::f5d4:6342:8038:f1b9%12 32 1
# WEIYIGEEK Server01 10.20.172.102 fe80::f5d4:6342:8038:f1b9%12 32 0
# WEIYIGEEK Server01 10.20.172.102 fe80::f5d4:6342:8038:f1b9%12 32 0

# 2.测试连接指定IP地址信息
Test-Connection 10.20.172.106
# Source Destination IPV4Address IPV6Address Bytes Time(ms)
# ------ ----------- ----------- ----------- ----- --------
# WEIYIGEEK 10.10.10.107 10.10.10.107 fe80::88cc:5b4:9dde:3e4d%12 32 1

# 3.从多台计算机向一台计算机发送回显请求
Test-Connection -Source Server02, Server12, localhost -ComputerName Server01 -Credential Domain01\Admin01

# 4.使用参数自定义测试命令(发送 256 字节, 32字节 节流阀)
Test-Connection -ComputerName WeiyiGeek -Count 3 -Delay 2 -TTL 255 -BufferSize 256 -ThrottleLimit 32

# 5.ComputerName参数的值是一个“Get Content”命令,它从`服务器.txt文件“”。该命令使用AsJob参数将命令作为后台作业运行,并将作业保存在“$job”变量中。
$job = Test-Connection -ComputerName (Get-Content Servers.txt) -AsJob
# “if”命令检查作业是否仍在运行。如果作业没有运行,“Receive job”获取结果并将其存储在“$results”变量中。
if ($job.JobStateInfo.State -ne "Running") {$Results = Receive-Job $job}


# 6.用凭据Ping远程计算机
# 该命令使用Credential参数指定有权ping远程计算机的用户帐户,并使用Impersonation参数更改要标识的模拟级别。
Test-Connection Server55 -Credential Domain55\User01 -Impersonation Identify


# 7.仅当连接测试成功时才创建会话
if (Test-Connection -ComputerName Server01 -Quiet) {New-PSSession Server01}