Finding All DHCP and DNS servers

后端 未结 1 1783
时光说笑
时光说笑 2021-01-29 00:21

I got a client who asked me to find all of his Dhcp and DNS servers with some additional info like DC servers and operationsystem so i decided to try sharpen my powershell skill

1条回答
  •  被撕碎了的回忆
    2021-01-29 01:04

    This is not too tricky. First off, you connect to a machine with the RSAT Tools installed, like an admin server, jump box, or Domain Controller, and get a list of all DHCP Servers.

    $DHCPServers = Get-DhcpServerInDC
    

    Then we use PowerShell's built in looping logic to step through each server, and check for the OS info you need.

    ForEach ($DHCPServer in $DHCPServers){
       $OSInfo = Get-CIMInstance -ComputerName $DHCPServer.DnsName -ClassName Win32_OperatingSystem
    }
    

    Finally, we'll modify this above to return the info you're looking for, namely the IP Address, Name, and OS Version

    ForEach ($DHCPServer in $DHCPServers){
       $OSInfo = Get-CIMInstance -ComputerName $DHCPServer.DnsName -ClassName Win32_OperatingSystem
       [pscustomobject]@{
          ServerName = $DHCPServer.DnsName;
          IPAddress=$DHCPServer.IpAddress;
          OS=$OSInfo.Caption
        }
    }
    
    ServerName IPAddress    OS                                    
    ---------- ---------    --                                    
    dc2016     192.168.10.1 Microsoft Windows Server 2016 Standard
    

    From there, you can store it in a variable, make it a spreadsheet, do whatever you need to do.

    Hope this helps.

    If this isn't working, make sure you have enabled PowerShell Remoting first.

    0 讨论(0)
提交回复
热议问题