header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Getting Domain Controllers into $Variables
Last Post 10 Aug 2010 07:52 AM by gmagerr. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
gmagerrUser is Offline
New Member
New Member
Posts:82
Avatar

--
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 LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    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
    gmagerrUser is Offline
    New Member
    New Member
    Posts:82
    Avatar

    --
    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 LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    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
    gmagerrUser is Offline
    New Member
    New Member
    Posts:82
    Avatar

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

    Thanks
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer