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

标签 powershell parameters

我想创建一个脚本来帮助复制在某个时间范围内修改的文件。我要离开$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/

相关文章:

java - 如何设置默认方法参数值?

python - 我正在寻找一种更简洁的方法来获取多个函数参数的 len

java - 为数据库类创建通用 "getColumn"方法 (Java)

c++ - 如何将函数指针作为模板参数传递

vb.net - 从 VB 在 Access 上运行查询,传递变量或函数作为查询参数

c# - 使用 C# 中的调用命令抛出异常

arrays - 如何编写一个 PowerShell 高级函数,该函数既可以使用管道输入的对象,也可以使用从参数值中获取的对象?

powershell - 在 powershell 中重命名文件名的一部分

regex - 如何将文件名的一部分移动到不同位置

powershell - 无法安装最新的 Azure PowerShell