查找所有 DHCP 和 DNS 服务器 [英] Finding All DHCP and DNS servers
问题描述
我有一个客户让我找到他所有的 Dhcp 和 DNS 服务器以及一些额外的信息,比如 DC 服务器和操作系统所以我决定尝试提高我的 powershell 技能,但我还是新手,所以我写了这个脚本,但我想仍然缺少一些东西,因为它不起作用
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
我设法找到了一种方法来获取我想要的操作系统信息,但它让我回到了公司中的所有服务器
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
}
推荐答案
这并不太棘手.首先,您连接到安装了 RSAT 工具的机器,例如管理服务器、跳转盒或域控制器,并获取所有 DHCP 服务器的列表.
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
然后我们使用 PowerShell 的内置循环逻辑来遍历每个服务器,并检查您需要的操作系统信息.
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
}
最后,我们将修改上面的内容以返回您要查找的信息,即 IP 地址、名称和操作系统版本
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.
希望这会有所帮助.
如果这不起作用,请确保您已先启用 PowerShell 远程处理.
If this isn't working, make sure you have enabled PowerShell Remoting first.
这篇关于查找所有 DHCP 和 DNS 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!