Export csv

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of 37mm 37mm 1 year, 4 months ago.

  • Author
    Posts
  • #31916
    Profile photo of 37mm
    37mm
    Participant

    I am trying to get this to export to a csv and append to make a inventory. Thanks again for your time. Cheers!

    $system = New-Object -TypeName psobject
    $cpu = Get-WmiObject win32_processor
    $os = Get-WmiObject win32_operatingsystem
    $disk = Get-WmiObject win32_logicaldisk
    $memory = Get-WmiObject win32_physicalmemory
    
    $system = [PSCustomObject]@{
        OperatingSystem = $os.Caption
        hostname = $os.PSComputerName
        LastReformat = $os.installdate
        OSarch = $os.OSArchitecture
        CPU = $cpu.name
        HDsize = ($disk.size|ForEach-Object {$_/ 1GB})
        HDfreeSpace = ($disk.freespace | ForEach-Object {$_/1GB})
        RAMtotal = ($memory.capacity | ForEach-Object {$_/1GB})
        RAMspeed = $memory.configuredclockspeed 
    }
    
    
    
    
    $today = Get-Date
    $cutoffdate = $today.AddDays(-30)
    
    $que = Get-ADComputer  -Properties * -Filter {LastLogonDate -gt $cutoffdate}|Select -Expand cn  
    
    foreach ($q in $que)
    {
      Invoke-Command -ScriptBlock {$system} -ComputerName $q 
    }
    
  • #31917
    Profile photo of 37mm
    37mm
    Participant

    Invoke-Command -ScriptBlock {$system} -ComputerName $q | export-csv -append -notypeinformation

    Gives me errors

    Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
    At line:29 char:60
    + ... erName $q | Export-Csv \\nas\it\james\inventory.csv -Append -NoTypeIn ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
    
  • #31923
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    That's because $system is a PowerShell Custom Object, not a script block.

  • #31924
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Use get-help on get-wmiobject. You do not need to use invoke-command at all. get-wmiobject has the ability natively to get data from remote systems.

    Also you do not need:
    $system = New-Object -TypeName psobject

  • #31925
    Profile photo of 37mm
    37mm
    Participant

    Thanks, been going down a google rabbit hole on this. Looking into it now.
    Cheers

  • #31926
    Profile photo of 37mm
    37mm
    Participant
    $today = Get-Date
    $cutoffdate = $today.AddDays(-30)
    
    $que = Get-ADComputer  -Properties * -Filter {LastLogonDate -gt $cutoffdate}|Select -Expand cn  
    
    foreach ($q in $que)
    {
        $cpu = Get-WmiObject -ComputerName $q  win32_processor
        $os = Get-WmiObject -ComputerName $q  win32_operatingsystem
        $disk = Get-WmiObject -ComputerName $q  win32_logicaldisk
        $memory = Get-WmiObject -ComputerName $q win32_physicalmemory
        $system = [PSCustomObject]@{
        OperatingSystem = $os.Caption 
        hostname = $os.PSComputerName
        LastReformat = $os.installdate
        OSarch = $os.OSArchitecture
        CPU = $cpu.name
        HDsize = ($disk.size|ForEach-Object {$_/ 1GB})
        HDfreeSpace = ($disk.freespace | ForEach-Object {$_/1GB})
        RAMtotal = ($memory.capacity | ForEach-Object {$_/1GB})
        RAMspeed = $memory.configuredclockspeed 
    }
        $system | export-csv C:\inventory2.csv -Append -NoTypeInformation
    }
    

You must be logged in to reply to this topic.