远程搜集主机网卡信息

开启功能

客户端

默认情况下,在Windows 10或者11 首次执行powershell脚本会提示”系统禁止脚本运行“的错误。这是因为M$为了安全,把powershell的脚本执行策略设定为有数字证书的才允许执行。因此,需要执行脚本的方法一个是使用合法的数字证书对脚本进行签名:

# 获取合法的PFX证书(IIS版本SSL证书)
# 一般情况下都会为证书设置密码,所以这一步需要输入密码进行验证
$cert = Get-PfxCertificate -FilePath "test.pfx"

# 然后为 demo.ps1 脚本文件签名:
Set-AuthenticodeSignature -FilePath "demo.ps1" `
-Certificate $cert -IncludeChain "All" `
-TimeStampServer "http://timestamp.verisign.com/scripts/timstamp.dll"

或者我们干脆取消这一限制:

# 设置执行策略无签名脚本执行
set-executionpolicy remotesigned

服务端

如果服务器已经加入Windows域环境的话,

# 打开远程管理,接受所有机器连接
Enable-PSRemoting -Force
Set-Item -Path WSMan:\localhost\client\trustedhosts -Value * -Force
# 关闭远程管理
# Disable-PSRemoting -force

执行搜集

# 添加需要搜集目标主机计算机名
$ServerList=( 'servera.example.com','serverb.example.com','serverc.example.com')
# 使用for循环把各个主机的网卡信息列出来
foreach ( $ServerName in $ServerList ) {
# 列出主机名
Invoke-Command -ComputerName $ServerName -Command {Get-Content Env:COMPUTERNAME}
# 列出主机网卡信息
Invoke-Command -ComputerName $ServerName -Command {Get-Netadapter |ft}
# 列出主机网卡地址信息
Invoke-Command -ComputerName phpv153003.firstshare.cn -Command { Get-NetIPAddress | Sort -Property ifIndex |ft -autosize |ft }
}

输出结果

servera

Name InterfaceDescription ifIndex Status MacAddress LinkSpeed
---- -------------------- ------- ------ ---------- ---------
NIC4 Broadcom NetXtreme Gigabit Ethernet #2 5 Disconnected 54-9F-35-15-FD-5F 0 bps
NIC3 Broadcom NetXtreme Gigabit Ethernet #4 7 Not Present 54-9F-35-15-FD-5E 0 bps
NIC2 Broadcom NetXtreme Gigabit Ethernet 2 Up 54-9F-35-15-FD-5D 1 Gbps
NIC1 Broadcom NetXtreme Gigabit Ethernet #3 11 Up 54-9F-35-15-FD-5C 1 Gbps


ifIndex IPAddress PrefixLength PrefixOrigin SuffixOrigin AddressState PolicyStore
------- --------- ------------ ------------ ------------ ------------ -----------
1 ::1 128 WellKnown WellKnown Preferred ActiveStore
1 127.0.0.1 8 WellKnown WellKnown Preferred ActiveStore
3 fe80::5efe:10.12.153.103%3 128 WellKnown Link Deprecated ActiveStore
7 fe80::5efe:172.31.153.3%7 128 WellKnown Link Deprecated ActiveStore
8 169.254.31.27 16 WellKnown Link Tentative ActiveStore
8 fe80::7c00:7d4d:22ea:1f1b%8 64 WellKnown Link Deprecated ActiveStore