簡體   English   中英

值在 Powershell 中沒有體現

[英]Values is not picking up in Powershell

我是 Powershell 的新手,我在 PS 中編寫了一個小實用程序,其中我提供 $firstDate 和 $lastDate 中的值,但是當我運行此程序時,這兩個值未打印在 $value 中。任何人都可以看看並幫助我在這一點上。

$firstDate=Get-Date -Year.2018 -Month.12 -Day.1
$lastDate=Get-Date -Year.2020 -Month.1 -Day.12
$value = $firstDate -eq $lastDate

當您使用-year-month-day提供值時,您必須在一個space之后為其定義值。 您正在使用點表示法,它主要用於訪問變量的屬性或方法。 @mklement0 :PowerShell 還支持 : 作為參數名稱和參數之間的分隔符)

$firstDate=Get-Date -Year 2018 -Month 12 -Day 1
$lastDate=Get-Date -Year 2020 -Month 1 -Day 12
$value = $firstDate -eq $lastDate
# value = False. 

這將為您提供false值,因為日期不匹配。

更新

即使您將年、月和日設置為相同,上述代碼也永遠不會等於 true。 這是因為 DateTime 的時間因素將關閉。 您可以執行以下操作以確保僅比較Dates

$value = $firstDate.Date -eq $lastDate.Date

這只會比較日期並忽略時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM