powershell - 无法比较创建日期和当前日期

标签 powershell powershell-2.0 vmware powercli

希望有人可以帮助我。我无法将当前日期与创建文件的日期进行比较。我从每个日期获得的输出及其代码如下。

创建日期输出:

21/05/2012 10:27:25 PM



当前日期输出:

8/05/2013 12:00:00 AM



是否可以比较这些日期?

我的代码如下:
$host = Read-Host 'Host: '
$username = Read-Host 'Username: '
$password = Read-Host 'Password: '

Connect-VIServer -Server $host -User $username -Password $password

$snapshotDate = Get-Snapshot -VM CONVCORPSPOINT | Select-Object Created | Format-Table -HideTableHeaders
$currentDate = Get-Date | Select-Object Date | Format-Table -HideTableHeaders

$snapshotDate
$currentDate

if ($snapshotDate -lt $currentDate) {
    Write-Host 'The snapshot date is earlier than the current date'
}
else {
    Write-Host 'The snapshot date is not earlier than the current date'
}

最佳答案

试试这个:

$x = Get-Date

您可以获得执行此操作的与date对象关联的所有方法的列表:
$x | gm

您可以这样设置日期格式:
$x.ToString("yyyyMMdd hh:mm:ss")

所有格式选项均在here中描述。
然后,您可以标准化日期并轻松比较它们。

关于powershell - 无法比较创建日期和当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16434374/

相关文章:

powershell - 无法使用此Windows Powershell运行此简单的.exe文件

powershell - 如何在 PowerShell 中从 csv 中查找 Active Directory 组名?

.net - 如何创建带有左对齐选项卡的 TabControl 面板?

powershell - 如何找到 msi 是否有时间戳?

powershell - 使用 powershell 处理字符串中的变量

linux - 将 Ubuntu 上的 VSCode 键绑定(bind)从类 linux 更改为类 Mac

loops - Powershell 可以一次循环遍历集合 N 个对象吗?

xml - 如果许多条目具有相同的节点名称,如何正确定位 xml 节点?

PowerShell 2 中的泛型不起作用?

node.js - nodejs + vmware vsphere sdk SOAP 连接