Jose Ortega
Jose Ortega

Reputation: 1040

Custom Objects to CSV PowerShell

  #Function to get the computerlist: Name,OS,IPv4, IPv6,DiskInfo
 function Get-ComputerListnDiskInfo{
 [CmdletBinding()]
  param(
  [Parameter(ValueFromPipeline=$True)] [string[]]$ComputerName 
  )
  BEGIN {
    Import-Module ActiveDirectory -Cmdlet Get-ADComputer -ErrorAction SilentlyContinue
  }
  PROCESS {

    try{
        $computerinfo = Get-ADComputer -Filter * -Properties OperatingSystem
        #Information about Name,Ipv4,IPv6,Device,VolumeName,Free,Busy,Size,Pfree,Pbusy for ALL COMPUTERS container
        $AllComputerInfo = @()
        foreach ($comp in $computerinfo){
            #Testing if computers is ON LINE
            $TestCon = Tester $comp.name
            $test = $TestCon.BooleanV
            if($test) {
                #write-output "$Test"
                $PhysicalDisks = Get-WMIObject -computername $comp.name -query "SELECT * from win32_logicaldisk where DriveType = 3" | Select Deviceid,VolumeName,FreeSpace,Size
                $Target = @()
                #Create the Object foreach disk and append in the Target Variable
                $GetOPNHealthStatus = Get-PhysicalDisk | select FriendlyName,OperationalStatus,HealthStatus

                Write-Output "$PhysicalDisk.count"

                #write-output $GetOPNHealthStatus.OperationalStatus
                $i=0
                foreach ($disk in $physicalDisks){

                    #Get all Items: size,free,busy,pfree and pbusy disk space info (can add a number at the end to set decimals)
                    $Size=FormatNSetSizeFreeSpace $disk.Size
                    $Free=FormatNSetSizeFreeSpace $disk.FreeSpace
                    $Busy=FormatNSetBusySpace $disk.Size $disk.FreeSpace
                    $Pfree=PercentFreeBusy $Free $size
                    $PBusy=PercentFreeBusy $Busy $size

                    #Create a new Object using all the info
                    $result =New-Object PSObject -Property @{
                        Device=$disk.DeviceID
                        VolumeName=$disk.VolumeName
                        Size=$Size
                        Free=$Free
                        Busy=$Busy
                        Pfree = $PFree
                        PBusy = $PBusy
                        OPStatus = $GetOPNHealthStatus.OperationalStatus[$i]
                        HStatus  = $GetOPNHealthStatus.HealthStatus[$i]
                    }

                    $i++
                    #add this info to the target array
                    $Target+= $result
                }


                    #Add all info into new object
                    $allIComnDiskInfo=New-Object PSObject -Property @{
                        Name = $comp.Name
                        OS = $comp.OperatingSystem
                        IPV4 =  $TestCon.IPv4
                        IPV6 =  $TestCon.IPv6
                        disksInfo = $Target
                    }
                #and Fill any just add this info to the $Allcomputer info (just online computer's)
                $AllComputerInfo+= $allIComnDiskInfo
            }   
        }
        return $AllComputerInfo
    }
    Catch{
        Write-Warning $_.Exception.Message
    }
  }
}

$test = Get-ComputerListnDiskInfo

running $test

$test = Get-ComputerListnDiskInfo

$test

disksInfo : {@{PBusy=8,148; VolumeName=; Busy=10,306; Pfree=91,853; Free=116,178; Device=C:; Size=126,483; OPStatus=O; HStatus=H}}
Name      : DC2012
OS        : Windows Server 2012 R2 Standard
IPV4      : 192.168.1.251
IPV6      : fe80::cd63:76bf:3d2b:340f%12

And running

$test | Export-Csv here.csv

I got this:

#TYPE System.String
"Length"
"6"

Why is happening this?

Why I don't get all this info?

And how should I search the info contained in the "diskInfo" variable

I tried to pass this $test variable to another function to format it and It seem not to work:

Thank you in advance for the answers

Upvotes: 1

Views: 3855

Answers (2)

Jose Ortega
Jose Ortega

Reputation: 1040

Basically the issue is this one: I have an system.object[] in the output while using CSV.

object or similar output when using export-csv

Upvotes: 0

TheMadTechnician
TheMadTechnician

Reputation: 36277

To start out with, you aren't just outputting a custom object, or an array of custom objects. But that's not the first problem I see. The first problem I see is that you have this big function that has a parameter, and then you do this:

$test = Get-ComputerListnDiskInfo

So you call that function with no arguments, so it has no computer to run it against. Some of the parts of the function will probably default to the local computer, but will they all? I don't know, maybe.

So what does $test actually contain? An array. Of what? Well, the first thing that the function outputs is a string:

Write-Output "$PhysicalDisk.count"

So the first item in your array is a string. Then you build a bunch of custom objects and arrays, and what not, and you Return those. Great, the next item in your $test array is a custom object. But $test is not an array of custom objects, or a single custom object, it is an array with a variety of things within it.

That is why Export-CSV will not work.

Upvotes: 1

Related Questions