37mm
1
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
}
37mm
2
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
That’s because $system is a PowerShell Custom Object, not a script block.
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
37mm
5
Thanks, been going down a google rabbit hole on this. Looking into it now.
Cheers
37mm
6
$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
}