Link to home
Start Free TrialLog in
Avatar of techdrive
techdriveFlag for United States of America

asked on

Creating a new object

Sorry about the question if it seems trivial but how does one know what commands work together when creating a new object. For example get-mailbox and get-user works together but if I use get-mailboxstatistics or get-mailboxfolderstatistics I have to do something different. How are you able to tell before you run the command. Another example case and point. If I add get-mailboxstatistics below like I have done it will not run and say something about null value.

$mailboxes = get-mailbox -organizationalunit "OU name"
$mailboxes | %{
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Database $_.Database
$obj | Add-Member NoteProperty Title (Get-mailboxstatistics $_.totalitemsize).Title
$obj | Add-Member NoteProperty Dept (Get-User $_.itemcount).Department
Write-Output $obj
}

Open in new window

Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

The key here is if the cmdlets and the parameters "Accepts Pipeline input" from other cmdlets.

If you run get-help get-mailbox -full

You will see under each parameter Accepts pipeline input? True or False

Depending on the cmdlet and also the parameter you use determines this.

Get-mailbox -Identity (does accept pipeline input) however get-mailbox -Anr or -Filter (does not accept pipeline input).

Will.
I don't believe this is really a question about which cmdlets work together.
But it's not really clear to me what you're asking.  In your example I don't know which part of the code you're talking about.

Creating a new object is not dependent on other cmdlets in any way.  It seems more likely that you are encountering some error (or unexpected results) with the commands
(Get-mailboxstatistics $_.totalitemsize).Title
(Get-User $_.itemcount).Department

which is causing a problem with the Add-Member command.
Avatar of techdrive

ASKER

I was clearly wrong..I took a look at my code and corrected this issue

$mailboxes = get-mailbox -organizationalunit ""
$mailboxes | %{
$obj = "" | Select-Object Name,Database,totalitemsize,itemcount
$obj.Name = $_.Name
$obj.Database = $_.Database
$obj.totalitemsize = (Get-mailboxstatistics $_.Name).totalitemsize
$obj.itemcount = (Get-mailboxstatistics $_.Name).itemcount
Write-Output $obj
}
I also meant to say thanks footech for pointing this out.
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial