Get-ItemProperty of reg value in all DCs all Domains

Welcome Forums General PowerShell Q&A Get-ItemProperty of reg value in all DCs all Domains

This topic contains 5 replies, has 5 voices, and was last updated by

1 year, 4 months ago.

  • Author
  • #81767

    Points: 71
    Rank: Member

    I have my variable populated correctly and have hostnames for all of my DCs but my forEach isn't working...

    $DCs = foreach ($domain in (get-adforest -Identity { get-addomaincontroller -filter * -server $domain | select hostname}
    foreach( $dc in $DCs )
    { invoke-command -computername $dc -command {Get-ItemProperty -path hklm:\System\CurrentControlSet\Services\NTDS\Parameters -Name "Strict Replication Consistency" | Select-Object "Strict Replication Consistency"}}

    What is incorrect here?

    thank you

  • #81769

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    What isn't working? It's a little hard to just cold-read code written by someone else ;).

  • #81772

    Points: 0
    Rank: Member

    I dont recall a -command parameter
    Invoke-command -scriptblock {}

    Try to run ur invoke against one of ur dc

  • #81776

    Points: 1
    Rank: Member

    looks like you need to expand your hostname property, rather than just selecting it:

    ... get-addomaincontroller -filter * -server $domain | select hostname}

    change to:

    ... get-addomaincontroller -filter * -server $domain | select -ExpandProperty hostname}

    without the -expandproperty switch what you are actually creating is an array of objects with a hostname property, rather than a simple string list. So when you run that through your foreach, instead of passing a string value with the DC name to the invoke-command, you're actually passing it a psobject with a hostname property. invoke-command has no idea what to do with that. You could also give invoke-command what it wants by using:

    invoke-command -computername $DC.hostname ...


    foreach($dc in $DCs.hostname)

    Those two options aren't as straightforward as the first one though, but should illustrate how to navigate around the objects you've created. Best bet is to do the first option and make sure the value in $DCs looks the way you want it.

    • #81823

      Points: 71
      Rank: Member


      thanks this really helped me see the contents of what I was sending over the forEach. I looked at gm for my original $DCs and then did it for the expandedProperty one and saw what you were talking about.

      Appreciate the lesson and I see the values now across all my DCs


  • #81865

    Points: 780
    Helping Hand
    Rank: Major Contributor

    # Or, you could tighten this up to, say...

    (Get-ADForest -Identity $env:USERDNSDOMAIN).domains `
    | % {(Get-ADDomainController -filter * -Server $_).HostName} `
    | % {invoke-command -computername $_ `
    -ScriptBlock {Get-ItemProperty `
    -path 'hklm:\System\CurrentControlSet\Services\NTDS\Parameters' `
    -Name "Strict Replication Consistency" `
    | Select-Object "Strict Replication Consistency"
    } | Format-Table -AutoSize

    # Results

    Strict Replication Consistency PSComputerName RunspaceId
    —————————— ————– ———-
    1 c96bb82c-...

The topic ‘Get-ItemProperty of reg value in all DCs all Domains’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort