ping IP范围,获取在线PC的信息



实际上我想修改我现有的powershell脚本。目前,我需要提供一个机器名称列表。我需要如何修改我的脚本,我不需要一个TXT文件,但只提供一个IP范围。脚本显示哪些ip/pc名是在线的等等

$machines = Get-Content -Path "C:temppcnames.txt"
function Get-LoggedOnUser
{
[CmdletBinding()]
param
(
[Parameter()]
[ValidateScript({ Test-Connection -ComputerName $_ -Quiet -Count 1 })]
[ValidateNotNullOrEmpty()]
[string[]]$ComputerName = $env:COMPUTERNAME
)
foreach ($comp in $machines)
{
$output = @{ 'ComputerName' = $comp }
$output.UserName = (Get-WmiObject -Class win32_computersystem -ComputerName $comp).UserName
$output.Info = (Get-CimInstance -Class CIM_ComputerSystem -ComputerName $comp).model
$output.IP = (Get-CimInstance -CimSession $comp -ClassName Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'").IPAddress[0]
[PSCustomObject]$output
}
}
Get-LoggedOnUser | Out-GridView

你是说这样吗?

function Test-IpRange {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[IpAddress]$IpStart,
[Parameter(Mandatory=$true, Position = 1)]
[IpAddress]$IpEnd
)
# calculate the range of IP addresses by converting the IP to decimal format
# avoid using obsolete property ([IpAddress]$IpAddress).Address
[byte[]]$bytes = $IpStart.GetAddressBytes()
[array]::Reverse($bytes)
$startRange = [System.BitConverter]::ToUInt32($bytes, 0)
[byte[]]$bytes = $IpEnd.GetAddressBytes()
[array]::Reverse($bytes)
$endRange = [System.BitConverter]::ToUInt32($bytes, 0)
# swap if startaddress > end address
if ($startRange -gt $endRange) { $startRange,$endRange = $endRange, $startRange }
for($i = $startRange; $i -le $endRange; $i++) {
$adr = ([IPAddress]$i).GetAddressBytes()
[array]::Reverse($adr)
$ipToTest = [IPAddress]::Parse($($adr -join '.')).IPAddressToString
$ping = Test-Connection -ComputerName $ipToTest -Count 1 -Quiet -ErrorAction SilentlyContinue
if ($ping) {
$computerName = (([System.Net.Dns]::GetHostEntry($ipToTest)).Hostname -split '.')[0]
$computerInfo = Get-CimInstance -Class CIM_ComputerSystem -ComputerName $computerName
$adapterInfo  = Get-CimInstance -ComputerName $computerName -Class Win32_NetworkAdapterConfiguration | 
Where-Object { $_.IPAddress -contains $ipToTest }
[PsCustomObject]@{
ComputerName = $computerName
IPAddress    = $ipToTest
Online       = $true
CurrentUser  = $computerInfo.UserName
Manufacturer = $computerInfo.Manufacturer
Model        = $computerInfo.Model
MACAddress   = $adapterInfo.MACAddress
}
}
else {
[PsCustomObject]@{
ComputerName = 'N/A'
IPAddress    = $ipToTest
Online       = $false
CurrentUser  = 'N/A'
Manufacturer = 'N/A'
Model        = 'N/A'
MACAddress   = 'N/A'
}
}
}
}
Test-IpRange '192.168.0.10' '192.168.0.15' | Format-Table -AutoSize

将返回类似

的内容
ComputerName IPAddress    Online CurrentUser     Manufacturer        Model    MACAddress       
------------ ---------    ------ -----------     ------------        -----    ----------       
PC07         192.168.0.10   True Contosochkdsk  Some PC maker, Ltd. ABCDE123 01:23:45:67:89:AA
PC03         192.168.0.11   True Contosowipedsk Some PC maker, Ltd. FGHIJ789 0A:1B:1C:1D:1E:1F
N/A          192.168.0.12  False N/A             N/A                 N/A      N/A              
N/A          192.168.0.13  False N/A             N/A                 N/A      N/A              
N/A          192.168.0.14  False N/A             N/A                 N/A      N/A              
N/A          192.168.0.15  False N/A             N/A                 N/A      N/A              


可以if ($ping) { .. }之间的所有内容替换为

Invoke-Command -ComputerName $ipToTest -HideComputerName -ScriptBlock {
# this will now run on the remote computer
$computerInfo = Get-CimInstance -Class CIM_ComputerSystem
$adapterInfo  = Get-CimInstance -Class Win32_NetworkAdapterConfiguration | 
Where-Object { $_.IPAddress -contains $using:ipToTest }
[PsCustomObject]@{
ComputerName = $env:COMPUTERNAME
IPAddress    = $using:ipToTest
Online       = $true
CurrentUser  = $computerInfo.UserName
Manufacturer = $computerInfo.Manufacturer
Model        = $computerInfo.Model
MACAddress   = $adapterInfo.MACAddress
}
} | Select-Object * -ExcludeProperty RunspaceId

并通过它让远程计算机而不是本地计算机收集信息。但是,如果这样会更快,我就不能说/test myself了。

Invoke-Command也可以在其-ComputerName参数中接受计算机名/IpAddresses数组,这可能会加快脚本的速度。
问题是,您必须首先在整个范围内使用Test-Connection获得可ping通的IP地址的子数组,并捕获哪些机器可以ping通,哪些机器失败。
对于可ping通的机器,然后使用单个Invoke-Command -ComputerName $onlineMachines,对于失败的机器,运行单独的循环以输出具有N/A的对象

最新更新