Moin,
ich habe hier eine etwas längere aber auch für mich eine etwas aufwendigere Abfrage geschrieben.
Die Daten funktionieren soweit, wie er sie mir ausgibt, aber er geht die Schleifen die ich unten eingegeben habe (Fett markierte Zeilen) jeweils nur einmal durch.
Woran liegt das?
Habe ich einen offensichtlichen Fehler gemacht beim Erstellen der Schleifen?
function Get-ADUserLastLogon([string]$userName)
{
$dcs = Get-ADDomainController -Filter {Name -like "*"}
#alle domaincontroller abfragen um die letzte anmeldung aller AD-User zu rthalten
foreach($dc in $dcs)
{
$hostname = $dc.HostName
$user = Get-ADUser $userName -Server $hostname -Properties lastLogon
$lngexpires = $user.lastLogon
if (-not ($lngexpires)) {$lngexpires = 0 }
If (($lngexpires -eq 0) -or ($lngexpires -gt [DateTime]::MaxValue.Ticks))
{
$LastLogon = "<Never>"
}
Else
{
$Date = [DateTime]$lngexpires
$LastLogon = $Date.AddYears(1600).ToLocalTime()
}
}
#alle AD-User abfragen und die gewünschten Properties auslesen und in eine csv Datei exportieren
}
cls
$excel = New-Object -ComObject excel.application
$excel.visible = $True
$workbook = $excel.Workbooks.Add()
#Genügend Mappen erzeugen
$excel.DisplayAlerts = $False
$sheet= $workbook.Worksheets.Item(1)
cls
#Role-HHLA-VMware-Administratoren
#Tabelle benennen
$sheet.Name = 'Administratoren'
#Formatierung der Überschriften
$sheet.Cells.Item(1,1).Font.Size = 10
$sheet.Cells.Item(1,1).Font.Italic=$True
$sheet.Cells.Item(1,2).Font.Size = 10
$sheet.Cells.Item(1,2).Font.Italic=$True
$sheet.Cells.Item(1,3).Font.Size = 10
$sheet.Cells.Item(1,3).Font.Italic=$True
$sheet.Cells.Item(1,4).Font.Size = 10
$sheet.Cells.Item(1,4).Font.Italic=$True
$sheet.Cells.Item(1,5).Font.Size = 10
$sheet.Cells.Item(1,5).Font.Italic=$True
$sheet.Cells.Item(1,6).Font.Size = 10
$sheet.Cells.Item(1,6).Font.Italic=$True
$sheet.Cells.Item(1,7).Font.Size = 10
$sheet.Cells.Item(1,7).Font.Italic=$True
#Tabellenüberschriften setzen
$sheet.Cells.Item(1,1) = 'Nachname'
$sheet.Cells.Item(1,2) = 'Vorname'
$sheet.Cells.Item(1,3) = 'Anzeigename'
$sheet.Cells.Item(1,4) = 'Letztes Anmeldedatum'
$sheet.Cells.Item(1,5) = 'Kontoablaufdatum'
$sheet.Cells.Item(1,6) = 'Abteilung'
$sheet.Cells.Item(1,7) = 'Kontoverzeichnis'
cls
Write-Host "+++ Auswertung von Role-HHLA-VMware-Administratoren wird erstellt..." -ForegroundColor Cyan
$row = 2
#In welche Spalte sollen die Infos eingefügt werden
$column = 1
foreach($samname in $infos){
$sheet.Cells.Item($row,$column) = $infos.samaccountname
$column++
#Increment to next Row and reset Column
$row++
$column = 1
}
$row = 2
#In welche Spalte sollen die Infos eingefügt werden
$column = 2
foreach($givenname in $infos){
$sheet.Cells.Item($row,$column) = $infos.GivenName
$column++
#Increment to next Row and reset Column
$row++
$column = 2
}
$row = 2
#In welche Spalte sollen die Infos eingefügt werden
$column = 3
foreach($name in $infos){
$sheet.Cells.Item($row,$column) = $infos.Name
$column++
#Increment to next Row and reset Column
$row++
$column = 3
}
$row = 2
#In welche Spalte sollen die Infos eingefügt werden
$column = 4
foreach($Lastlogon in $infos){
$sheet.Cells.Item($row,$column) = $infos.LastLogon
$column++
#Increment to next Row and reset Column
$row++
$column = 4
}
$row = 2
#In welche Spalte sollen die Infos eingefügt werden
$column = 5
foreach($accexpiration in $infos){
$sheet.Cells.Item($row,$column) = $infos.accountExpirationDate
$column++
#Increment to next Row and reset Column
$row++
$column = 5
}
$row = 2
#In welche Spalte sollen die Infos eingefügt werden
$column = 6
foreach($department in $infos){
$sheet.Cells.Item($row,$column) = $infos.Department
$column++
#Increment to next Row and reset Column
$row++
$column = 6
}
$row = 2
#In welche Spalte sollen die Infos eingefügt werden
$column = 7
foreach($distinguishedName in $infos){
$sheet.Cells.Item($row,$column) = $infos.DistinguishedName
$column++
#Increment to next Row and reset Column
$row++
$column = 7
}
$alleADUser = Get-ADUser -Filter {enabled -eq $true} | sort
foreach($user in $alleADUser)
{
Get-ADUserLastLogon $user.samaccountname
$infos = Get-ADUser $user.samaccountname -Properties *
$datum = $LastLogon
$samname = $infos.samaccountname
$givenname = $infos.GivenName
$name = $infos.Name
$accexpiration = $infos.accountExpirationDate
$distinguishedName = $infos.distinguishedName
$department = $infos.department
}