Finding All DHCP and DNS servers

心不动则不痛 提交于 2020-05-24 05:14:23

问题


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 skills but im still new to this so i wrote this script but i guess something is still missing because it doesnt work

EDIT: i managed to find a way to get the info i want which is the OS but it gets me back ALL the servers in the company

$servers = get-dhcpserverindc
   foreach($server in $Servers){
get-adcomputer -filter {Operatinsytem -like "*windows server*"} -properties 
Operatingsystem | sort name | format-table name,Operatinsytem

}

回答1:


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.



来源:https://stackoverflow.com/questions/61575474/finding-all-dhcp-and-dns-servers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!