psobject

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5420

    by maindave at 2013-01-09 14:36:51

    Hi, I'm trying to display properties from a global group using the following, the variables contain the data but something with the psobject is not right. any help would be appreicated.

    Function get-groupmem {

    Param (
    $group=(Read-Host "Enter Group Name")
    )

    #Write-Host "$group"

    $groupprop=Get-ADGroupMember $group

    $obj = New-Object -typename psobject

    $obj | Add-Member -membertype noteproperty `
    -name "Class" -value ($groupprop.name)

    Write-Output $obj
    }

    get-groupmem

    ****This is what is displayed******

    Class
    —–

    by DonJ at 2013-01-09 14:53:49

    So, you can use the Code or PowerShell button to the toolbar to keep your code formatted nicely.

    First, let's rewrite this a bit. Using Read-Host that way is a hack, and I don't use Add-Member in this scenario because it gets to be a bit much to read and type. And Write-Host kills puppies. The following will accomplish all of the same things, but in a way more consistent with native PowerShell commands.


    function Get-GroupMem {
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$True)][string]$group
    )
    Write-Verbose "Group is $group"
    $groupprop = Get-ADGroupMember $group
    $prop = @{'Class'=$groupprop.name}
    New-Object -Type PSObject -Prop $prop
    }
    Get-GroupMem 'whatever' -verbose

    Now, there's nothing wrong with the PSObject. It's fine.

    First, when you called Get-GroupMem in your example, you didn't pass in a group name. So no group would be retrieved. I would expect no output in that case.

    Second, Get-ADGroupMember returns one object for each member of the group. That means a group with multiple members will put multiple objects into $groupprop. You can't just refer to a property name of a collection – you have to enumerate through the collection.


    $groupprop = Get-ADGroupMember $group
    foreach ($item in $groupprop) {
    $prop = @{'Class'=$item.Name}
    New-Object -Type PSObject -Prop $prop
    }

    Might be more what you're after. I'm not sure – you didn't state what your actual goal was, so all I can do is look for potential problems in the way you've coded your function. What I've suggested would put the name of each group member into the "Class" column in your output.

    by DonJ at 2013-01-09 14:56:27

    By the way, if your goal is to display properties of the group itself, you're using the wrong command. Let me know if that's what you're trying to do.

    by maindave at 2013-01-10 07:15:14

    Don, thks for the quick reply. I've been reading your PS in a Month of lunches and following the examples in Chap 19, which explains
    the use of $obj = New-Object -typename psobject.
    So, what I'm trying to accomplish is to have the function prompt to enter a group name, then use get-adgroupmember to display all members,
    then use get-aduser to display firstname,lastname etc. so, I was just trying to keep the code simple at this point. I'm also not sure how to handle groups that are members of groups. maybe an IF statement?

    Thks again
    Ps you book is great and has help me a great deal!

    by nohandle at 2013-01-10 07:59:43

    [quote="maindave"]So, what I'm trying to accomplish is to have the function prompt to enter a group name, then use get-adgroupmember to display all members,
    then use get-aduser to display firstname,lastname etc. [/quote]
    Just get the name and call the Get-ADGroupMember then use select to select the properties you want.

    If you need to rename the properties you can use the syntax described {url=http]here[/url] in the second point. The $_ is the current object in the pipeline.

You must be logged in to reply to this topic.