本文介绍了DateTime减法在PowerShell中不起作用-赋值与相等运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天(2017-05-29)我在Windows 7企业版上使用PowerShell 5.0.10586.117并运行以下命令(缩短):

Today (2017-05-29) I am using PowerShell 5.0.10586.117 on Windows 7 Enterprise and run the following (shortened):

$dateOfLicense = "2017-04-20"
$dateOfToday = '{0:yyyy-MM-dd}' -f (Get-Date)

$TimeDifference = [DateTime]$dateOfToday - [DateTime]$dateOfLicense
if (($TimeDifference) = 14)
{
    Write-Host "test"
}

即使两天之间的时间差是39,我的代码也会跳到if子句中,并向屏幕发送测试".

Even the difference between both days is 39, my code jumps in the if-clause and sends "test" to screen.

我在做什么错了?

推荐答案

您正在 14分配给$TimeDifference.相反,您将不使用-le来比较Days属性:

You are assigning 14 to $TimeDifference. Instead you wan't to compare the Days property using -le:

if ($TimeDifference.Days -le 14)
{
    Write-Host "test"
}

这篇关于DateTime减法在PowerShell中不起作用-赋值与相等运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:31