我的腳本在每個“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

相關內容