简体   繁体   English

PowerShell多个输入IP地址

[英]PowerShell Multiple Input IP Addresses

I have an issue with multiple input in this code: 我在此代码中有多个输入问题:

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"

This is for one IP address of switch and a I need to go through multiple switches of one production with different IP addresses for example 192.168.16.1, 192.168.17.1 suddenly with one script with list of IP or something like that. 这是针对一个交换机的IP地址的,我需要使用一个带有IP列表或类似内容的脚本,突然通过一个具有不同IP地址的生产的多个交换机,例如192.168.16.1、192.168.17.1。

My output: 我的输出:

 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

I want this output for more IPs. 我想要更多IP的输出。 Do you have any simple solution that would go through multiple IP addresses? 您有可以通过多个IP地址访问的简单解决方案吗? Then I still work with these data. 然后,我仍然使用这些数据。 Thanks for every idea. 感谢您的每一个想法。

  1. Provide the multiple IP's as a "list", 提供多个IP作为“列表”,
  2. Simply encapsulate the repeated code within a Foreach loop. 只需将重复的代码封装在Foreach循环中。

Thus: 从而:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 并行ping多个IP地址并输出到Powershell中的多个.TXT文件 - Ping multiple ip addresses in parallel and output to multiple .TXT files in Powershell 如何搜索多个文本文件的IP地址,并使用PowerShell输出找到文本文件的IP地址? - how to search multiple text files for IP addresses and output the IP addresses found to a text file using powershell? 如何扫描多个主机名以解析 IP 地址 Powershell 脚本 - How to Scan multiple Hostnames to resolve Ip addresses Powershell script 枚举所有具有 IP 和 MAC 地址的网卡 powershell - Enumerate all NICs with IP and MAC addresses in powershell Powershell - 只显示向下 IP 个地址 - Powershell - Show only down IP addresses PowerShell - 删除 IP - 带有 Remove-NetIPAddress 的地址 - PowerShell - Removing IP - Addresses with Remove-NetIPAddress 在 Powershell 中使用 nslookup 获取主机名的 IP 地址 - Getting IP addresses for hostnames using nslookup in Powershell Powershell 脚本未在 CSV 文件中显示 IP 地址 - Powershell script not displaying IP addresses in CSV file 用前导 0 填充 IP 地址 - powershell - Pad IP addresses with leading 0's - powershell 是否有任何 Powershell 脚本,或者如果虚拟机有多个 ip 地址,我如何修改此脚本以将多个 ip 作为 csv 文件导入? - Is there any Powershell script or how can i modify this script to import multiple ips as a csv file if a vm has multiple ip addresses?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM