Dezirdt Uzurnaim
Dezirdt Uzurnaim

Reputation: 21

Multiple commands in Powershell script with Export

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.

Upvotes: 1

Views: 2199

Answers (3)

iRon
iRon

Reputation: 23830

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.

Upvotes: 0

Prasoon Karunan V
Prasoon Karunan V

Reputation: 3063

  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

Upvotes: 1

No Refunds No Returns
No Refunds No Returns

Reputation: 8346

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

Upvotes: 0

Related Questions