【问题标题】:How to get dhcp lease expire time with WMI?如何使用 WMI 获取 dhcp 租约到期时间?
【发布时间】:2020-06-03 09:07:00
【问题描述】:

现在我使用Get-DhcpServerv4Lease -ComputerName 'pc' -ScopeId 'id' -AllLeases$object.leaseExpiryTime 来获取租约到期时间。

但是是否可以通过 WMI 查询获得它?请举个例子。

【问题讨论】:

  • 我知道这种方法:Get-WmiObject -query "SELECT * FROM Win32_NetworkAdapterConfiguration" 但看起来我无法使用它获得到期时间

标签: powershell wmi dhcp


【解决方案1】:

使用Win32_NetworkAdapterConfiguration,因为它确实包含DHCPLeaseExpires。举个例子,

gwmi -query "select DHCPLeaseExpires, ipaddress, Description from Win32_NetworkAdapterConfiguration where dhcpleaseexpires is not null"
# Output
Description      : Intel(R) Dual Band Wireless-AC 8265
DHCPLeaseExpires : 20200604032809.000000+180
IPAddress        : {192.168.1.104, fe80::...}

要将 DHCPLeaseExpires 转换为 .Net DateTime,请使用来自 System.ManagementManagementDateTimeConverter.ToDateTime(String)。一个 DateTime 支持多个formatting strings 像这样,

$wlan = gwmi -query "select ..."
Add-Type -AssemblyName System.Management
$t = [Management.ManagementDateTimeConverter]::ToDateTime($wlan.DHCPLeaseExpires)
$t.GetType()                                                                            
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DateTime                                 System.ValueType

$t.ToString('u')
2020-06-04 03:28:09Z

$t.ToString('G')                                                                        
4.6.2020 3:28:09

【讨论】:

  • 感谢您的回答。但是,如果我有更多的参数,那么我需要怎么办?看,我运行 gwmi -ComputerName dhcp -query "select DHCPLeaseExpires from Win32_NetworkAdapterConfiguration where DHCPLeaseExpires is not null" 并得到了这个输出:__GENUS : 2 __CLASS : Win32_NetworkAdapterConfiguration __SUPERCLASS : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : DHCPLeaseExpires : 20200603122700.000000+120 PSComputerName 在你的输出中你只得到了你所要求的。但我得到了很多额外的信息。为什么?
  • 为简洁起见,我省略了这些。将结果传送到Select-Object 并选择您需要的任何内容。
  • 请您再告诉我一件事。时间格式是什么 - “20200604032809.000000+180”?有什么方法可以将它与任何格式的 (Get-Date) 进行比较?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-21
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
相关资源
最近更新 更多