Solved

Powershell:  New-Object add-member for multiple attributes shows no value

Posted on 2011-02-23
5
1,244 Views
Last Modified: 2012-06-27
Hello Experts,

I have the following Function in powershell to get complete disk information.


function get-diskinfo  {
param($servername)

if (($servername -eq "localhost") -or ($servername -eq $hostname)) {
    $ldiskobj = Get-WmiObject Win32_logicaldisk -computername $servername |
    Where-Object {$_.DriveType -eq "3"} | 
    Select-Object @{Name="ServerName";Expression={"{0:N1}" -f($_.__SERVER)}},
        @{Name="Drive";Expression={"{0:N1}" -f($_.DeviceID)}}, 
        @{Name="Size";Expression={"{0:N1}" -f($_.Size/1gb)}}, 
        @{Name="Freespace";Expression={"{0:N1}" -f($_.FreeSpace/1gb)}} 
    $phdskobj = Get-WmiObject Win32_DiskDrive -computername $servername |
    Select-Object Model,
    Bytespersector,
    InterfaceType,
    Manufacturer,
    Status,
    TotalHeads,
    TotalCylinders

}

else {
    $ldiskobj = Get-WmiObject Win32_logicaldisk -computername $servername -Credential $logincred |
    Where-Object {$_.DriveType -eq "3"} | 
    Select-Object @{Name="ServerName";Expression={"{0:N1}" -f($_.__SERVER)}},
    @{Name="Drive";Expression={"{0:N1}" -f($_.DeviceID)}}, 
    @{Name="Size";Expression={"{0:N1}" -f($_.Size/1gb)}}, 
    @{Name="Freespace";Expression={"{0:N1}" -f($_.FreeSpace/1gb)}}
    
    $phdskobj = Get-WmiObject Win32_DiskDrive -computername $servername -Credential $logincred |
    Select-Object Model,
    Bytespersector,
    InterfaceType,
    Manufacturer,
    Status,
    TotalHeads,
    TotalCylinders

}

$dskobj = New-Object psobject
$dskobj | Add-Member NoteProperty "Model" -Value $phdskobj.Model
$dskobj | Add-Member NoteProperty "Status" -Value $phdskobj.status
$dskobj | Add-Member NoteProperty "Drive" -Value $ldiskobj .Drive
$dskobj | Add-Member NoteProperty "Freespace" -Value $ldiskobj .Freespace
cls

$dskobj |fl

} 

Open in new window


The Issue I am facing is when I have only one Hard drive and one partition , the script execute successfully with valid return.

However When a Server is having more than one physical disk or more than one partition it is returning no value for $dskobj properties. Well I understand why it is doing it , because it doesn't know how to present it. However I can't think of any way to present this data.

0
Comment
Question by:comebui
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
Learnctx earned 500 total points
ID: 34966386
Because you're returning an array, you'll need to approach it a little bit differently. I haven't put the credential info back in, just adapt this to your needs and see if it works :)

Usage: Get-DiskInfo -target computername
Function Get-DiskInfo
{
    param([string]$target)
    if (!$target){$target = "."}
    $arrDiskInfo = @()
    GWMI -ComputerName $target Win32_DiskDrive | % {
        $objDiskInfo = New-Object PSObject
        $objDiskInfo | Add-Member -Type NoteProperty -Name "Model" -Value $_.Model
        $objDiskInfo | Add-Member -Type NoteProperty -Name "Status" -Value $_.Status
        GWMI -ComputerName $target -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($_.DeviceID)'} WHERE ResultClass=Win32_DiskPartition" | % {
            GWMI -ComputerName $target -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($_.DeviceID)'} WHERE ResultClass=Win32_LogicalDisk" | % {
                $objDiskInfo | Add-Member -Type NoteProperty -Name "Drive" -Value $_.DeviceID
                $objDiskInfo | Add-Member -Type NoteProperty -Name "Freespace" -Value $("{0:N1}" -f($_.Freespace/1GB))
            }
        }
        $arrDiskInfo += $objDiskInfo
    }
    $arrDiskInfo | FL *
}

Open in new window

0
 
LVL 1

Author Comment

by:comebui
ID: 34969178
I have added the credential part and it is working as I wanted. Thanks for your help!!
Now few silly questions to annoy you.
1. What is %{}  ?  - You piped output of win32_diskdrive into this container is it same as an array @()

Few operation inside the %{} is bit difficult for a newbie like me understand. I understand the Associators of query , however why aren't you doing the same operation for $_.Model  as you are doing for $_.deviceid..

Thing is I am writing more functions like this and I can only use wmi queries for data from the servers as none of they will have powershell. So it would be great to know how this is working so that I keep building my shared function scripts


0
 
LVL 17

Expert Comment

by:Learnctx
ID: 34976682
% is a bad habit :) I forgot to replace that with the actual cmdlet when copying it over.
It is a shortcut for the ForEach-Object cmdlet. For example, these will do the same thing.
Get-Process | Foreach-Object {Blah}
Get-Process | % {Blah}

Open in new window

? is a shortcut for Where-Object, so if you see people piping to ? that's what that is all about.

The reason you don't need to do this to find the model and deviceID, is that there is only 1 reference to each physical device so for each drive in the target host you can assume the model and status will be the same for each partition on that drive.
0
 
LVL 17

Expert Comment

by:Learnctx
ID: 34976688
Also for some more info on Associators and all that have a look here: http://www.codeproject.com/KB/system/WQLByExample.aspx
0
 
LVL 1

Author Comment

by:comebui
ID: 34977254
Brilliant answers all my Questions!!!!
One thing now I have to figure out , How to add a new property (like : win32_timezone.caption ) to the existing psobject ($objdiskinfo)
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
powershell and sql server - alerting 7 77
Powershell Script to list Permissions 7 34
install .NET 3.5 on windows 10 13 45
powershell to append file extension from a CSV 3 22
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now