When using get-wmiobject with and psobject being created and exporting to a csv. If object 1 has 4 headers but object 2 has 8 headers due to the iteration of the information. You will only see the 4 headers in the export csv.
I am only using the disk section in this post since that is the only one I am having an issue with right now.
If you use this against 15 servers or computers. Lets say the first computer you go against in a list and export to CSV has 2 drives. Though the second computer has 5 drives. You will never see beyond 2 drives in the CSV. It appears that the first iteration of the array object sets the header. If you use the variable $donkeypages after running the function. You can still see the full information of each machine to compare against.
Example command when loaded into your profile.
info .\listofcomputers.txt disk 1
This should provide you with a Info-List.csv of all the computers in the list provided with the disks they have. Though when testing make sure your first machine has the lowest amount of drives and second machine has more. To reproduce the problem.
I am looking to be able to fix this problem. Kind of hitting my head on the wall with this one. I would like to see all drives. No matter the order. Or if I can count the iterations and before hand and make that the first entry to set the headers. Or dynamically set the headers. I have even looked at creating a CSV per server/computer and then combining them all.
Here is the example of the script.
function Info {
Param (
[Parameter(Position=1,Mandatory=$TRUE,HelpMessage="Supply a list of servers or computers or a single computer/server")]
$cn,
[Parameter(Position=2,Mandatory=$FALSE,HelpMessage="Variables proc=Processor, mem=Memory, sys=System, time=LocalSystemTime, nic=Network Card, hba=HBAInfo, user=UserInfo, disk=Disk, freedisk=Free Disk Space, 3=PAE Switch, bios=Bios, os=Operating System, =ALL")]
$actn,
[Parameter(Position=3,Mandatory=$FALSE,HelpMessage="CSV 1 or 0. This will export the information into a csv if this is set to 1.")]
$csv
)
PROCESS {
if (($cn -like "*.csv") -or ($cn -like "*.txt")) {
$cn = Get-Content $cn
} else {
$cn = $cn
}
$donkeypages = @()
foreach ($c in $cn) {
if (Test-Connection -Cn $c -BufferSize 16 -Count 1 -ea 0 -quiet) {
$obj = new-Object PSObject
$obj | Add-Member NoteProperty Computername $c
write-host "$c is alive" -f green -b black
switch ($actn) {
"disk" {
try {
Get-WmiObject win32_diskdrive -cn $c -ErrorAction stop > $null
$DISKDRIVE = Get-WmiObject win32_diskdrive -cn $c | Select-Object model,interfacetype,partitions
FOR($i=0;$i -lt $DISKDRIVE.length; $i++) {
$obj | Add-Member NoteProperty "$i DiskModel" ($diskdrive[$i].model)
$obj | Add-Member NoteProperty "$i DiskInterfaceType" ($diskdrive[$i].interfacetype)
$obj | Add-Member NoteProperty "$i DiskPartitions" ($diskdrive[$i].partitions)
}
}
catch {
$Exception = $_
switch ($Exception){
{($Exception.Exception.Message -like "*Access is denied*")} {Write-Warning "Access denied to $env:username"; $obj | Add-Member NoteProperty "0 DiskModel"("Failed to access server with account $env:username")}
{($Exception.Exception.Message -like "*The RPC server is unavailable*")} {Write-Warning "System RPC connection not accessible"; $obj | Add-Member NoteProperty "0 DiskModel"("RPC Failure")}
}
}
try {
Get-WmiObject win32_volume -cn $c -ErrorAction stop > $null
$VOLUME = Get-WmiObject win32_volume -cn $c | Select-Object SerialNumber,Label,Name,@{Label='Capacity';EXPRESSION={"{0:N2}" -f ($_.capacity/1GB)}},@{Label='GBFreeSpace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}}
FOR($i=0;$i -lt $VOLUME.length; $i++) {
$obj | Add-Member NoteProperty "$i DiskSerialNumber" ($volume[$i].serialnumber)
$obj | Add-Member NoteProperty "$i DiskLabel" ($volume[$i].label)
$obj | Add-Member NoteProperty "$i DiskName" ($volume[$i].name)
$obj | Add-Member NoteProperty "$i DiskCapacity" ($volume[$i].capacity)
$obj | Add-Member NoteProperty "$i DiskFreeSpace" ($volume[$i].gbfreespace)
}
}
catch {
$Exception = $_
switch ($Exception) {
{($Exception.Exception.Message -like "*Invalid class*")} {Write-Warning "System does not contain the Win32_Volume class"; $obj | Add-Member NoteProperty "0 DiskSerialNumber" ("Cannot find class win32_volume")}
{($Exception.Exception.Message -like "*The RPC server is unavailable*")} {Write-Warning "System RPC connection not accessible"; $obj | Add-Member NoteProperty "0 DiskSerialNumber"("RPC Failure")}
{($Exception.Exception.Message -like "*Access is denied*")} {Write-Warning "Access denied to $env:username"; $obj | Add-Member NoteProperty "0 DiskSerialNumber"("Failed to access server with account $env:username")}
}
}
}
if ($_.systemstartupoptions) {
$obj | ForEach-Object {$_.systemstartupoptions = [string]$_.systemstartupoptions}
}
if (!$csv) {
Write-Output $obj | Format-List *
$donkeypages += $obj
} else {
$donkeypages += $obj
}
} Else {
write-warning "$c is not alive"
}
}
if ($csv) {
$donkeypages | Export-Csv -notype ".\Info-List.csv"
} else {
$global:donkeypages = $donkeypages
}
}
}