Data not exporting to CSV

I know this is going to be stupid and i'm kicking myself for not seeing why this is not working. But in my ELSE statement when i pipe that to a .csv i am getting blank lines.

It outputs to the console fine just nothing in the .csv file.

Thoughts ?


Function get-mdtbuiltcomputers {
param (
    [string[]]$computername='localhost'
)

    foreach ($comp in $computername) { 
    $path=Test-Path "\\$comp\c$\Windows\smsts.ini"
    $connected = Test-Connection -quiet -count 2 -delay 2 -ComputerName $comp
        if ($path -and $connected) {
                $file=Get-ChildItem \\$comp\c$\Windows\smsts.ini
                $Output = [ordered]@{'Computername'=$comp;
                                     'MDT Image Date'=$file.creationtime
                                    }
            $obj = New-Object -TypeName psobject -Property $Output
            Write-Output $obj
            }
        else
            {
            Write-Output "$comp is unavailable or not a Standard MDT Image"
            }
     }

February 12th, 2015 5:54pm

The CSV is expecting the same column name, change the else to something like this to still use the same object setup:

  $Output = [ordered]@{
     'Computername'=$comp;
     'MDT Image Date'="unavailable or not a Standard MDT Image"}
  $obj = New-Object -TypeName psobject -Property $Output
  Write-Output $obj

  • Proposed as answer by Mike Laughlin Thursday, February 12, 2015 6:32 PM
Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 6:50pm

FYI:

Here is a good way to guarantee consistent object generation.

Function get-mdtbuiltcomputers {
    param (
        [string[]]$computername='localhost'
    )

    foreach($comp in $computername){
        $p=[ordered]@{
            Computername=$comp
            Status='Unavailable or not a Standard MDT Image'
            MDTImageDate=$null
        }
        $path=Test-Path "\\$comp\c$\Windows\smsts.ini"
        $connected = Test-Connection -quiet -count 2 -delay 2 -ComputerName $comp
        if($path -and $connected){
            $file=Get-ChildItem \\$comp\c$\Windows\smsts.ini
            $p.Status='Available'
            $p.MDTImageDate=$file.CreationTime
         }
         New-Object PsObject -Property $p
     }
}
Avoid adding spaces in Property names.
February 12th, 2015 7:08pm

Perfect ! thank you so much guys. I knew it would be a simple thing.

One more question and this may not be the right forum for this but it's related to this report im trying to run.

I'm trying to find a way through the output if for instance a machine is NOT AVAILABLE currently but i run the report again i would like the computername to update with AVAILABLE the next time the report is run.

I'm assuming if i use the -append option it will create a duplicate line item and not update the existing computer name.

Thoughts ?

Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 8:36pm

That is a new and unrelated issue.  Please start a new topic with a complete explanation and someone will answer.
February 12th, 2015 8:52pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics