locked
Get Servers from Two Domains RRS feed

  • General discussion

  • Hello and thanks for your help.

    I'm trying to get all my servers from two different domains and two different forest.  If I run the following two lines they each work:

    Get-ADComputer -server DomainName -Filter {operatingsystem -like '*Server*'} -Properties OperatingSystem |Select name,OperatingSystem
    Get-ADComputer  -Filter {operatingsystem -like '*Server*'} -Properties OperatingSystem |Select name,OperatingSystem

    However, the problem comes when I try to combine them both in single line to feed them into a variable,
    $ServerList = Get-ADComputer ((-Filter {operatingsystem -like '*Server*'} -Properties OperatingSystem |Select name) -and (-server DomainName-Filter {operatingsystem -like '*Server*'} -Properties OperatingSystem |Select name))

    I know that I must be missing something small but I can see it.  Thanks.


    • Edited by abdielh Monday, January 28, 2019 5:53 PM
    • Changed type Bill_Stewart Friday, March 15, 2019 6:47 PM
    • Moved by Bill_Stewart Friday, March 15, 2019 6:48 PM This is not "train me in basics step-by-step in a forum" forum
    Monday, January 28, 2019 5:53 PM

All replies

  • Simple.  You are missing all of the basic training in PowerShell and guessing is not even close.

    It is a simple matter of syntax.  You cannot just mix items arbitrarily.  Also properly formatting your code would help you see obvious mistakes.

    The first line is all you need.  The second line does nothing new.  Perhaps you should start by stating what the purpose of this is.  Why do you think you need to combine the two lines.

    Get-ADComputer -server DomainName -Filter { operatingsystem -like '*Server*' } -Properties OperatingSystem | 
         Select-Object name, OperatingSystem

    Also always start by carefully reading the documentation for all commands you want to use.

    help Get-ADComputer -online
    help Select-Object -online


    \_(ツ)_/


    • Edited by jrv Monday, January 28, 2019 6:14 PM
    Monday, January 28, 2019 6:13 PM
  • Hi JRV, thanks for trying to respond to my "simple" problem and lack of "simple matter of syntax."  I'm not a PowerShell export and I'm just trying some simple stuff here.  Like I stated, I'm trying to get the information from two different domains in two different forest.  The first line includes "-server" so it can get the information from the DC where I am not logged in.  The second line does not contain "-server" because I am logged on to that domain and in the absence of it, it will be "using the domain of the computer running Windows PowerShell."  It's in "basic" powershell training and "simple matter of syntax."
    Monday, January 28, 2019 7:08 PM
  • It cannot be done with one command.

    $domain1, $domain2 |
        ForEach-Object{
            Get-ADComputer -server $_ -Filter { operatingsystem -like '*Server*' } -Properties OperatingSystem
        } |
         Select-Object name, OperatingSystem


    \_(ツ)_/

    Monday, January 28, 2019 7:16 PM
  • You can also just do this:

    Get-ADComputer -server sbs01:3268 -Filter * -Properties OperatingSystem |
         select Name, DnsHostName, OperatingSystem
    The 3268 specifies a GC server.


    \_(ツ)_/


    • Edited by jrv Monday, January 28, 2019 7:38 PM
    Monday, January 28, 2019 7:29 PM
  • This will find an available GC to query:

    $gc = Get-ADDomainController -Discover -Service GlobalCatalog
    $server = "$($gc.HostName):3268"
    Get-adcomputer -Server $server -filter * -Properties OperatingSystem |
    Select Name, DnsHostName, OperatingSystem


    \_(ツ)_/


    • Edited by jrv Monday, January 28, 2019 7:44 PM
    Monday, January 28, 2019 7:42 PM
  • I think what jrv is trying to tell you is that you can't simply guess and arbitrarily paste code together (without any understanding of how it works) and expect the computer to figure out what you mean. As you already know, the computer only does precisely and exactly what you tell it to do.

    Learning PowerShell is a process that takes some time as there are some ways it works a bit differently than other shells. The learning investment is worth it, but it is an investment.

    We don't have the resources to perform one-on-one hands-on PowerShell training. That won't work in a forum anyway, because it simply isn't interactive enough.


    -- Bill Stewart [Bill_Stewart]

    Monday, January 28, 2019 8:23 PM
  • Hello Bill,

    Thanks for adding this information and I think I tried to make this problem too simple.  JRV suggested to include the purpose of my question, which I will next time.  What I am trying to accomplish is to get how many days each one of my servers has been up for.  I wrote the script that gives me that information but I have to run it on each domain at a time.  Here's how it looks like:

    $Domain1 = Get-ADComputer  -Filter {operatingsystem -like '*Server*'} -Properties OperatingSystem |Select name
    $Domain2 = Get-ADComputer -server DCName -Filter {operatingsystem -like '*Server*'} -Properties OperatingSystem |Select name

    foreach ($Server in $Domains)
        {
        $ServerName = $Server.name
        $BootDate = Get-CimInstance -ComputerName $ServerName -ClassName win32_operatingsystem | select CSName, LastBootupTime
        $DisplayBoot = $BootDate.LastBootupTime
        $os = Get-WmiObject -ComputerName $ServerName win32_operatingsystem
        $uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
        $DisplayTime = $Uptime.Days
        $Report = " " + $Server.name, $DisplayTime, " Days"
        $Report |Out-File U:\scripts\Get-UpTime\Uptime.txt -Append
        }

    But then, I also complicated my life more than I need it.  All I could have done is combine the results of each variable into another one.

    $Domains = $Domain1, $Domain2



    • Edited by abdielh Tuesday, January 29, 2019 6:32 PM
    Tuesday, January 29, 2019 6:31 PM
  • Somebody needs to learn basic PowerShell before moving on.

    [array]$servers += Get-ADComputer  -Filter {operatingsystem -like '*Server*'} -Properties OperatingSystem |Select --expand name
    [array]$servers += Get-ADComputer -server DCName -Filter {operatingsystem -like '*Server*'} -Properties OperatingSystem |Select -expand name


    \_(ツ)_/


    • Edited by jrv Tuesday, January 29, 2019 6:36 PM
    Tuesday, January 29, 2019 6:36 PM
  • This is how to do this the easy way:

    {
         Get-ADComputer -Filter { operatingsystem -like '*Server*' }
         Get-ADComputer -Filter { operatingsystem -like '*Server*' 
    }}.Invoke() | ForEach-Object{ Get-CimInstance -ClassName win32_operatingsystem -ComputerName $_.Name } | Select-Object PsComputerName, LastBootUpTime

    There is no need to convert the data and time.  It is already a datetime object.


    \_(ツ)_/



    • Edited by jrv Tuesday, January 29, 2019 6:45 PM
    Tuesday, January 29, 2019 6:43 PM
  • We can also standardize and prettify it:

    function Get-Uptime{
        Param(
            
        )
        $filter = { operatingsystem -like '*Server*' }
        Get-ADComputer -Filter $filter 
        Get-ADComputer -Filter $filter -Server domain2
    }
    
    Get-Uptime | 
        ForEach-Object{Get-CimInstance win32_operatingsystem -ComputerName $_.Name} | 
        Select-Object PsComputerName, LastBootUpTime


    \_(ツ)_/



    • Edited by jrv Tuesday, January 29, 2019 6:59 PM
    Tuesday, January 29, 2019 6:50 PM
  • If you really want uptime in days then change this:

    Select-Object PsComputerName, @{n='Uptime';e={([datetime]::Today - $_.LastBootUpTime).Days}}
    


    \_(ツ)_/

    Tuesday, January 29, 2019 6:53 PM
  • If you really only have two computers then this is the easy way to do it:

    $gc = Get-ADDomainController -Discover -Service GlobalCatalog
    $server = "$($gc.HostName):3268"
    Get-adcomputer -Server $server -filter {operatingsystem -like '*Server*'} |
        ForEach-Object{Get-CimInstance win32_operatingsystem -ComputerName $_.Name} |
        Select-Object PsComputerName, @{ n = 'Uptime'; e = { ([datetime]::Today - $_.LastBootUpTime).Days } }


    \_(ツ)_/



    • Edited by jrv Tuesday, January 29, 2019 6:59 PM
    Tuesday, January 29, 2019 6:55 PM
  • Thanks JRV.  And yes, I am working on my PS skills.  Definitely worth it.
    Tuesday, January 29, 2019 7:01 PM