Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how can i display the values that Get-ADDomainController asnwers in a foreach ?

Posted on 2014-12-02
12
Medium Priority
?
717 Views
Last Modified: 2014-12-02
Hello experts,

i'm trying to get values out of the response of Get-ADDomainController but i dont succeed inside a foreach.

what am I doing wrong, i only receive three times the domain but no ip nor name.

here's what i wrote so far.

foreach($server in (Get-ADForest).globalcatalogs){
    foreach-object { 'server : {0, 20}  ---  IP : {0, 20}  ---  Name: {0, 20}' -f
     $(Get-ADDomainController -Server $server).Domain ,
     $(Get-ADDomainController -Server $server).IPv4Address,
     $(Get-ADDomainController -Server $server).Name
    }
}

thank you in advance.

toshi.
0
Comment
Question by:toshi_
  • 7
  • 5
12 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 40476407
Try this - only executes get-addomaincontroller once, instead of thrice:
foreach($server in (Get-ADForest).globalcatalogs){
  get-addomaincontroller -server $server | select domain, ipv4address, name
}

Open in new window

0
 

Author Comment

by:toshi_
ID: 40476415
;)

i'm begining with powershell, i'll keep your solution in mind ;)

thanks a lot.
0
 

Author Comment

by:toshi_
ID: 40476417
what about string controlling as i was trying to do ?
i'd like to have the responses more close one to each other.
0
WatchGuard Case Study: NCR

With business operations for thousands of customers largely depending on the internal systems they support, NCR can’t afford to waste time or money on security products that are anything less than exceptional. That’s why they chose WatchGuard.

 

Author Comment

by:toshi_
ID: 40476424
i'm trying to display with string format  but i dont knwo where to it ?


i'm having error with the following line

foreach($server in (Get-ADForest).globalcatalogs){
  get-addomaincontroller -server $server | select '{0. 20}, -- {1, 20}, {2, 20} ' -f  domain, ipv4address, name
}
0
 

Author Comment

by:toshi_
ID: 40476430
not in such way either....



foreach($server in (Get-ADForest).globalcatalogs){
  get-addomaincontroller -server $server | select   domain, ipv4address, name | '{0. 20}, -- {1, 20}, {2, 20}' -f  domain, ipv4address, name
}
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40476432
This should accomplish that adjustment...

foreach($server in (Get-ADForest).globalcatalogs){
  get-addomaincontroller -server $server | format-table domain, ipv4address, name -autosize
}

Open in new window

0
 

Author Comment

by:toshi_
ID: 40476442
thank again, but autosize is not creating columns with the results...
can you help me with formating the result ?

how to get the result and play with {0,20} ?

toshi
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40476444
Oh, I see...
So, try this instead of your original script:
foreach($server in (Get-ADForest).globalcatalogs){
  $dc = get-addomaincontroller -server $server 
  'server : {0, 20}  ---  IP : {0, 20}  ---  Name: {0, 20}' -f $dc.domain, $dc.ipv4address, $dc.name
}

Open in new window

0
 

Author Comment

by:toshi_
ID: 40476455
thank for your quick reply,

this display is the same that i started with .....only the domain name is display three times .
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 40476459
Oh, ah...I overlooked that... increment the elements:
 'server : {0, 20}  ---  IP : {1, 20}  ---  Name: {2, 20}' -f $dc.domain, $dc.ipv4address, $dc.name
0
 

Author Comment

by:toshi_
ID: 40476464
youuuuuu rooock !

thanks a lot !
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40476487
Glad I could help. :^)
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

972 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