小编Mic*_*ckB的帖子

如何在PowerShell中将DateTime作为参数传递?

我有一个脚本,它调用另一个脚本并传入参数.每当我尝试传入日期时间时,日期时间的一部分将用作其他参数的参数.

script1.ps1

$dateEnd = Get-Date "12/31/14"
$siteUrl = "http://foo.com"
$outputPath = "c:\myFolder"

$argumentList = "-file .\script2.ps1", "test1", $dateEnd, $siteUrl, $outputPath
Start-Process powershell.exe -ArgumentList $argumentList
Run Code Online (Sandbox Code Playgroud)

script2.ps1

param
(
     [string]$test1,
     [DateTime]$dateEnd,
     [string]$siteUrl,
     [string]$outputFile
)      

$test1
$dateEnd
$siteUrl
$outputFile

Read-Host -Prompt "Press Enter to exit"
Run Code Online (Sandbox Code Playgroud)

这将导致:

test1
Wednesday, December 31, 2014 12:00:00 AM
00:00:00
http://foo.com
Run Code Online (Sandbox Code Playgroud)

编辑 - 字符串作为日期,我的错字:

如果我通过字符串12/31/14它工作正常,但我希望能够通过日期.

parameters powershell datetime

3
推荐指数
2
解决办法
2万
查看次数

标签 统计

datetime ×1

parameters ×1

powershell ×1