Solved

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

Posted on 2014-12-02
12
620 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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 antispam), the admini…

734 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