powershell - 为什么我的 foreach 变量在每次迭代后没有进入 PowerShell 中的输出?

标签 powershell foreach scope iteration dhcp

我有 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 if($scope | ... 语句在每次迭代后转到我的 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 之类的语句作为表达式,其输出(甚至是循环的迭代输出)会自动收集在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] 类型约束(与:[object[]] 相同)仅当情况下只有 时才需要一个输出对象,并且您希望确保收集的输出始终是一个数组。

关于powershell - 为什么我的 foreach 变量在每次迭代后没有进入 PowerShell 中的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66988468/

相关文章:

powershell - 获取 PowerShell 循环中当前项目的索引

PowerShell 和 ActiveDirectory 模块 - 查找不是特定组成员的用户

java - for-each 数组操作

java - 如何限制子类修改抽象类中方法的范围?

c# - Scope_Identity 不能在 c# 上使用 GUID

powershell - 如何使用 PowerShell 脚本禁用所有 BizTalk 主机实例

powershell - powershell中的.replace和-replace有什么区别?

javascript - 如何在 JSX 代码中打印数组的所有元素(作为父状态的 props 传递)?

php - 我的foreach只找到数组的最后一个元素

python:从外部代码导入 numpy 作为 np 在我自己的用户定义模块中丢失