gmagerr
 New Member Posts:82

 |
| 09 Aug 2010 05:47 PM |
|
Hi guys,
I'm trying to query our network for domain controllers and put them into variables. Santa Monica DC's go into $DCSM, Pittsburgh go into $DCPGH etc. I need a little help, i'm getting Fasle back in the console, not the names of the DC's. Here's the code.
Thanks
Get-QADComputer -ComputerRole DomainController | Where-Object {$_.Name -NotMatch "SRGDC1"} | ForEach-Object{ $DCSM = $_.Name -Match {"SMDC1,SMDC2,SMDC3,SMDC4"} $DCPGH = $_.Name -Match {"PGHDC1,PGHDC2,"} $DCDC = $_.Name -Match {"DCDC1,DCDC2"} $DCQT = $_.Name -Match {"RQPIDC1"} $DCCMB = $_.Name -Match {"CMBGDC1,CMBGDC2"} } Write-Host "Santa Monica: " $DCSM Write-Host "Pittsburgh: " $DCPGH Write-Host "Arlington: " $DCDC Write-Host "Qutar: " $DCQT Write-Host "Cambridge: " $DCCMB
|
|
|
|
|
Shay Levy PowerShell MVP, Admin
 Veteran Member Posts:1362

 |
| 09 Aug 2010 11:53 PM |
|
$DCs = Get-QADComputer -ComputerRole DomainController | Where-Object {$_.Name -NotMatch "SRGDC1"} $DCSM = $DCs | where {$_.Name -Match "SMDC1|SMDC2|SMDC3|SMDC4"} $DCPGH = $DCs | where {$_.Name -Match "PGHDC1|PGHDC2"} $DCDC = $DCs | where {$_.Name -Match "DCDC1|DCDC2"} $DCQT = $DCs | where {$_.Name -Match "RQPIDC1"} $DCCMB = $DCs | where {$_.Name -Match "CMBGDC1|CMBGDC2"} |
|
Shay Levy Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter: @ShayLevy |
|
|
gmagerr
 New Member Posts:82

 |
| 10 Aug 2010 05:34 AM |
|
Thanks Shay,
Instead of hard coding the names, I think this makes it more dynamic and should pick up any new DC's in those sites. Does the syntax look ok?
Thanks
$DCs = Get-QADComputer -ComputerRole DomainController | Where-Object {$_.Name -NotMatch "SRGDC1"}
$DCSM = $DCs | where {$_.Name -Like "*SMD*"} $DCPGH = $DCs | where {$_.Name -Like "*PGHD*"} $DCDC = $DCs | where {$_.Name -Like "*DCDC*"} $DCQT = $DCs | where {$_.Name -Like "*RQPI*"} $DCCMB = $DCs | where {$_.Name -Like "*CMBG*"}
Write-Host "Santa Monica: " $DCSM Write-Host "Pittsburgh: " $DCPGH Write-Host "Arlington: " $DCDC Write-Host "Qutar: " $DCQT Write-Host "Cambridge: " $DCCMB
|
|
|
|
|
Shay Levy PowerShell MVP, Admin
 Veteran Member Posts:1362

 |
| 10 Aug 2010 06:44 AM |
|
If you're looking to create variables for each AD site you have then try this, it creates a variable (named on each site name) for each site with a value of the DCs in that site: [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites | Where-Object {$_.Servers} | Foreach-Object {New-Variable -Name $_.Name -Value ($_.Servers | Foreach-Object {$_.Name})}
|
|
Shay Levy Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter: @ShayLevy |
|
|
gmagerr
 New Member Posts:82

 |
| 10 Aug 2010 07:52 AM |
|
Wow! You never cease to amaze. How would I use that variable? What's it called?
Thanks
|
|
|
|
|