gpt4 book ai didi

powershell - 将多个成员添加到Powershell哈希表

转载 作者:行者123 更新时间:2023-12-03 00:35:34 25 4
gpt4 key购买 nike

我有一个包含子网信息的CSV文件,将用于填充其中包含服务器信息的CSV文件。我首先导入子网信息,然后在处理它时,我试图将多个成员添加到初始哈希表中,但是行为不符合预期。
以下代码按预期方式处理第一项,并使用正确的信息创建一个新列。该代码表明它至少处理了另外两个部分,但未添加成员。如何更改代码以允许在单个数组中创建多个成员?目标是使每个子网的网关字段(列)对于该子网都是唯一的。

五个变量(variableA-E)的目的是模仿真实代码中发生的事情。实际代码从哈希表运行比较,但这不是必需的。我愿意根据需要更改该部分。

CSV file contents:
NetworkName,Subnet,VLANID,Gateway,VLAN
Servers,"192.168.1.0/24","2041","192.168.1.1","ServerVLAN-2041"
Workstations,"192.168.2.0/24","1001","192.168.2.1","WorkstationVLAN-1001"
DMZ,"172.16.0.0/28","340","172.16.0.1","DMZVLAN-340"
Servers,"192.168.3.0/24","2043","192.168.3.1","ServerVLAN-2043"
Workstations,"192.168.4.0/24","1004","192.168.4.1","WorkstationVLAN-1004"
DMZ,,,,

码:
$csvfile = "C:\temp\testfile.csv"
$hashArray = Import-CSV $csvfile

$variableA = "192.168.1.0"
$variableB = "192.168.2.0"
$variableC = "192.168.3.0"
$variableD = "172.16.0.1"
$variableE = "192.168.5.0"

$hashArray | % {
if ($_.subnet) { $variable = ($_.subnet).split("/")[0] }
Else { $variable = $null }
if ($variable -eq $variableA -and $variable -ne $null)
{
$_ | add-member "ServerGW1" -NotePropertyValue $_.gateway
Write-Host "Added Server gateway 1: "$_.gateway -ForegroundColor Yellow
}

if ($variable -eq $variableC -and $variable -ne $null)
{
$_ | add-member "ServerGW2" -NotePropertyValue $_.gateway
Write-Host "Added Server gateway 2: "$_.gateway -ForegroundColor Yellow
}

if ($variable -eq $variableB -and $variable -ne $null)
{
$_ | add-member "WorkstationGW1" -NotePropertyValue $_.gateway
Write-Host "Added Workstation gateway 1: "$_.gateway -ForegroundColor Yellow
}

if ($variable -eq $variableD -and $variable -ne $null)
{
$_ | add-member "DMZGW1" -NotePropertyValue $_.gateway
Write-Host "Added DMZ gateway 1: "$_.gateway -ForegroundColor Yellow
}

if ($variable -eq $variableE -and $variable -ne $null)
{
$_ | add-member "WorkstationGW2" -NotePropertyValue $_.gateway
Write-Host "Added Workstation gateway 2: "$_.gateway -ForegroundColor Yellow
}
}
$hashArray | Out-GridView

Out-GridView输出:

OutGrid Results

控制台输出:

Console output

预期产量:
Expected Output

最佳答案

Out-GridView使用第一个对象的属性来呈现列。缺少所有新列(ServerGW1除外),因为它们未在$ hashArray的第一个对象中初始化。您可以使用$ null值初始化所有行的所有属性,或提供一个属性列表以供选择,然后将结果输出到Out-GridView

$hashArray | Select-Object NetworkName,Subnet,VLANID,Gateway,VLAN, ServerGW1, ServerGW2,WorkstationGW1,WorkstationGW2,DMZGW1  | Out-GridView

初始化所有属性:
$hashArray | % {
$variable =if ($_.subnet) { ($_.subnet).split("/")[0] }Else { $null }
$_ | add-member "ServerGW1" -NotePropertyValue $(if ($variable -eq $variableA){ $_.gateway}Else { $null })
$_ | add-member "ServerGW2" -NotePropertyValue $(if ($variable -eq $variableC){ $_.gateway}Else { $null })
$_ | add-member "WorkstationGW1" -NotePropertyValue $(if($variable -eq $variableB){ $_.gateway}Else { $null })
$_ | add-member "DMZGW1" -NotePropertyValue $(if ($variable -eq $variableD ){ $_.gateway}Else { $null })
$_ | add-member "WorkstationGW2" -NotePropertyValue $(if ($variable -eq $variableE){ $_.gateway}Else { $null })
}
$hashArray | Out-GridView

关于powershell - 将多个成员添加到Powershell哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41131209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com