PowerShell 多输入 IP 地址

PowerShell Multiple Input IP Addresses

我在这段代码中遇到多个输入问题:

cls
Install-Module SNMP
Get-Command -Module SNMP

Write-Output ----------------------------------------
$IP = "192.168.15.1"
Write-Output ----------------------------------------
Write-Output "Selected IP is: $IP"
Write-Output ----------------------------------------

Invoke-SnmpWalk -IP $IP -Community public -OID .1.3.6.1.2.1.2.2.1.2  | Format-Table Data -AutoSize -HideTableHeaders | Out-File C:\NameP.ps1xml 

Get-Content C:\NameP.ps1xml  | Where { $_ -ne ""  } | Set-Content C:\NameP1.ps1xml 
$Ports = @(Get-Content C:\NameP1.ps1xml | Where-Object { $_.StartsWith("Unit") } )
$NumberOfports = @(Get-Content C:\NameP1.ps1xml | Where-Object { $_.StartsWith("Unit") } ).Count

Write-Output ----------------------------------------
Write-Output "Name ports: "$Ports  
Write-Output ----------------------------------------
Write-Output "Number of ports:  $NumberOfports"

这是针对交换机的一个 IP 地址,我需要通过一个具有不同 IP 地址的产品的多个交换机,例如 192.168.16.1、192.168.17.1 突然用一个带有 IP 列表或类似内容的脚本.

我的输出:

 CommandType     Name                                               Version    Source     
-----------     ----                                               -------    ------     
Function        Get-SnmpData                                       1.0.0.1    SNMP       
Function        Invoke-SnmpWalk                                    1.0.0.1    SNMP       
Function        Set-SnmpData                                       1.0.0.1    SNMP       
----------------------------------------
----------------------------------------
Selected IP is: 192.168.15.1
----------------------------------------
----------------------------------------
Name of ports: 
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 1                   
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 2                   
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 3                   
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 4                   
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 5                   
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 6                   
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 7                   
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 8                   
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 9                   
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 10                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 11                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 12                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 13                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 14                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 15                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 16                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 17                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 18                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 19                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 20                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 21                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 22                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 23                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 24                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 25                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 26                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 27                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 28                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 29                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 30                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 31                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 32                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 33                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 34                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 35                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 36                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 37                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 38                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 39                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 40                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 41                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 42                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 43                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 44                  
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 45 - no sfp inserted
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 46 - no sfp inserted
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 47 - no sfp inserted
Unit: 1 1000BASE-T RJ45 Gigabit Ethernet Frontpanel Port 48 - no sfp inserted
----------------------------------------
Number of ports:  48

我想要更多 IP 的输出。您有任何可以通过多个 IP 地址的简单解决方案吗? 然后我仍然使用这些数据。 谢谢你的每一个想法。

  1. 提供多个 IP 作为 "list",
  2. 简单地将重复的代码封装在一个 Foreach 循环中。

因此:

Install-Module SNMP
Get-Command -Module SNMP
Write-Output ----------------------------------------
$IP = "192.168.15.1","192.168.17.1" # Add more to the list
Write-Output ----------------------------------------
Write-Output "Selected IP is: $IP"
Write-Output ----------------------------------------

$IP | % {
    Invoke-SnmpWalk -IP $_ -Community public -OID .1.3.6.1.2.1.2.2.1.2  | Format- 
    Table Data -AutoSize -HideTableHeaders | Out-File C:\NameP.ps1xml 

    Get-Content C:\NameP.ps1xml  | Where { $_ -ne ""  } | Set-Content 
    C:\NameP1.ps1xml 
    $Ports = @(Get-Content C:\NameP1.ps1xml | Where-Object { $_.StartsWith("Unit") } 
    )
    $NumberOfports = @(Get-Content C:\NameP1.ps1xml | Where-Object { 
    $_.StartsWith("Unit") } ).Count

    Write-Output ----------------------------------------
    Write-Output "Name ports: "$Ports  
    Write-Output ----------------------------------------
    Write-Output "Number of ports:  $NumberOfports"

} # End of ForEach