Search code examples
powershellpowershell-2.0powershell-4.0powershell-remoting

Get-ADDomain | Select Name


I am currently making a script which is supposed to go into a server, grabbing the domain name and the Users in a specific group, then go out and continue on the next server. Well, i got the connection phase done and now I need the users and the domain name. Here is a snippet of code:

foreach ($User in (Get-ADGroupMember fjärrskrivbordsanvändare | Select Name)) 
{
    $UserArray += $User.name
    $DomainArray = Get-ADDomain | Select Name 

    $DomainArray
    $UserArray
}

As you can see I add the Names of the users in an Array, then I want to save the NAME of the domain in an array and then print them both to see if its ok. But when i print the domains, the name does show up ALONG with the ip to the server and some code i dont know where it comes from. It looks like this

cstein    (IP) 82ddgfhdghjdfw-sdfgsbbc-42sfdsdf4-a8dfgd-734ddeee345817

Does anyone know a fix to this? I only want "cstein" when i do Select Name.

I scrambled all the numbers and letters in the long "code" or whatever it is. I don't know if it's sensitive information.

Keep in mind that you are talkig to a Swedish 15 year old when you write an answer.


Solution

  • I would create a PSCustomObjectcontaining a User and a Domain property:

    $result = Get-ADGroupMember fjärrskrivbordsanvändare | Foreach {
        [PSCustomObject]@{
                User = $_.Name
                Domain = Get-ADDomain | Select Name 
            }   
    }
    

    Now you can retrieve all user names using:

    $result | select User
    

    Note: You probably don't want to call Get-ADDomain each time within your foreach...