我对PowerShell非常陌生,我怀疑因为我不是程序员,所以我的问题的原因可能是您对大多数人非常明显的原因。基本上我的脚本错误与Powershell重新使用哈希表foreach循环
“不能索引到一个空数组。
循环收集静态配置的多宿主服务器中每个网络适配器的属性。我的调试语句显示foreach,如果按预期工作 - 但似乎我不能在foreach循环中重用哈希表。这里的正确方法是什么? (注意:这是400行脚本的一部分,它主要集中在哈希表中,因此如果可能的话我需要使用哈希表)。
# hugely truncated.
$this = @() # initialise results table as an array.
$ip = "myserver.mydomain.com"
foreach ($connected_nic in ($Adapter = Get-WmiObject -computer $ip win32_networkadapter -filter "NetConnectionStatus='2'" | where {$_.PNPDeviceID -notmatch "1394"})) { # Find electrically connected adapters, which are not firewire.
Write-host -ForegroundColor Green $Connected_nic
if ($cfg=($dns=(Get-WmiObject -ComputerName $ip Win32_NetworkAdapterConfiguration -filter "Index = '$($connected_nic.Index)'")).IPaddress -like "192.168.*") { # test each connected adapter to see if it's IP = 192.168.X.X
$ips = $dns | select -expandproperty Ipaddress
$dns # check return is as expected.
$results = [ordered] @{
'Netbios Name' = $dns.DNSHostname
'IPv4 Address' = $ips[0] #cope with ipv6
'Subnet Mask' = [String]$dns.IpSubnet
'Default Gateway' = [String]$dns.DefaultIPGateway
'Primary DNS Server' = $dns.DNSServerSearchOrder[0]
'Secondary DNS Server' = $dns.DNSServerSearchOrder[1]
'MAC Address' = $dns.MACaddress
}
$dnsout = New-Object PSObject -Property $results # Create a new row for the report from our hash table.
$this += $dnsout # Add this row to the main report.
} # Configurations of interest test.
} # End connected adapter test
#output
Netbios Name : server
IPv4 Address : 192.168.228.54
Subnet Mask : 255.255.255.0
Default Gateway : 192.168.228.5
Primary DNS Server : 192.168.228.51
Secondary DNS Server : 192.168.224.51
MAC Address : 00:23:7D:22:1C:52
Netbios Name : server
IPv4 Address : 192.168.228.54
Subnet Mask : 255.255.255.0
Default Gateway : 192.168.228.5
Primary DNS Server : 192.168.228.51
Secondary DNS Server : 192.168.224.51
MAC Address : 00:23:7D:22:1C:52
Netbios Name : server
IPv4 Address : 192.168.228.54
Subnet Mask : 255.255.255.0
Default Gateway : 192.168.228.5
Primary DNS Server : 192.168.228.51
Secondary DNS Server : 192.168.224.51
MAC Address : 00:23:7D:22:1C:52
Connected adapter configurations
DHCPEnabled : False
IPAddress : {192.168.228.54}
DefaultIPGateway : {192.168.228.5}
DNSDomain :
ServiceName : l2nd
Description : HP NC373i Multifunction Gigabit Server Adapter #2
Index : 2
DHCPEnabled : False
IPAddress : {192.168.1.1}
DefaultIPGateway :
DNSDomain :
ServiceName : VMnetAdapter
Description : VMware Virtual Ethernet Adapter for VMnet1
Index : 9
DHCPEnabled : False
IPAddress : {192.168.18.1}
DefaultIPGateway :
DNSDomain :
ServiceName : VMnetAdapter
Description : VMware Virtual Ethernet Adapter for VMnet8
Index : 10
编辑以增加详细信息。 在该示例中,计算机有三个有效的网络适配器,它们将返回三组配置结果。每组结果都将特定于该网络适配器。当我在代码中启用调试日志记录时,我可以看到foreach和if语句正在按照我的要求执行其逻辑并返回三组唯一结果。我的问题是捕获输出不能按预期工作。正在发生的事情是,我最终得到了三组完全相同的结果。现在,你可能会试图说某些东西因此是空的,不能覆盖第一组结果 - 但是 - 这是不正确的。我肯定会得到三组不同的结果,但是当我尝试散列表时,只有第一组结果成功显示。下一次我尝试添加更多结果AKA覆盖 - 它失败,无法索引到一个空数组中。只需复制我的代码,并运行它与任何计算机与多个适配器是活跃的,它会barf。我想知道如何使它不成功,并成功覆盖散列表中的第一组结果。就是那个问题。
是的,你是完全正确的 - 谢谢你对我的耐心。 我现在明白了这个问题到底是什么 - 我的测试主题碰巧是一个糟糕的选择。代码实际上很好。非常感谢!
-ea默默继续#完美的作品。
:
您可以通过只是试图获得一个不存在的数组的索引中看到了吗?每次循环循环时,都会覆盖变量$ results,这是您唯一建立哈希表的地方。 – 2014-10-03 13:23:04
我强烈建议您不要尝试内嵌所有内容,因为它会使调试不可靠。如果你想在foreach循环中访问除当前$ connected_nic以外的散列表的任何成员,那么你需要将散列表存储在一个你可以在循环中引用的变量中。 FWIW在你的this()数组变量中缓存结果是一个powershell反模式。你应该把你的新对象放在流水线上,让format-table或者format-list或者export-csv来处理剩下的事情。 – 2014-10-03 14:22:48
感谢您关注此问题 - 您能详细阐述一下如何将我的结果放入管道中,并让format-xxxx像您说的那样小心吗? – Anthony1256 2014-10-03 18:03:03