【问题标题】:Using Powershell to get DHCP Client Info使用 Powershell 获取 DHCP 客户端信息
【发布时间】:2020-10-11 04:06:00
【问题描述】:

是否可以使用 Powershell 在庞大的 DHCP 服务器和作用域列表中查找 DHCP 客户端?

我在一家医院工作,该医院有多个 DHCP 服务器(每个区域一个),具有多个作用域(每台服务器超过 50 个 DHCP 作用域。我正在尝试找到一种方法,我可以编写一个 Powershell 脚本来筛选所有服务器和范围,并返回我的(手动)服务器停用过程所需的相关信息,而不必手动执行此操作。

我们确实有应用程序和工具 (Solarwinds) 可以让我获得所需的信息,但它涉及且耗时,我宁愿将此 Powershell 脚本作为“一站式”脚本运行收集我的信息并清理 DHCP 清单。并最终将其融入到退役服务器的整个端到端流程中。我的梦想是能够运行一个脚本,给它一个服务器名称,并让整个过程运行并清除从 DHCP 到 DNS 到 AD 用户和计算机的所有内容。但我现在从小处着手。

【问题讨论】:

  • 只有知道自己想要什么,知道从哪里开始,并使用正确的工具,一切皆有可能。但是,SO 有规则:Provide MRE --- How to ask --- Don't ask --- Proper Topic。您是否查看过可用的 cmdlet?你搜索过什么?你试过什么?发布您的代码。这里常见的说法是 SO 不是脚本编写服务。人们可以提供帮助,但你必须表现出来。
  • 我应该知道最好不要在这里问问题。谢谢你带我走出你的酷孩子沙箱。
  • ;-} 不,问题很好,这就是我们都在这里的原因。但是,它们必须符合规则。 ;-} 好吧,除非它真的很有趣,而且有些人会和你一起深入研究它,或者他们只是感觉好。 ;-} 即使在“善良”的部分,如果它(问题)不符合 SO 规则,也可能会因为这样做而受到其他人的惩罚。 Col 儿童沙盒。 ;--}。不,只是互联网和您最喜欢的搜索引擎,或此页面顶部的 SO 搜索框。只需搜索“PowerShell DHCP”
  • 我在 Reddit 上逐字发布了同样的问题,并收到了积极的回应。 reddit.com/r/PowerShell/comments/jb4fnd/…
  • 比较社区确实没有实际意义。每个社区都是不同的,每个社区都有自己的规则、版主、成员;参与者将做他们在每个人身上所做的事情。在 Reddit 上的许多情况下,我也看到了相同的回击,如果不是每次回击的话。所以,从你得到它的地方寻求帮助。然而,这方面的所有人都在自己的时间免费做这件事。因此,为什么感谢海报的努力。可以提供更方便和更谨慎的响应。

标签: powershell dhcp windows-server-2019


【解决方案1】:

使用示例资源

'PowerShell DHCP'

使用视频教程

'Youtube DHCP management'

使用内置 cmdlet

Get-Command -Name '*DHCP*' | 
Where-Object -Property Name -like '*scope*' | 
Format-Table -AutoSize
# Results
<#
CommandType Name                                     Version Source    
----------- ----                                     ------- ------    
Function    Add-DhcpServerv4FailoverScope            2.0.0.0 DhcpServer
Function    Add-DhcpServerv4MulticastScope           2.0.0.0 DhcpServer
Function    Add-DhcpServerv4Scope                    2.0.0.0 DhcpServer
Function    Add-DhcpServerv4Superscope               2.0.0.0 DhcpServer
Function    Add-DhcpServerv6Scope                    2.0.0.0 DhcpServer
Function    Get-DhcpServerv4MulticastScope           2.0.0.0 DhcpServer
Function    Get-DhcpServerv4MulticastScopeStatistics 2.0.0.0 DhcpServer
Function    Get-DhcpServerv4Scope                    2.0.0.0 DhcpServer
Function    Get-DhcpServerv4ScopeStatistics          2.0.0.0 DhcpServer
Function    Get-DhcpServerv4Superscope               2.0.0.0 DhcpServer
Function    Get-DhcpServerv4SuperScopeStatistics     2.0.0.0 DhcpServer
Function    Get-DhcpServerv6Scope                    2.0.0.0 DhcpServer
Function    Get-DhcpServerv6ScopeStatistics          2.0.0.0 DhcpServer
Function    Remove-DhcpServerv4FailoverScope         2.0.0.0 DhcpServer
Function    Remove-DhcpServerv4MulticastScope        2.0.0.0 DhcpServer
Function    Remove-DhcpServerv4Scope                 2.0.0.0 DhcpServer
Function    Remove-DhcpServerv4Superscope            2.0.0.0 DhcpServer
Function    Remove-DhcpServerv6Scope                 2.0.0.0 DhcpServer
Function    Rename-DhcpServerv4Superscope            2.0.0.0 DhcpServer
Function    Set-DhcpServerv4MulticastScope           2.0.0.0 DhcpServer
Function    Set-DhcpServerv4Scope                    2.0.0.0 DhcpServer
Function    Set-DhcpServerv6Scope                    2.0.0.0 DhcpServer
#>

使用帮助文件中的示例开始或完成您的任务

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-DhcpServerv4Scop).Parameters
(Get-Command -Name Get-DhcpServerv4Scop).Parameters.Keys
Get-help -Name Get-DhcpServerv4Scop -Examples
Get-help -Name Get-DhcpServerv4Scop -Full
Get-help -Name Get-DhcpServerv4Scop -Online

# Find all cmdlets / functions with a target parameter
Get-Command -CommandType Cmdlet |
Where-Object {
    Try {$PSItem.parameters.keys -match 'credential'}
    Catch{} 
}|
Out-GridView -PassThru -Title '
Available cmdlets which has a specific parameter'

Get-Command -CommandType Function |
Where-Object {
    Try {$PSItem.parameters.keys -match 'credential'}
    Catch{} 
}|
Out-GridView -PassThru -Title '
Available functions which has a specific parameter'

# Get property enums/options for a specifc cmdlet/function
(Get-Service | Select-Object -First 1).Status.GetType()
[System.ServiceProcess.ServiceControllerStatus]::
GetNames([System.ServiceProcess.ServiceControllerStatus])

寻找其他可以利用的模块/脚本

Find-Module -Name '*DHCP*' | Format-Table -AutoSize
# Results
<#
Version Name                     Repository Description                                                                                                                  
------- ----                     ---------- -----------                                                                                                                  
2.0.0.0 xDhcpServer              PSGallery  Module with DSC Resources for DHCP Server area                                                                               
1.3     DHCPClient               PSGallery  Sample module for retrieving DHCP client details, based on the script published by Ingmar Verheij at https://www.ingmarver...
1.2.1   DHCPMigration            PSGallery  A module to copy various DHCP information from 1 server to another.                                                          
1.0.0.3 Read-DHCPLogFiles        PSGallery  A small PS module to read DHCP txt logs                                                                                      
1.3     cDhcpServerDynamicUpdate PSGallery  Class based resource to configure DHCP server dynamic updates 
#>

Find-Script -Name '*DHCP*' | Format-Table -AutoSize
# Results
<#
Version Name                               Repository Description                                                                     
------- ----                               ---------- -----------                                                                     
1.0.0   NetIPInterface_EnableDHCP_Config   PSGallery  Enabling DHCP for the IPv4 Address and DNS on the adapter with alias 'Ethernet'.
1.0.0   DnsServerAddress_EnableDHCP_Config PSGallery  Enabling DHCP for the IPv4 Address and DNS on the adapter with alias 'Ethernet'.
#>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2012-11-28
    • 1970-01-01
    • 2016-01-29
    • 2014-10-04
    • 2015-02-21
    • 2018-12-21
    相关资源
    最近更新 更多