微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么每次迭代后我的 foreach 变量不会在 PowerShell 中输出到我的输出?

如何解决为什么每次迭代后我的 foreach 变量不会在 PowerShell 中输出到我的输出?

我有一个 DHCP 脚本,用于在 DHCP 服务器的所有范围内查找匹配的主机名

  • 我首先获取所有 DHCP 服务器并导入一个 .txt 的主机名
$list = Get-Content C:\script\HostNameList.txt #Defines content it pulls as list 
$DHServers = Get-DhcpserverInDC #gives variable name for loop

 # Gets all DHCP servers ands scopes 
    foreach ($Server in $DHServers){
        $scopes = Get-DHcpserverv4Scope -ComputerName $Server.dnsname #get all scopes
    }
  • 我遍历主机名和范围列表以寻找匹配项。这里的某个地方是我的问题
$Output = foreach ($hostname in $list) { #Calls each item in list a hostname and sends to output
    if (test-connection -count 1 -computername $hostname -quiet) #With 1 ping,check if hostname is online
    {   
        foreach ($scope in $scopes){ 
            if($scope | Get-DhcpserverV4Lease -ComputerName $server.dnsname | Where-Object HostName -like "$hostName*" ) #compares the hostname to lease to find which scope it is in
            { $scope.name } #return scope it found hostname in
        }
        [PSCustomObject]@{ #Rename varibles in data pull for output file
        Asset = $hostname
        Location = $scope.name #only want the name of the scope
        Status = "Online"
        }
    }   

    else #statement if hostname is not online
    { 
        Write-host "$hostname Is offline,only Last Location is kNown. $hostname was added to the output file." -BackgroundColor DarkRed
        [PSCustomObject]@{
        Asset = $hostname
        Location = $scope.name #only want the name of the scope,since the name = Location
        Status = "Offline"
        }
    }
}
$Output #show output in powershell
$Output | Export-Csv -Path C:\script\Asset_Result.csv -NoTypeinformation #outputs .csv
  • 这就是它正在做的事情,输出重复了 DHCP 作用域列表中的最后一项。
Asset    Location         Status
-----     --------         ------
A847    Public Internet      Online
A261    Public Internet      Offline
A201    Public Internet      Online
  • 这是它应该做的
Asset    Location         Status
-----     --------         ------
A847        FLoor 1         Online
A261      West 1st FL       Offline
A201        Floor 3         Online

我怎样才能在我的 $scope.name 语句在每次迭代后转到我的 PSCustomObject?

解决方法

这个:

foreach ($Server in $DHServers){
  $scopes = Get-DHCPServerv4Scope -ComputerName $Server.dnsname #get all scopes
}

是 - 在净效果上 - 相当于:

$scopes = Get-DHCPServerv4Scope -ComputerName $DHServers[-1].dnsname #get all scopes

也就是说,您不断地重新分配给循环体中的同一个变量 ($scopes),替换 之前的值,这样您最终只能得到 last 循环迭代,用于存储在 $DHServers 中的 last 服务器,即 $DHServers[-1]


最好的解决方案是依靠 PowerShell 使用 foreach 等语句的能力作为表达式,其输出 - 甚至循环的迭代输出 - 会自动收集在 {{1 }} PowerShell 的数组(具有两个或多个输出):

[object[]]

注意:# Collect the output from *all* Get-DHCPServerv4Scope calls. [array] $scopes = foreach ($Server in $DHServers) { Get-DHCPServerv4Scope -ComputerName $Server.dnsname #get all scopes } 类型约束(与:[array] 相同)仅在可能只有 一个 输出对象并且您希望确保收集的输出总是一个数组。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。