gpt4 book ai didi

powershell - 如何将今天的日期作为 powershell 中参数的默认值

转载 作者:行者123 更新时间:2023-12-05 00:50:56 25 4
gpt4 key购买 nike

我想创建一个脚本来帮助复制在某个时间范围内修改的文件。我要离开$EndDate参数作为可选,在这种情况下,我希望脚本使用今天的日期作为默认日期。

下面是脚本:

param (
[Parameter(Mandatory=$True)]
[string]$Path,

[Parameter(Mandatory=$True)]
[string]$targetDir,

[Parameter(Mandatory=$True)]
[string]$BeginDate,

[Parameter(Mandatory=$False)]
[string]$EndDate,

[switch]$force
)

Get-ChildItem -Path $Path -Recurse | Where-Object {$_.LastWriteTime -gt $BeginDate -and $_.LastWriteTime -lt $EndDate }| cp -Destination $targetDir -Force

最佳答案

[Parameter(Mandatory=$False)][string]$enddate = Get-Date,

给它一个像这样的默认值,你可能还想格式化它:
[Parameter(Mandatory=$False)][string]$enddate = (Get-Date -f dd\MM\yy)

关于powershell - 如何将今天的日期作为 powershell 中参数的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45060435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com