如何使用 WMI 获取 dhcp 租约到期时间?

How to get dhcp lease expire time with WMI?

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

但是可以通过WMI查询得到吗?请举例说明。

使用 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.Management. A DateTime supports several formatting strings 使用 ManagementDateTimeConverter.ToDateTime(String)

$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