Mein Skript überschreibt den vorherigen Eintrag nach jeder "foreach" -Schleife

329
David Prentice

Das Skript, das ich hier habe, schreibt die gewünschten Informationen aus. Ich habe jedoch festgestellt, dass jeder neue gültige Eintrag den letzten ersetzt. Ich kann nicht herausfinden warum. Am Ende habe ich also nur einen Eintrag, auch wenn 1000 Computer erkannt wurden. Bitte hilf mir dabei, das herauszufinden.

$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))  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=" GB" -f ((Get-WmiObject Win32_PhysicalMemory -ComputerName $computer |Measure-Object Capacity -Sum).Sum / 1GB) $x = gwmi win32_computersystem -ComputerName $computer |select @ Else {'Desktop Or Other something else.'}}},Manufacturer,@ 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 } } 
0
Was hast du versucht? Es sieht so aus, als würden Sie dem Array keine Elemente hinzufügen. Twisty Impersonator vor 6 Jahren 0

1 Antwort auf die Frage

1
SimonS

Ihr Problem war, dass die Datei in jeder Schleife überschrieben wurde. Jetzt speichert Ihr Skript zunächst alles in einer Variablen und exportiert es am Ende.

ein paar weitere probleme mit ihrem script:

  • Der Try / Catch wird im Moment nie etwas fangen, da kein Abbruchfehler vorliegt.
  • Das Format Ihres Skripts war furchtbar - bitte formatieren Sie es besser, sonst ist es sehr schwer zu lesen.
  • Warum wählen Sie am Ende alles aus, bevor Sie es exportieren? Wenn Sie alles exportieren möchten, müssen Sie nichts auswählen. Wenn es sich um Sortierzwecke handelt, ändern Sie New-Object PSObject -propertyauf [pscustomobject]. Dadurch wird bereits die korrekte Sortierreihenfolge erzwungen

Hier ist das aktualisierte Skript

$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 = " GB" -f ((Get-WmiObject Win32_PhysicalMemory -ComputerName $computer | Measure-Object Capacity -Sum).Sum / 1GB) $x = gwmi win32_computersystem -ComputerName $computer | select @ Else {'Desktop Or Other something else.'}}},Manufacturer,@ 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 
Das ist großartig und ich freue mich, dass Sie mir den Optimierungstipp für das Organisieren geben! Ich versuche herauszufinden, wie man sowohl Offline-Computer "$ computer offline nicht erreichen" einschließt, als auch Online-Computer in der Suche nach einer vollständigeren Liste. Ich denke ich würde das mit Variablen machen, aber nicht sicher ... David Prentice vor 6 Jahren 0
@DavidPrentice Die gesamte Ausgabe in `foreach () {}` wird derzeit in der Variablen $ outtbl` gespeichert. Sie können damit ein bisschen herumspielen. B. dasselbe "PSCustomObject" erstellen, wenn ein Computer offline oder online ist. SimonS vor 6 Jahren 0