将Datetime传递给powershell脚本

将Datetime传递给powershell脚本,powershell,Powershell,在powershell脚本中,我尝试使用两个datetime参数调用另一个脚本 父脚本: $startDate = "02/05/2015 19:00" $endDate = "02/06/2015 14:15" Invoke-Expression "C:\MoveAndDeleteFolder.ps1 -startDate $startDate -endDate $endDate" 子脚本: param ( [Datetime]$startDate, [Datetime]$

在powershell脚本中,我尝试使用两个datetime参数调用另一个脚本

父脚本:

$startDate = "02/05/2015 19:00"
$endDate = "02/06/2015 14:15"

Invoke-Expression "C:\MoveAndDeleteFolder.ps1 -startDate $startDate -endDate $endDate"
子脚本:

param
(
    [Datetime]$startDate,
    [Datetime]$endDate
)

$startDate| Write-Output
结果:

2015年2月10日星期二00:00:00

->时间浪费了


有人知道为什么吗?

问题是您正在使用字符串调用脚本

调用表达式
“C:\MoveAndDeleteFolder.ps1-startDate$startDate-endDate$endDate”

$startdate
$enddate
包含日期和时间之间的空格,因此在解析日期时,日期被视为参数值,但由于空格,时间被视为参数。下面的示例显示了这一点

test1.ps1:

param
(
    [Datetime]$startDate,
    [Datetime]$endDate
)

$startDate| Write-Output

"Args:"
$args
脚本:

$startDate = "02/05/2015 19:00"
$endDate = "02/06/2015 14:15"

Write-Host "c:\test.ps1 -startDate $startDate -endDate $endDate"

Invoke-Expression "c:\test.ps1 -startDate $startDate -endDate $endDate"
输出:

#This is the command that `Invoke-Expression` runs.
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

#This is the failed parsed date
5. februar 2015 00:00:00

Args:
19:00
14:15
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

5. februar 2015 19:00:00
这里有两种解决方案。您可以直接运行脚本,而无需
调用表达式
,它将正确发送对象

c:\test.ps1 -startDate $startDate -endDate $endDate
输出:

#This is the command that `Invoke-Expression` runs.
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

#This is the failed parsed date
5. februar 2015 00:00:00

Args:
19:00
14:15
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

5. februar 2015 19:00:00
或者可以在表达式字符串中引用
$startDate
$endDate
,如:

Invoke-Expression "C:\MoveAndDeleteFolder.ps1 -startDate '$startDate' -endDate '$endDate'"

问题是您正在使用字符串调用脚本

调用表达式
“C:\MoveAndDeleteFolder.ps1-startDate$startDate-endDate$endDate”

$startdate
$enddate
包含日期和时间之间的空格,因此在解析日期时,日期被视为参数值,但由于空格,时间被视为参数。下面的示例显示了这一点

test1.ps1:

param
(
    [Datetime]$startDate,
    [Datetime]$endDate
)

$startDate| Write-Output

"Args:"
$args
脚本:

$startDate = "02/05/2015 19:00"
$endDate = "02/06/2015 14:15"

Write-Host "c:\test.ps1 -startDate $startDate -endDate $endDate"

Invoke-Expression "c:\test.ps1 -startDate $startDate -endDate $endDate"
输出:

#This is the command that `Invoke-Expression` runs.
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

#This is the failed parsed date
5. februar 2015 00:00:00

Args:
19:00
14:15
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

5. februar 2015 19:00:00
这里有两种解决方案。您可以直接运行脚本,而无需
调用表达式
,它将正确发送对象

c:\test.ps1 -startDate $startDate -endDate $endDate
输出:

#This is the command that `Invoke-Expression` runs.
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

#This is the failed parsed date
5. februar 2015 00:00:00

Args:
19:00
14:15
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

5. februar 2015 19:00:00
或者可以在表达式字符串中引用
$startDate
$endDate
,如:

Invoke-Expression "C:\MoveAndDeleteFolder.ps1 -startDate '$startDate' -endDate '$endDate'"

问题是您正在使用字符串调用脚本

调用表达式
“C:\MoveAndDeleteFolder.ps1-startDate$startDate-endDate$endDate”

$startdate
$enddate
包含日期和时间之间的空格,因此在解析日期时,日期被视为参数值,但由于空格,时间被视为参数。下面的示例显示了这一点

test1.ps1:

param
(
    [Datetime]$startDate,
    [Datetime]$endDate
)

$startDate| Write-Output

"Args:"
$args
脚本:

$startDate = "02/05/2015 19:00"
$endDate = "02/06/2015 14:15"

Write-Host "c:\test.ps1 -startDate $startDate -endDate $endDate"

Invoke-Expression "c:\test.ps1 -startDate $startDate -endDate $endDate"
输出:

#This is the command that `Invoke-Expression` runs.
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

#This is the failed parsed date
5. februar 2015 00:00:00

Args:
19:00
14:15
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

5. februar 2015 19:00:00
这里有两种解决方案。您可以直接运行脚本,而无需
调用表达式
,它将正确发送对象

c:\test.ps1 -startDate $startDate -endDate $endDate
输出:

#This is the command that `Invoke-Expression` runs.
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

#This is the failed parsed date
5. februar 2015 00:00:00

Args:
19:00
14:15
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

5. februar 2015 19:00:00
或者可以在表达式字符串中引用
$startDate
$endDate
,如:

Invoke-Expression "C:\MoveAndDeleteFolder.ps1 -startDate '$startDate' -endDate '$endDate'"

问题是您正在使用字符串调用脚本

调用表达式
“C:\MoveAndDeleteFolder.ps1-startDate$startDate-endDate$endDate”

$startdate
$enddate
包含日期和时间之间的空格,因此在解析日期时,日期被视为参数值,但由于空格,时间被视为参数。下面的示例显示了这一点

test1.ps1:

param
(
    [Datetime]$startDate,
    [Datetime]$endDate
)

$startDate| Write-Output

"Args:"
$args
脚本:

$startDate = "02/05/2015 19:00"
$endDate = "02/06/2015 14:15"

Write-Host "c:\test.ps1 -startDate $startDate -endDate $endDate"

Invoke-Expression "c:\test.ps1 -startDate $startDate -endDate $endDate"
输出:

#This is the command that `Invoke-Expression` runs.
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

#This is the failed parsed date
5. februar 2015 00:00:00

Args:
19:00
14:15
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

5. februar 2015 19:00:00
这里有两种解决方案。您可以直接运行脚本,而无需
调用表达式
,它将正确发送对象

c:\test.ps1 -startDate $startDate -endDate $endDate
输出:

#This is the command that `Invoke-Expression` runs.
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

#This is the failed parsed date
5. februar 2015 00:00:00

Args:
19:00
14:15
c:\test.ps1 -startDate 02/05/2015 19:00 -endDate 02/06/2015 14:15

5. februar 2015 19:00:00
或者可以在表达式字符串中引用
$startDate
$endDate
,如:

Invoke-Expression "C:\MoveAndDeleteFolder.ps1 -startDate '$startDate' -endDate '$endDate'"
或者试试这个:

Invoke-Expression "C:\MoveAndDeleteFolder.ps1 -startDate `"$startDate`" -endDate `"$endDate`""
或者试试这个:

Invoke-Expression "C:\MoveAndDeleteFolder.ps1 -startDate `"$startDate`" -endDate `"$endDate`""
或者试试这个:

Invoke-Expression "C:\MoveAndDeleteFolder.ps1 -startDate `"$startDate`" -endDate `"$endDate`""
或者试试这个:

Invoke-Expression "C:\MoveAndDeleteFolder.ps1 -startDate `"$startDate`" -endDate `"$endDate`""

哇!我没有注意到空间。那是。。。。。令人尴尬的好球。:)但仍建议避免调用表达式。至少提供的样品不需要它。哇。我没有注意到空间。那是。。。。。令人尴尬的好球。:)但仍建议避免调用表达式。至少提供的样品不需要它。哇。我没有注意到空间。那是。。。。。令人尴尬的好球。:)但仍建议避免调用表达式。至少提供的样品不需要它。哇。我没有注意到空间。那是。。。。。令人尴尬的好球。:)但仍建议避免调用表达式。至少提供的示例不需要它。老实说,我不明白为什么人们如此热衷于使用
调用表达式
。它几乎不需要(与许多其他语言中的
eval
相同,实际上也是如此)。它只会使事情复杂化,丢掉了PowerShell相对于更简单的shell的所有好处,而且几乎从来都不是问题的解决方案(只是另一个问题)。老实说,我从来都不明白为什么人们如此热衷于使用
调用表达式
。它几乎不需要(与许多其他语言中的
eval
相同,实际上也是如此)。它只会使事情复杂化,丢掉了PowerShell相对于更简单的shell的所有好处,而且几乎从来都不是问题的解决方案(只是另一个问题)。老实说,我从来都不明白为什么人们如此热衷于使用
调用表达式
。它几乎不需要(与许多其他语言中的
eval
相同,实际上也是如此)。它只会使事情复杂化,丢掉了PowerShell相对于更简单的shell的所有好处,而且几乎从来都不是问题的解决方案(只是另一个问题)。老实说,我从来都不明白为什么人们如此热衷于使用
调用表达式
。它几乎不需要(与许多其他语言中的
eval
相同,实际上也是如此)。它只会使事情复杂化,丢掉PowerShell相对于更简单的shell的所有好处,而且几乎从来都不是问题的解决方案(只是另一个问题)。