我的脚本在每次“foreach”循环后都会覆盖前一个条目

我的脚本在每次“foreach”循环后都会覆盖前一个条目

我这里的脚本写出了我想要的信息。但是我注意到每个新的有效条目都会替换最后一个。我不知道为什么。所以即使检测到 1000 台计算机,我每次得到的也只是 1 个条目。请帮助我解决这个问题。

$computers = Get-ADComputer -SearchBase "DC=some,DC=web,DC=com" -Filter * | Select-Object -ExpandProperty Name
      foreach ($computer in $computers){
if(!(Test-Connection -CN $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
     {write-host "Cannot reach $computer offline." -f red}
      else {
$outtbl = @()
Try{
$sr=Get-WmiObject -Class Win32_BIOS -ComputerName $computer  -ErrorAction Continue 
$Xr=Get-WmiObject -Class Win32_Processor -ComputerName $computer -ErrorAction Continue   
$ld=Get-ADComputer $computer -Properties * -ErrorAction Continue
$r="{0} GB" -f ((Get-WmiObject Win32_PhysicalMemory -ComputerName $computer |Measure-Object Capacity  -Sum).Sum / 1GB)
$x = gwmi win32_computersystem -ComputerName $computer |select @{Name = "Type";Expression = {if (($_.pcsystemtype -eq '2')  ) 

{'Laptop'} Else {'Desktop Or Other something else.'}}},Manufacturer,@{Name = "Model";Expression = {if (($_.model -eq "$null")  ) {'Virtual'} Else {$_.model}}},username -ErrorAction Continue
$t= New-Object PSObject -Property @{
    ServiceTag = $sr.serialnumber
    ComputerName = $ld.name
    IPV4Address=$ld.ipv4Address
    Enabled=$ld.Enabled
    Description=$ld.description
    OU=$ld.DistinguishedName.split(',')[1].split('=')[1] 
    Type = $x.type
    Manufacturer=$x.Manufacturer
    Model=$x.Model
    RAM=$R
    ProcessorName=($xr.name | Out-String).Trim()
    NumberOfCores=($xr.NumberOfCores | Out-String).Trim()
    NumberOfLogicalProcessors=($xr.NumberOfLogicalProcessors | Out-String).Trim()
    AddressWidth=($xr.Addresswidth | Out-String).Trim()
    OperatingSystem=$ld.operatingsystem
    OperatingSystemServicePack=$ld.OperatingSystemServicePack
    OperatingSystemVersion=$ld.OperatingSystemVersion
    OperatingSystemHotfix=$ld.OperatingSystemHotfix
    LastLogonDate=$ld.lastlogondate
    ObjectCreated=$ld.Created
    ObjectModified=$ld.whenChanged
    LoggedInUser=$x.username
    }
    $outtbl += $t
    }
    catch [Exception]
    {
        "Error communicating with $computer, skipping to next"
    }
   $outtbl | select Computername,ServiceTag,IPV4Address,Description,Enabled,OU,Type,Manufacturer,Model,RAM,ProcessorName,NumberOfCores,NumberOfLogicalProcessors,AddressWidth,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion,OperatingSystemHotfix,ObjectCreated,ObjectModified,LoggedInUser,LastLogonDate | Format-Table * -Wrap -AutoSize | Out-String -Width 4096 | Out-File $env:USERPROFILE\Desktop\AD-Inventory.txt
}
}

答案1

你的问题是,文件在每次循环中都被覆盖。现在你的脚本将首先将所有内容保存到一个变量中,然后在最后将其导出。

您的脚本还存在一些问题:

  • try/catch 目前永远不会捕获任何东西,因为没有终止错误。
  • 你的脚本的格式太糟糕了 - 请将其格式化得更好,否则很难阅读。
  • 为什么在导出之前要选择所有内容?如果要导出所有内容,则无需选择内容。如果是为了排序,请更改New-Object PSObject -property[pscustomobject],这将强制正确的排序顺序

这是更新后的脚本

$computers = Get-ADComputer -SearchBase "DC=some,DC=web,DC=com" -Filter * | Select-Object -ExpandProperty Name
$outtbl = foreach ($computer in $computers){
    if(!(Test-Connection -CN $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
    { 
        write-host "Cannot reach $computer offline." -f red 
    }
    else {
        Try{
            $sr = Get-WmiObject -Class Win32_BIOS -ComputerName $computer  -ErrorAction Continue 
            $Xr = Get-WmiObject -Class Win32_Processor -ComputerName $computer -ErrorAction Continue   
            $ld = Get-ADComputer $computer -Properties * -ErrorAction Continue
            $r  = "{0} GB" -f ((Get-WmiObject Win32_PhysicalMemory -ComputerName $computer | Measure-Object Capacity  -Sum).Sum / 1GB)
            $x  = gwmi win32_computersystem -ComputerName $computer | select @{Name = "Type";Expression = {if (($_.pcsystemtype -eq '2')  ) 
                  {'Laptop'} Else {'Desktop Or Other something else.'}}},Manufacturer,@{Name = "Model";Expression = {if (($_.model -eq "$null")  ) {'Virtual'} Else {$_.model}}},username -ErrorAction Continue
            New-Object PSObject -Property @{
                ServiceTag = $sr.serialnumber
                ComputerName = $ld.name
                IPV4Address=$ld.ipv4Address
                Enabled=$ld.Enabled
                Description=$ld.description
                OU=$ld.DistinguishedName.split(',')[1].split('=')[1] 
                Type = $x.type
                Manufacturer=$x.Manufacturer
                Model=$x.Model
                RAM=$R
                ProcessorName=($xr.name | Out-String).Trim()
                NumberOfCores=($xr.NumberOfCores | Out-String).Trim()
                NumberOfLogicalProcessors=($xr.NumberOfLogicalProcessors | Out-String).Trim()
                AddressWidth=($xr.Addresswidth | Out-String).Trim()
                OperatingSystem=$ld.operatingsystem
                OperatingSystemServicePack=$ld.OperatingSystemServicePack
                OperatingSystemVersion=$ld.OperatingSystemVersion
                OperatingSystemHotfix=$ld.OperatingSystemHotfix
                LastLogonDate=$ld.lastlogondate
                ObjectCreated=$ld.Created
                ObjectModified=$ld.whenChanged
                LoggedInUser=$x.username
            }
        }
        catch [Exception]
        {
            "Error communicating with $computer, skipping to next"
        }
    }
}

$outtbl | select Computername,ServiceTag,IPV4Address,Description,Enabled,OU,Type,Manufacturer,Model,RAM,ProcessorName,NumberOfCores,NumberOfLogicalProcessors,AddressWidth,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion,OperatingSystemHotfix,ObjectCreated,ObjectModified,LoggedInUser,LastLogonDate | Format-Table * -Wrap -AutoSize | Out-String -Width 4096 | Out-File $env:USERPROFILE\Desktop\AD-Inventory.txt

相关内容