使用具有自定义格式的 ToString() 格式化日期会导致输出不一致
Date formatted using ToString() with a custom format results in inconsistent output
我使用 powershell 脚本导出了所有存在的 DHCP 服务器的列表以及服务器上的范围。结果必须在 python 中的第二个脚本中进行解析。 (那是我们监控的系统)。
这是脚本:
$DHCPServers = (Get-DhcpServerInDC).Dnsname
$Scopes = @()
$Leases = @()
foreach ($DHCPServer in $DHCPServers) {
$Scopes += Get-DhcpServerv4Scope -ComputerName $DHCPServer | select-Object @{Name="DHCPServer"; Expression = {$DHCPServer}},ScopeID,Name,StartRange,EndRange
}
$Scopes | Export-Csv -path .\DHCP_Freeleases-Scopes-get.csv -Encoding utf8 -NoTypeInformation
foreach ($Scope in $Scopes) {
$Leases += Get-DHCPServerv4Lease -ScopeId $Scope.ScopeID -ComputerName $Scope.DHCPServer | select-Object @{Name="DHCPServer"; Expression = {$Scope.DHCPServer}}, IPAddress, @{Name="LeaseExpiryTime"; Expression = {$_.LeaseExpiryTime.toString("yyyy/MM/dd/HH:mm")}}, @{Name="ScopeID"; Expression = {$Scope.ScopeID}}, Addressstate
}
$Leases | Export-Csv -path .\DHCP_Freeleases-Leases-get.csv -Encoding utf8 -NoTypeInformation
虽然我在本地 DHCP 服务器(Windows 服务器 2012 R2)上编写了该脚本,但 CSV 导出是我想要的方式:
"dhcp1.server.intern","10.26.9.202","2022/01/25/08:22","172.26.9.192","Active"
现在我将脚本部署到不同的服务器(Windows Server 2019),完全相同的脚本给了我这个 CSVline:
"dhcp1.server.intern","10.26.9.202","2022.01.25.08:22","172.26.9.192","Active"
我想明白:怎么会这样?我以为 .toString() 用我的参数给出了它?
感谢您的帮助!
DateTime.ToString()
方法将正斜杠和冒号视为格式说明符。这意味着它们可以被 locale-specific 个字符替换。
The "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z",
":", or "/" characters in a format string are interpreted as custom
format specifiers rather than as literal characters. To prevent a
character from being interpreted as a format specifier, you can
precede it with a backslash (), which is the escape character.
要强制使用固定格式,只需使用反斜杠转义这些字符:
$_.LeaseExpiryTime.toString("yyyy\/MM\/dd\/HH\:mm")
正如您在评论中指出的那样,您也可以使用 predefined format,例如 's' (ISO 8601),即 locale-independent 的定义:
$_.LeaseExpiryTime.toString('s')
输出看起来像 2022-01-25T11:20:26
。
我使用 powershell 脚本导出了所有存在的 DHCP 服务器的列表以及服务器上的范围。结果必须在 python 中的第二个脚本中进行解析。 (那是我们监控的系统)。
这是脚本:
$DHCPServers = (Get-DhcpServerInDC).Dnsname
$Scopes = @()
$Leases = @()
foreach ($DHCPServer in $DHCPServers) {
$Scopes += Get-DhcpServerv4Scope -ComputerName $DHCPServer | select-Object @{Name="DHCPServer"; Expression = {$DHCPServer}},ScopeID,Name,StartRange,EndRange
}
$Scopes | Export-Csv -path .\DHCP_Freeleases-Scopes-get.csv -Encoding utf8 -NoTypeInformation
foreach ($Scope in $Scopes) {
$Leases += Get-DHCPServerv4Lease -ScopeId $Scope.ScopeID -ComputerName $Scope.DHCPServer | select-Object @{Name="DHCPServer"; Expression = {$Scope.DHCPServer}}, IPAddress, @{Name="LeaseExpiryTime"; Expression = {$_.LeaseExpiryTime.toString("yyyy/MM/dd/HH:mm")}}, @{Name="ScopeID"; Expression = {$Scope.ScopeID}}, Addressstate
}
$Leases | Export-Csv -path .\DHCP_Freeleases-Leases-get.csv -Encoding utf8 -NoTypeInformation
虽然我在本地 DHCP 服务器(Windows 服务器 2012 R2)上编写了该脚本,但 CSV 导出是我想要的方式:
"dhcp1.server.intern","10.26.9.202","2022/01/25/08:22","172.26.9.192","Active"
现在我将脚本部署到不同的服务器(Windows Server 2019),完全相同的脚本给了我这个 CSVline:
"dhcp1.server.intern","10.26.9.202","2022.01.25.08:22","172.26.9.192","Active"
我想明白:怎么会这样?我以为 .toString() 用我的参数给出了它?
感谢您的帮助!
DateTime.ToString()
方法将正斜杠和冒号视为格式说明符。这意味着它们可以被 locale-specific 个字符替换。
The "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" characters in a format string are interpreted as custom format specifiers rather than as literal characters. To prevent a character from being interpreted as a format specifier, you can precede it with a backslash (), which is the escape character.
要强制使用固定格式,只需使用反斜杠转义这些字符:
$_.LeaseExpiryTime.toString("yyyy\/MM\/dd\/HH\:mm")
正如您在评论中指出的那样,您也可以使用 predefined format,例如 's' (ISO 8601),即 locale-independent 的定义:
$_.LeaseExpiryTime.toString('s')
输出看起来像 2022-01-25T11:20:26
。