如何使用 powershell 排除周末和节假日
How to Exclude Weekends and holidays using powershell
我正在尝试在 PowerShell 中编写代码以排除周末和节假日,但出现错误,你能帮我解决这个问题吗?
$a = Get-Date -format "yyyy-MM-dd"
#Get-Date -format "dddd yyyy-MM-dd"
if($a.DayOfWeek -eq 'Tuesday')
{
Write-Host "tuesday"
}
else
{
Write-Host "false"
}
It is recommended to use Set-StrictMode
for your development scripts to identify code issues.
来自Get-Help Set-StrictMode -online
:
The Set-StrictMode
cmdlet configures strict mode for the current
scope and all child scopes, and turns it on and off. When strict mode
is on, PowerShell generates a terminating error when the content of an
expression, script, or script block violates basic best-practice
coding rules.
您可以立即使用 Set-StrictMode
:
获得详尽的问题答案
Set-StrictMode -Version latest
$a=Get-Date -format "yyyy-MM-dd"
$a.DayOfWeek -eq 'Tuesday'
The property 'DayOfWeek' cannot be found on this object. Verify that the property exists.
At line:3 char:1
+ $a.DayOfWeek -eq 'Tuesday'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
错误来自于您使用 -Format "yyyy-MM-dd"
将日期从 Get-Date
转换为 字符串
$a = Get-Date -format "yyyy-MM-dd"
$a.GetType()
表明 $a 现在是一个字符串。
字符串不像 DateTime 对象那样有 DayOfWeek
属性。
只需删除 -Format
部分
$a = Get-Date
if($a.DayOfWeek -eq 'Tuesday') {
Write-Host "Today is Tuesday"
}
else {
Write-Host "Today is NOT Tuesday"
}
$a = Get-Date -format "yyyy-MM-dd"
#Get-Date -format "dddd yyyy-MM-dd"
if($a.DayOfWeek -eq 'Tuesday')
{
Write-Host "tuesday"
}
else
{
Write-Host "false"
}
It is recommended to use Set-StrictMode
for your development scripts to identify code issues.
来自Get-Help Set-StrictMode -online
:
The
Set-StrictMode
cmdlet configures strict mode for the current scope and all child scopes, and turns it on and off. When strict mode is on, PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.
您可以立即使用 Set-StrictMode
:
Set-StrictMode -Version latest
$a=Get-Date -format "yyyy-MM-dd"
$a.DayOfWeek -eq 'Tuesday'
The property 'DayOfWeek' cannot be found on this object. Verify that the property exists. At line:3 char:1 + $a.DayOfWeek -eq 'Tuesday' + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException + FullyQualifiedErrorId : PropertyNotFoundStrict
错误来自于您使用 -Format "yyyy-MM-dd"
Get-Date
转换为 字符串
$a = Get-Date -format "yyyy-MM-dd"
$a.GetType()
表明 $a 现在是一个字符串。
字符串不像 DateTime 对象那样有 DayOfWeek
属性。
只需删除 -Format
部分
$a = Get-Date
if($a.DayOfWeek -eq 'Tuesday') {
Write-Host "Today is Tuesday"
}
else {
Write-Host "Today is NOT Tuesday"
}