1

I've been trying to work through this for quite some time. My ultimate goal is to get the exported report as a single csv sheet. However, I've been highly unsuccessful. I then broke it down to export 2 sheets that I can just merge, however, CIM is not playing nice with that at all. Then my other issue came with not calling from my list properly.

$ComputerList = "C:\ps_test\pclastlogon.txt"
$LogPath = "C:\ps_test\Logs"
$LogTime = Get-Date -Format s | foreach {$_ -replace ":", "-"}
$CsvLogonPath = $LogPath+'\RebootStatus-'+$LogTime+'-Logon.csv'
$CsvBootPath = $LogPath+'\RebootStatus-'+$LogTime+'-LastBoot.csv'

Import-Module ActiveDirectory

IF ( -Not (Test-Path -Path $LogPath)) {New-Item -Path $LogPath -ItemType Directory}

$Computers = Get-Content $ComputerList

Foreach ($Computers in $ComputerList) {
Get-ADComputer -Identity $Computers -Properties * -Filter * | Select-Object cn,LastLogonDate,@{LABEL="Last Logon";EXPRESSION={[DateTime]::FromFileTime($_.LastLogon)}} | Export-Csv -Path $CsvLogonPath
}

Foreach ($Computers in $ComputerList) {
Get-CimInstance Win32_OperatingSystem -ComputerName $Computers | Select csname,LastBootUpTime | Export-Csv -Path $CsvBootPath
}

Can someone please point me in the right direction? Thanks in advance.

  • 1
    I recommend looking at the `Foreach-Object` cmdlet instead of the foreach statement. – Eris Oct 24 '17 at 21:56

3 Answers3

1
  1. Not to use -filter * -Properties *, its too expensive. Mention the required Properties in -Properties and if you are mentioning -Identity, -filter * is not necessarily required.

  2. Wrap Get-ADComputer and Get-CimInstance in a single foreach and create a CustomObject then export to CSV.

[Not Tested] Fore example:

$AllDetails = Foreach ($Computers in $ComputerList) {
          $DetailsfromAD = Get-ADComputer -Identity $Computers -Properties cn,LastLogonDate,LastLogon | Select-Object cn,LastLogonDate,@{LABEL="Last Logon";EXPRESSION={[DateTime]::FromFileTime($_.LastLogon)}}
          $DetailsFromCIM = Get-CimInstance Win32_OperatingSystem -ComputerName $Computers | Select csname,LastBootUpTime 

          $PropertyHash = @{
                           CN               = $DetailsfromAD.CN
                           LastLogonDate    = $DetailsfromAD.LastLogonDate
                          'Last Logon'      = $DetailsfromAD.'Last Logon'
                           csname           = $DetailsFromCIM.csname
                           LastBootUpTime  = $DetailsFromCIM.LastBootUpTime
           }
           New-Object -TypeName PSObject -Property $PropertyHash
}

Export $AllDetails to a CSV file

Prasoon Karunan V
  • 2,916
  • 2
  • 12
  • 26
  • This is exactly the solution I was going to come down and write. Just create a PSCustomObject with the results of both commands in each loop that gets rolled up into the final object for export to CSV. – LeeM Oct 25 '17 at 08:22
  • Here's what my export results return IsReadOnly IsFixedSize IsSynchronized Keys Values SyncRoot Count FALSE FALSE FALSE System.Collections.Hashtable+KeyCollection System.Collections.Hashtable+ValueCollection System.Object 5 – Dezirdt Uzurnaim Oct 25 '17 at 17:15
  • @DezirdtUzurnaim a small correction added (New-Object) – Prasoon Karunan V Oct 26 '17 at 03:30
0

Just a guess here but after piping I think you need to for-each your command list. Something like

Get-ADComputer -Identity $Computers -Properties * -Filter * | % { Select-Object cn,LastLogonDate,@{LABEL="Last Logon";EXPRESSION={[DateTime]::FromFileTime($_.LastLogon)}} | Export-Csv -Path $CsvLogonPath }

But then you will need to do something to append each result instead of just having the last one in $CSvLogonPath

No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43
0

A general approach to join two object lists that result from a single source list:

Add the computer name ($Computers) from the original computer list ($ComputerList) as a primary key in both object lists using @{Label="ComputerName"; Expression={$Computers}}:

$ADComputers = Foreach ($Computers in $ComputerList) {
    Get-ADComputer -Identity $Computers -Properties * -Filter * | Select-Object @{Label="ComputerName"; Expression={$Computers}},cn,LastLogonDate,@{LABEL="Last Logon";EXPRESSION={[DateTime]::FromFileTime($_.LastLogon)}}
}

$CimInstances = Foreach ($Computers in $ComputerList) {
    Get-CimInstance Win32_OperatingSystem -ComputerName $Computers | Select @{Label="ComputerName"; Expression={$Computers}},csname,LastBootUpTime
}

Use the Join-Object function to join the object lists on the ComputerName:

$ADComputers | Join $CimInstances -On ComputerName | Export-Csv -Path $CsvBootPath

You might consider to go easy on this and forget about the primary key and just join the two tables based on the their index:

$ADComputers | Join $CimInstances -Using {$LeftIndex -eq $RightIndex}

But I recommend against this because if one of your tables is missing a record (e.g. because it doesn't exist the database), the indexes will likely be incorrect aligned.

iRon
  • 20,463
  • 10
  • 53
  • 79