Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 用于从计算机列表获取外部IP地址的Powershell脚本_Loops_Powershell_Ip - Fatal编程技术网

Loops 用于从计算机列表获取外部IP地址的Powershell脚本

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

我一直在尝试使用此脚本获取网络上多台计算机的外部IP地址。到目前为止,脚本似乎在循环中迭代,但在本地机器上运行命令,而不是在循环中的远程机器上运行命令

$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