powershell - Powershell 中的缩短功能

标签 powershell

当我开始发出大面积的 ping 请求时,我首先开始在 Excel 表中“写下”所有 IP 地址,以生成 csv 输出以将其导入 powershell。

然后我考虑让 Powershell 中的这些事情变得更简单,工作量更少。

$range = 1..254

$ips1 = ForEach-Object -Process {'10.10.1.' + $Range}
$ips2 = ForEach-Object -Process {'10.10.2.' + $Range}
$ips3 = ForEach-Object -Process {'10.10.3.' + $Range}
$ips4 = ForEach-Object -Process {'10.10.4.' + $Range}
$ips5 = ForEach-Object -Process {'10.10.5.' + $Range}
$ips6 = ForEach-Object -Process {'10.10.6.' + $Range}
$ips7 = ForEach-Object -Process {'10.10.7.' + $Range}
$ips8 = ForEach-Object -Process {'10.10.8.' + $Range}
$ips9 = ForEach-Object -Process {'10.10.9.' + $Range}
$ips10 = ForEach-Object -Process {'10.10.10.' + $Range}
$ips11 = ForEach-Object -Process {'10.10.11.' + $Range}
$ips12 = ForEach-Object -Process {'10.10.12.' + $Range}
$ips13 = ForEach-Object -Process {'10.10.13.' + $Range}
$ips14 = ForEach-Object -Process {'10.10.14.' + $Range}
$ips15 = ForEach-Object -Process {'10.10.15.' + $Range}
$ips16 = ForEach-Object -Process {'10.10.16.' + $Range}
$ips17 = ForEach-Object -Process {'10.10.17.' + $Range}
$ips18 = ForEach-Object -Process {'10.10.18.' + $Range}
$ips19 = ForEach-Object -Process {'10.10.19.' + $Range}
$ips20 = ForEach-Object -Process {'10.10.20.' + $Range}
$ips21 = ForEach-Object -Process {'10.10.21.' + $Range}
$ips22 = ForEach-Object -Process {'10.10.22.' + $Range}
$ips23 = ForEach-Object -Process {'10.10.23.' + $Range}
$ips24 = ForEach-Object -Process {'10.10.24.' + $Range}
$ips25 = ForEach-Object -Process {'10.10.25.' + $Range}
$ips26 = ForEach-Object -Process {'10.10.26.' + $Range}
$ips27 = ForEach-Object -Process {'10.10.27.' + $Range}
$ips28 = ForEach-Object -Process {'10.10.28.' + $Range}
$ips29 = ForEach-Object -Process {'10.10.29.' + $Range}

所以我到了这一点,但现在我被困住了如何才能使它更短而没有错误,所以之后我有一个大变量,它将所有 ips 存储在一个中

最佳答案

这是一个较短的解决方案,其结果与您的代码相同:

ForEach ($Network in 1..29) {
    $IPAddresses = ForEach ($IP in 1..254) {
        "10.10.$Network.$IP"
    }

    New-Variable -Name IPs$Network -Value $IPAddresses
}

要获得一个包含所有 IP 的变量:

$AllIPs = ForEach ($Network in 1..29) {
    ForEach ($IP in 1..254) {
        "10.10.$Network.$IP"
    }   
}

关于powershell - Powershell 中的缩短功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55258088/

相关文章:

powershell - PowerShell变量无法按预期工作

java - PowerShell 术语无法识别

powershell - 如何在运行时通过脚本检查 PowerShell 中是否存在 cmdlet

powershell - 如何在powershell中的多行之间进行选择?

xml - 如何使用 PowerShell 根据 XSD 验证 XML 文件?

PowerShell--使用 IP 地址和子网掩码计算网络 ID

azure - 无法在 PowerShell 中安装 Azure 模块

powershell - Azure Powershell 选择带有订阅 ID 的 AzureSubscription

powershell - 将警报规则添加到 Azure 云服务时出错

powershell - 为什么 Powershell 环境路径与系统环境路径不同?