Несколько входных IP-адресов PowerShell - PullRequest
0 голосов
/ 14 сентября 2018

У меня проблема с множественным вводом в этом коде:

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 Ответ

0 голосов
/ 14 сентября 2018
  1. Предоставьте несколько IP-адресов в виде "списка",
  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
...