How to pass DateTime as parameter in PowerShell?

I have a script that calls another script and passes parameters along with it. Whenever I try to go to datetime, parts of the datetime are used as arguments for other parameters.

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

      

script2.ps1

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

$test1
$dateEnd
$siteUrl
$outputFile

Read-Host -Prompt "Press Enter to exit"

      

This will lead to:

test1
Wednesday, December 31, 2014 12:00:00 AM
00:00:00
http://foo.com

      

EDIT - string as date, typo on my part:

If I pass in a string 12/31/14

, it works fine, but I would like to be able to pass in a date.

+3


source to share


3 answers


On the line that assigns $ argumentList, change the $ dateEnd parameter to be $dateEnd.toString('s')

.

The arguments to Windows processes are strings, not objects, so Start-Process must convert the ArgumentList to a string. Powershell.exe then parses that string, splitting it into spaces (like any Windows process) and putting it back in your parameters.



Usually this should work fine, but in this case, pay attention to what happens at startup (get-date).tostring()

. The default output for a DateTime object contains a space that interferes with parsing.

So the solution is to format the date parameter in the argument list without spaces and still remain in a format that DateTime :: Parse () can recognize (so PowerShell can reload the variable at the other end) Passing 's'

to DateTime :: toString () gives you a format like this.

+2


source


This is due to the combination of using positional parameters and quoting. Here's one change that should make it work (quoted date input):

$argumentList = "-file D:\script2.ps1", "test1", "`"$dateEnd`"", $siteUrl, $outputPath

      



Is there a reason why you are calling a separate PowerShell process? You can call it like this:

#This will run in separate scope 
    & ".\script2.ps1" test1 $dateEnd $siteUrl $outputPath

#This will run in the local (current) scope:
    . ".\script2.ps1" test1 $dateEnd $siteUrl $outputPath

      

+6


source


I think it is a problem that you are using DateTime objects incorrectly. If you want to specify a date, please do so. What you are suggesting as a string is the time, not the date. If you want to specify both , provide both: Get-Date.

$endDate = "12/31/14 08:00:00"

      

It's 8:00 am on December 31st.

Then, in your script, call what you want specifically.

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

$test1
$dateEnd.ToLongDateString()
$dateEnd.ToLongTimeString()
$siteUrl
$outputFile

      

When a DateTime object is cast to this (shown here with your other test data) you should get:

test1
Wednesday, December 31, 2014
8:00:00 AM
http://foo.com
C:\myfolder

      

0


source







All Articles