Loops 用于从计算机列表获取外部IP地址的Powershell脚本
我一直在尝试使用此脚本获取网络上多台计算机的外部IP地址。到目前为止,脚本似乎在循环中迭代,但在本地机器上运行命令,而不是在循环中的远程机器上运行命令Loops 用于从计算机列表获取外部IP地址的Powershell脚本,loops,powershell,ip,Loops,Powershell,Ip,我一直在尝试使用此脚本获取网络上多台计算机的外部IP地址。到目前为止,脚本似乎在循环中迭代,但在本地机器上运行命令,而不是在循环中的远程机器上运行命令 $computers = get-content "c:\scripts\scriptdev\Addresses.txt" $outfile ="c:\scripts\scriptdev\test2.csv" $results = @() foreach ($computer in $computers) { Invoke-RestMethod
$computers = get-content "c:\scripts\scriptdev\Addresses.txt"
$outfile ="c:\scripts\scriptdev\test2.csv"
$results = @()
foreach ($computer in $computers)
{
Invoke-RestMethod http://ipinfo.io/json | Select -exp ip $computer
Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name $computer
}
您需要使用cmdlet或参数来指定远程执行
$computer
本身只是一个带有字符串值的变量
有些cmdlet支持-ComputerName$computer
参数,而Invoke RestMethod
等cmdlet则要求您使用Invoke命令或类似命令运行它们
前
太不清楚了。您从未“要求”任何cmdlet远程执行<代码>$results
和$outfile
从不使用。您输入了$computer
作为Select Object
的列名,这将使它生成具有自己属性(而不仅仅是值)的对象…我网络上多台计算机的外部IP地址-除非您确定该地址仅在您的网络中配置,计算机没有明确的单一外部IP地址。可能存在不止一个外部IP地址的原因有很多——多个WAN连接、不同的策略(取决于登录者或一天中的时间)、不同协议的不同路由、不同传入服务(HTTPS、SSH)的多个IP……谢谢,这非常有帮助。您知道如何删除由此生成的runspaceid区域吗?$results |选择对象名称,ExternalIP
记住,如果这解决了您的问题,请将其标记为答案,以便将问题标记为已关闭。
$computers = get-content "c:\scripts\scriptdev\Addresses.txt"
$results = @()
foreach ($computer in $computers)
{
$results += Invoke-Command -HideComputerName -ComputerName $computer -ScriptBlock {
New-Object -TypeName psobject -Property @{
Name = Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name
ExternalIP = Invoke-RestMethod http://ipinfo.io/json | Select -ExpandProperty ip
}
}
}
$results